if-else if语句无法正常工作 [英] If-else if statement not working properly

查看:91
本文介绍了if-else if语句无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make a multiplication game for my sister, but my code doesn't go through if-else statements properly. I have no idea why this is so. Can someone tell what I am doing wrong?

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始终返回字符串 在这种情况下,您需要将其转换为要比较的类型 int(raw_input("> "))

raw_input always returns a string You need to convert it into the type you're comparing it to, in this case int(raw_input("> "))

这篇关于if-else if语句无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆