繁体   English   中英

if-else if语句无法正常工作

[英]If-else if statement not working properly

我正在尝试为姐姐做一个乘法游戏,但是我的代码没有正确通过if-else语句。 我不知道为什么会这样。 有人可以告诉我我做错了吗?

from random import randint

print "Welcome to Multiplication Practice."
print "\n-------"

correct = 0

wrong = 0

while wrong<3:

    a = randint(1,9)
    b = randint(1,9)
    print "What is %s x %s?" %(a,b)
    c = a * b
    action = raw_input("> ")
    if action == c:
        print "correct!"
        correct +=1

    elif  action != c:
        print "Wrong!"
        wrong +=1

    else:
        print "Invalid answer."

print correct 

print wrong

raw_input返回一个字符串。 这将永远不会等于一个数字。 您需要将输入的字符串转换为数字:

action = int(raw_input("> "))

raw_input始终返回一个字符串,您需要将其转换为要与之比较的类型,在这种情况下,它是int(raw_input("> "))

暂无
暂无

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

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