简体   繁体   English

while True 循环中的 Python 错误

[英]Python Error on while True loop

total = 0                                                   
print ("Enter first number")
num1 = input()
print ("Enter 1)Add 2)Minus 3)Multiply 4)Divide")
choice = input()

while True:
    print("Wrong Answer Pick Again")
    print("Enter 1)Add 2)Minus 3)Multiply 4)Divide")
    choice = input()
    if choice => 1 and choice =< 4:
        break

if choice == 1:
  print ('Enter second number')
  num2 = input()
  total = num1 + num2 

elif choice == 2:
  print ('Enter second number')
  num2 = input()
  total = num1 - num2
elif choice == 3:
  print ('Enter second number')
  num2 = input()
  total = num1 * num2
elif choice == 4:
  print ('Enter second number')
  num2 = input()
  total = num1 / num2

print("Total")
print (total)

I`m getting a syntax error on the "if choice => 1 and choice =<4:" can some one please help.我在“if choice => 1 and choice =<4:”上遇到语法错误,有人可以帮忙吗? I have tried so many different things and nothing have worked.我尝试了很多不同的东西,但没有任何效果。

This script should work for you :这个脚本应该适合你:

total = 0                                                   

print ("Enter 1)Add 2)Minus 3)Multiply 4)Divide")
choice = int(input())

for _ in range(int(input("total test cases"))):


    if choice >= 1 and choice <= 4:
        if choice == 1:
            print ("Enter first number")
            num1 = int(input())
            print ('Enter second number')
            num2 = int(input())
            total = num1 + num2
            print("Total is: ",total)
            choice=int(input("enter choice again"))

        elif choice == 2:
            print ("Enter first number")
            num1 = int(input())
            print ('Enter second number')
            num2 = int(input())
            total = num1 - num2
            print("Total is: ",total)
            choice=int(input("enter choice again"))

        elif choice == 3:
            print ("Enter first number")
            num1 = int(input())
            print ('Enter second number')
            num2 = int(input())
            total = num1 * num2
            print("Total is: ",total)
            choice=int(input("enter choice again"))

        elif choice == 4:
            print ("Enter first number")
            num1 = int(input())
            print ('Enter second number')
            num2 = int(input())
            total = num1 / num2
            print("Total is: ",total)
            choice=int(input("enter choice again"))
    else:
        print("wrong number entered")
        choice=int(input("enter again"))

Note:If you find this answer helpful you can mark this answer correct注意:如果您觉得此答案有帮助,您可以将此答案标记为正确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM