功能和可变的麻烦

时间:2015-01-30 20:22:25

标签: python function variables

我最近开始使用python,我的代码遇到了问题。基本上,我的代码应该生成10个添加的随机方程。我遇到的问题是Real_Answer变量即使它有正确的答案也不会识别User_Answer,它总是说它是错的,即使它是正确的......帮助!

def Random_Num():
 a = random.randint(0, 20)   #Random number between 0 - 20
 return a



def Ending(Real_Answer, Counter):
    if User_Answer == Real_Answer:
        print ("That is correct well done!")
        Counter = Counter + 1
    if User_Answer is not Real_Answer:
        print ("That is the wrong answer! The answer is", Real_Answer)

Counter = 0
i = 0
Name = input("What is your name?")
for i in range(0,10):
       Random_Num()
       Number_Add = Random_Num()
       Number_Again = Random_Num()
       print("The equation is", Number_Add, "+", Number_Again)
       Real_Ans = Number_Add + Number_Again
       int(Real_Answer)
       User_Answer = input(str("What do you think the answer is?"))
print("That is the end of the 10 questions")
print("Overall, you got ", Counter," out of 10!")

2 个答案:

答案 0 :(得分:0)

Real_Answer(一个整数)与一个字符串User_Answer进行比较时,你永远不会发现它们是相等的。 User_Answer = int(input('What do you think the answer is?'))是一种更好的方式。

答案 1 :(得分:0)

尝试:

def Ending(Real_Answer, Counter):
    if User_Answer == Real_Answer:
        print ("That is correct well done!")
        Counter = Counter + 1
    else:
        print ("That is the wrong answer! The answer is", Real_Answer)