Reputation: 31
I wrote my code and someone told me to do while loop like this, but it's not right. It goes in to a infinite loop and as a newbiee I'm not sure what's wrong. I have looked at other searches and don't understand how they fixed their code because it is all different. Don't mark this as a duplicate because it stops me from getting answers directly for my code. I'm a newbie and I'll take all of your crap. Thanks
print ("enter a number 1 - 2: ")
userin = input()
while userin != 1 or userin != 2:
print ("Enter a number that is 1 or 2")
if userin == "1":
print ("\n One")
elif userin == "2":
print ("\n One", "\n Two")
else:
print ("Enter a number that is 1 or 2")
This was the original code without the while loop:
print ("Enter a number (2 - 3): ")
userin = input()
if userin == "2":
print("\n One","\n Two")
elif userin == "3":
print("\n One", "\n Two", "\Three")
else: print("Invalid. Enter a number2 -3!")
Upvotes: 1
Views: 103
Reputation: 388
The problem in your code is that you just take the user input once at the very beginning and the while loop continues to run based on that input resulting in an infinite loop. Basically, you never take the input again
Try using the following code:
print ("enter a number 1 - 2: ")
userin = input()
while userin != "1" and userin != "2":
print ("Enter a number that is 1 or 2")
userin = input()
if userin == "1":
print ("One\n")
elif userin == "2":
print ("Two\n")
Explanation: Initially you take an input from the user. And the while loop is started with a check for whether the input is neither 1 nor 2 i.e the loop only proceeds if userin is neither 1 nor 2. Further if it is neither 1 nor 2, the user is asked to re-enter a number. And input is taken again using userin = input()
Once the user enters either of 1 or 2, the loop exits and "One" or "Two" is printed according to the user's final input.
Upvotes: 1
Reputation: 20336
You have a couple problems. For one thing, when is userin
both equal to 1
and equal to 2
? Never! You need to use userin != 1 and userin != 2
. That isn't quite right either, though, because you are comparing strings with integers. You see input()
returns a string, but you are comparing it to 1
and 2
. You should be using userin != '1'
and userin != '2'
so that you are comparing strings to strings. Your final problem is that you keep comparing userin
to 1
and 2
, but you never redefine it after the original time. You need to add an input()
call into your while
loop. Your final code:
print ("enter a number 1 - 2: ")
userin = input()
while userin != '1' and userin != '2':
print ("Enter a number that is 1 or 2")
userin = input()
if userin == "1":
print ("\n One")
elif userin == "2":
print ("\n One", "\n Two")
Your else
is redundant because if userin
is neither 1
nor 2
, the while
loop will run again and print it.
Upvotes: 0
Reputation: 1
# Please try this...
ip_list = [1, 2] # input list
print ("enter a number 1 - 2: ")
userin = input()
while userin not in ip_list:
print ("Enter a number that is 1 or 2")
userin = input()
>>> while userin not in ip_list:
... print("Enter a number that is 1 - 2!")
... userin = input()
...
Enter a number that is 1 - 2!
5
Enter a number that is 1 - 2!
6
Enter a number that is 1 - 2!
-1
Enter a number that is 1 - 2!
0
Enter a number that is 1 - 2!
9
Enter a number that is 1 - 2!
1 <===== Here it exits while loop
Upvotes: 0
Reputation: 4418
The test userin != "1" or userin != "2"
is always true. If the user enters a 1, then the second part is true. If the user enters a 2, then the first part is true. So, the loop never ends. Try using and
instead of or
.
Upvotes: 3