#๐Ÿ”’ Using continue for specific part of loop - Beginner

131 messages ยท Page 1 of 1 (latest)

dry lance
#

So as I'm trying to improve my calculator program after every result I ask the user if they would like to use the calculator. Now I've created a condition if they say Y it would repeat the process. However at the bottom I added != "N" for any other respose other than Y or N. Outside the following code I provided there is another while loop which takes the response of N and stops the program.

But my question is, for the last elif condition, how can I create a loop where if the user doesn't type either Y or N it would keep asking again? I tried continue but that would start the whole program (the loop outside the code I provided.)

if operator == "Multiply":
            multiply = number_One * number_Two
            print("Answer:", multiply)
            restart = input("Would you like to continue (Y/N)? ")
            if restart == "Y":
                continue
            elif restart != "N":
                print(
                    "That's not one of the options. Please type in Y for (Yes) or N for (No)"
                )```
lyric lanternBOT
#

@dry lance

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

minor meteor
#

One possible solution is a while loop that keeps prompting the user to type in Y or N until the user inputs one of those letters

dry lance
#

So another while loop in a while loop?

long void
#

whats the variable that holds the users answer

dry lance
long void
#

hrm

#

wait i think i see it, its restart

dry lance
#

I just answered you with that question

#

How is it restart

long void
#

okay so if you do while restart not Y or N "whatever is is you want to do"

long void
#

as seen by restart = input()

dry lance
dry lance
dry lance
long void
#

thats basically what i just says this is so sad

dry lance
#

My bad ๐Ÿ˜”

long void
#

all good lol i can barely ever understand myself, no reason i should expect anyone else to

dry lance
#

@minor meteor Seems to work. Thanks for the answer

#

I wrapped the while around the Y/N question specifically

minor meteor
#

Nice

dry lance
#

Also another question

#
while (restart != "Y") or (restart != "N"):```
#

is the condition here suppose to be or or and

#

Or is it that either could work

minor meteor
#

or

pine aurora
minor meteor
#

You can try and, I'm pretty sure that loop would be never stop (or start? idk my brain isn't braining right now)

dry lance
#

Thanks again

#

Wait that didn't work lol

#

Came back to the same issue again

#
if operator == "Divide":
            if number_Two == 0:
                print(
                    "You cannot divide by 0. Start the program again if you would like to try again.clear"
                )
                exit()
            divide = number_One / number_Two
            print("Answer:", divide)
            
            while (restart != "Y") or (restart != "N"):
                restart = input("Would you like to continue (Y/N)? ")
                if restart == "Y":
                    continue
                elif restart != "N":
                    print(
                        "That's not one of the options. Please type in Y for (Yes) or N for (No)"
                    )```
#

The loop in the second while when the user types Y repeats the same question again

#

@minor meteor

minor meteor
#

Oh you might want break

#

instead of continue

#

Or wait hold on

dry lance
#

Wouldn't break "break" out of the loop and move to the enxt line

minor meteor
#

You don't need if restart == "Y" anymore

minor meteor
dry lance
dry lance
minor meteor
#

continue too

dry lance
#

In case you're wondering what's menu_select it's just choosing between calculator and other apps that I made

#

So don't want to confuse on that

minor meteor
#

I think this makes more sense:

            
            restart = input("Would you like to continue (Y/N)? ")
            while (restart != "Y") or (restart != "N"):
                
                restart = input(
                        "That's not one of the options. Please type in Y for (Yes) or N for (No)"
                    )```
dry lance
#

Oooh

minor meteor
#

Oh wait I forgot the input inside the loop

dry lance
#

Then the if statement = Y would be outside and underneath while yea?

minor meteor
#

There I edited it

minor meteor
dry lance
#

Or will While automatically recognize that

minor meteor
#

I forgot it, I have now edited it

lament sage
#

Do you guys need help?

dry lance
#

Isn't restart suppose to be within while

#

Because right now it's just infinitly asking the question whether you write Y or N

minor meteor
#

By the way, your main while loop (at the very top) is:
while restart != "no": operator = (
and you're asking for "N"

#

you're typing lowercase y and n

#

Right now your code is case sensitive

dry lance
#

Ah you didn't add it my bad lol

minor meteor
#

If you want to make it case insensitive, you can use
restart = input("...").lower()

dry lance
minor meteor
minor meteor
#

So the inner loop is still case sensitive

dry lance
#

Right

#

Let me try now that I've fixed that

dry lance
#

inputs are ended with .title().strip()

minor meteor
#

Show inner loop code again?

dry lance
#

so cace sensitive shouldn't be an issue

#
if operator == "Divide":
            if number_Two == 0:
                print(
                    "You cannot divide by 0. Start the program again if you would like to try again.clear"
                )
                exit()
            divide = number_One / number_Two
            print("Answer:", divide)
            
            restart = input("Would you like to continue (Y/N)? ").title().strip()
            while (restart != "Y") or (restart != "N"):
                
                restart = input(
                        "That's not one of the options. Please type in Y for (Yes) or N for (No)"
                    ).title().strip()```
minor meteor
#

yeah there's a problem with the loop condition

#

it's always True

dry lance
#

It's funny we went from it was never looping to now it's looping forever

minor meteor
#

lol

#

Do you know lists?

dry lance
#

Not yet

#

My next lesson is libraries tho

#

Dw it is not a class lesson or anything

minor meteor
#

I'm thinking about how to fix the loop without it being messy

dry lance
#

Technically speaking if I knew def by now the code would've been a lot shorter. But it's messy for now. As I'm learning I'm updating my code with it so

#

Messy for now is fine

minor meteor
#
while not ((restart == "Y") or (restart == "N")):
#

Messy but I think it should work

dry lance
#

Wait what's not

#

Isn't that the same as !=

minor meteor
#

!e

print(not True)
print (not False)
lyric lanternBOT
dry lance
#
while not ((restart == "Y") or (restart == "N")):
while (restart != "Y") or (restart != "N"):```
#

Explain me the difference lol

lean axle
dry lance
dry lance
#

Soo

#

Which one are we picking

minor meteor
#

The first one

dry lance
#
while restart != "Y" and restart != "N":
#

This?

minor meteor
#

Yes

dry lance
#

I asked earlier with and and you said restart cannot be Y and N at the same time lol

#

Just want to make sure

minor meteor
#

Hmmm yeah that's my mistake, sorry

dry lance
#

No worries

#

Fixed

#

Thank you for the help

minor meteor
#

No problem!

dry lance
#

At the moment when the user types N the only way to exit the code is typing exit()

minor meteor
#

As an input?

dry lance
#

Yea so like

#
if operator == "Multiply":
            multiply = number_One * number_Two
            print("Answer:", multiply)

            while (restart != "Y") or (restart != "N"):
                restart = input("Would you like to continue (Y/N)? ")
                
                restart = input("Would you like to continue (Y/N)? ").title().strip()
            while restart != "Y" and restart != "N":

                restart = (
                    input(
                        "That's not one of the options. Please type in Y for (Yes) or N for (No)"
                    )
                    .title()
                    .strip()
                )
                exit()

        if operator == "Divide":
            if number_Two == 0:
                print(
                    "You cannot divide by 0. Start the program again if you would like to try again.clear"
                )
                exit()
            divide = number_One / number_Two
            print("Answer:", divide)

            restart = input("Would you like to continue (Y/N)? ").title().strip()
            while restart != "Y" and restart != "N":

                restart = (
                    input(
                        "That's not one of the options. Please type in Y for (Yes) or N for (No)"
                    )
                    .title()
                    .strip()
                )
                exit()```
#

Top multiply bottom divide

#

I was wondering

#

Why not just type exit() once at the very end

#

Is that possible?

minor meteor
#

I think so

dry lance
#

Yea cuz you know they say

#

Less lines better, and I want to get used to that

#

Because right now if I don't type exit() but run the loop with N it keeps asking the question again

lyric lanternBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.