-4
x = 0
while True:
  choice = int(input ("Choose 1 or 2"))

  if choice == 2:
    print("You chose 2")
    x == 1
  if choice == 1:
    print("You chose 1")
    x == 1
  if choice >2: 
    print("I said 1 or 2.")
    x == 0
  if choice <1:
    print("I said 1 or 2")
    x == 0

So if I choose 1 or 2 I want it to stop but if I choose otherwise I want it to loop but it's looping always no matter what could somebody help with this?

  • 2
    You might want to have a look at the suggestions in this thread, [how to ask for a valid user input](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Mr. T Jan 26 '18 at 13:47

4 Answers4

2

You do have to explicitly break out of an infinite loop:

while True:
    choice = int(input("Choose 1 or 2")) 
    if choice in (1, 2):
        print("You chose {}".format(choice))
        break
    print("I said 1 or 2")

Also note that x == 1 is not an assignment, but a boolean expression which - on its own - doesn't do anything, except raise an error if x isn't defined.

user2390182
  • 72,016
  • 6
  • 67
  • 89
1

As written your code has an infinite while True loop. The loop condition is fixed and there are no break statements, so naturally it loops forever.

  1. If your goal is to quit when x == 1 then you need to test for that in your loop condition. while True will never stop. Make sure you're assigning to x with x = 1 -- one equals sign.

    x = 0
    while not x:
      choice = int(input ("Choose 1 or 2"))
    
      if choice == 2:
        print("You chose 2")
        x = 1
      if choice == 1:
        print("You chose 1")
        x = 1
      if choice >2: 
        print("I said 1 or 2.")
      if choice <1:
        print("I said 1 or 2")
    
  2. Alternatively, you could explicitly break out of the loop. Then you wouldn't even need x.

    while True:
      choice = int(input ("Choose 1 or 2"))
    
      if choice == 2:
        print("You chose 2")
        break
      if choice == 1:
        print("You chose 1")
        break
      if choice >2: 
        print("I said 1 or 2.")
      if choice <1:
        print("I said 1 or 2")
    
  3. A third option is to check choice in the loop condition.

    choice = None
    while choice not in {1, 2}:
      choice = int(input ("Choose 1 or 2"))
    
      if choice == 2:
        print("You chose 2")
      if choice == 1:
        print("You chose 1")
      if choice >2: 
        print("I said 1 or 2.")
      if choice <1:
        print("I said 1 or 2")
    

There's some unnecessary repetition. I'd advise refactoring the checks a bit to eliminate duplicate code.

while True:
  choice = int(input("Choose 1 or 2"))

  if choice in {1, 2}:
    break
  else:
    print("I said 1 or 2.")

print("You chose " + str(choice))
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

Your code has several errors in it.

While True will do exactly that: it will loop until True becomes False. The problem is that your code has no way of doing that, i.e. looping infinitely.

You can either use break after a successful input, as other answers have already provided, or you can do something like this:

x = 0
flag = True

while flag:
    choice = int(input('Choose 1 or 2: '))
    if choice in [1, 2]:
        print('You chose', choice)
        x = choice
        flag = False
    else:
        print('I said 1 or 2.')

Initially a the boolean flag is set to True. The while loop checks the condition and sees that flag is set to true, so it enters the loop.

The user's input will be taken, assigned to variable choice, and if choice in [1, 2]: is an easy way of checking if the input is either 1 or 2. It checks if choice matches any element of the list [1, 2]. If so, then the user's input is valid.

On successful input, you assign the input to x and then set the flag to False. The next iteration of the while loop will again check the condition, but see that flag is set to False, and will break itself out of the loop.

An invalid input will print but will keep flag to True, initiating another iteration of the loop.

Your code also includes x == 0 where you meant x = 0. What you are intending to do is assign a value to variable x, but == is a boolean operator that checks for equality between two boolean expressions. You want to use the assign operator = instead.

A.B.
  • 169
  • 10
-1

You can add break statements

x = 0
while True:
  choice = int(input ("Choose 1 or 2"))

  if choice == 2:
    print("You chose 2")
    x=1
    break
  if choice == 1:
    print("You chose 1")
    x=1
    break 
  if choice >2: 
    print("I said 1 or 2.")
    x=0
  if choice <1:
    print("I said 1 or 2")
    x=0
Raja G
  • 5,973
  • 14
  • 49
  • 82