Multi choice While Loops


#1

Someone help me with my own piece of code.

y = ()
while y != 1 or 2:
  print("So this is your first time here, right?")
  print("""\n
  [1] "Yes"
  [2] "No"
  """)
  y = int(input("\n>"))

For some reason if I input ANY number, the loop will just repeat.
(RUN THIS CODE TO KNOW WHAT I MEAN)
Then if I remove the multi choice part, it doesn’t repeat but I want it to be 1 or 2.
Is there any other solution?


#2

A loop will run for as long as the loop condition evaluates to true.

your loop conditions consist of two parts: y != 1 and 2

2 isn’t compared with anything, so python will simply evaluate wether or not 2 is true, which it is:

if 2:
   print "positive integers are considered true"
else:
   print "positive integers are considered false"

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.