检查用户是否要继续,使用while并继续使用Python 3.3

时间:2016-01-16 21:45:10

标签: python while-loop break continue

这是一个简单的程序,用于检查数字是奇数还是偶数。我想检查用户是否要继续运行,当我运行它时,我在中断时出现无效的语法错误行,我出错了什么?

while True:
if cont != "no":
    num = (int(input("Type a number. ")))
    num_remainder = num % 2
    if num_remainder == 0:
        print ()
        print (num, " is an even number.")
    else:
        print ()
        print (num, " is an odd number.")
cont= (input("Would you like to continue?")
    continue
else:
    break

由于

1 个答案:

答案 0 :(得分:2)

我认为这更简单,

while True:
    num = int(input("Type a number. "))
    print ()  # blank line before assert whether is or not a even number
    if num % 2 == 0:
        print (num, " is an even number.")
    else:
        print (num, " is an odd number.")

    if input("Would you like to continue?") == 'no':  # Ohh, you want to break now.
        break