Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

Python: why isn't my if statement running

I've been trying to write basic settings for my code and for some reason my if statement isn't working. I am pretty sure that my mistake is really stupid, but I can't find it.

elif menu == 3:
    set1 = 3
    print("1.Restart the program automatically")
    print("2.Restart the program manually")
    while set1 != 1 or set1 != 2:
        set1 == int(input("Please enter your choice:"))
    if set1 == 1:
        print("Set 1 works")
    elif set1 == 2:
        print("Set 2 works")
    else:
        print("Smash the monitor")
print("Goes to the main body")
question from:https://stackoverflow.com/questions/66068182/python-why-isnt-my-if-statement-running

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

there are two mistakes you need to fix.

  1. change while set1 != 1 or set1 != 2: to while set1 != 1 and set1 != 2: otherwise it will never stop.
  2. change set1 == int(input("Please enter your choice:")) to set1 = int(input("Please enter your choice:"))

now everything is Ok. final code must look like this:

elif menu == 3:
    set1 = 3
    print("1.Restart the program automatically")
    print("2.Restart the program manually")
    while set1 != 1 and set1 != 2:
        set1 = int(input("Please enter your choice:"))
    if set1 == 1:
        print("Set 1 works")
    elif set1 == 2:
        print("Set 2 works")
    else:
        print("Smash the monitor")
print("Goes to the main body")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...