比较if语句不起作用?

时间:2017-03-31 18:39:58

标签: python python-3.x

此代码无效,有什么帮助吗? 主要问题是:

while True:
    x1 = input("what's a?")
    if x1 == a:
        delay("a is correct!")
        x2 = input("what's b?")
        if x2 == b:
            delay("you're correct!")
            delay("bye")
            break

以下是完整的参考代码:

from random import randint
from time import sleep
def delay(x):
    print(x)
    sleep(0.4)
delay("Welcome to the adding game")
delay("two numbers will be randomly generated")
delay("we'll call then a and b")
delay("We'll show you a+b first")
delay("then a-b")
delay("use that information to then determine the answer")
input("ready?")
a = randint(1,15)
b = randint(1,15)
print("a+b = ",a+b)
print("a-b = ",a-b)
print(a,b)
while True:
    x1 = input("what's a?")
    if x1 == a:
        delay("a is correct!")
        x2 = input("what's b?")
        if x2 == b:
            delay("you're correct!")
            delay("bye")
            break
        else:
            delay("wrong")
    else:
        delay("you're wrong")
        x3 = input("what's b?")
        if x3 == b:
            delay("that's correct!")
            delay("now use that info to figure out a")
        else:
            delay("wrong again")
            delay("look at the numbers and try again")

1 个答案:

答案 0 :(得分:1)

input()返回一个字符串,ab是整数,因此比较它们不会有效。

x1 = int(input("what's a?")),同样适用于x2x3,会使比较按预期进行。