Reputation: 9
I'm trying to get this loop to return to the the question of inputting user input until they decide to exit the program (choose selection 7), I'm having trouble figuring it out.
selection = input("Select 1 2 3 OR 7 ")
while selection != "7":
if selection == "1":
print("1 is selected")
l = int(input("INPUT ? "))
print("answer IS: ", l ** 2)
break
elif selection == "2":
print("2 is selected")
l = int(input("INPUT ? "))
w = int(input("INPUT ? "))
print("answer is: ", l * w)
break
elif selection == "3":
print("3 is selected")
r = int(input("INPUT ? "))
print("answer IS: ", (r ** 2) * 3.14)
break
elif selection == "7":
print("BYE")
The code works but I need to get it so that after it provides you with an answer, it'll ask you for the input again until you select 7 to exit. I just can't seem to get that part of it working.
Upvotes: 0
Views: 104
Reputation: 495
You can place the the input for selection inside of the loop:
selection = None
while selection != 7:
selection = input("Select 1 2 3 OR 7 ")
...
I'm trying to get this loop to return to the the question of inputting user input until they decide to exit
You also don't want to have any breaks inside of your while loop. Currently, your code exits after one input for selection (because selection of 1, 2, and 3 all have a break in them).
elif selection == "7":
print("BYE")
You can take the print statement and place it after the while loop. It's redundant to check if selection equals 7, when the while loop does that already.
Upvotes: 0
Reputation: 1332
Your selection variable should be inside the while loop if you want to continually ask for input from the user.
selection = 100
while selection != "7":
selection = input("Select 1,2,3 or 7: ")
Upvotes: 0
Reputation: 43
saying elif selection=="7" at the end contradicts yourself sort of.
if it's not 1 2 or 3, just say else: quit() or whatever it is in python, since anything other than 1 2 or 3 is irelevant
Upvotes: 0
Reputation: 9599
Put the first line inside the loop:
selection = None
while selection != 7:
selection = input("Select 1 2 3 OR 7 ")
# ...
Hope it helps.
Upvotes: 4