2014-11-22 66 views
0

基本上我设置了一个代码,但由于一些随机的原因,它总是说答案是错误的。为什么代码产生这个答案? [Python]

因此,说这是'10×10' &我会说这是‘100’

然而,代码表示,回到我的身边,‘I'm sorry the answer is 100

为什么代码生成此回答?

下面是代码:

P.S:我知道,我没有为这一切做“Num_(NUMBER)”!

from random 
import randint 
import random 

correct = 0 

for i in range (3): 
    num_1 = randint (1, 10) 
    num_2 = randint (1, 10) 
    prob1 = num_1*num_2 

print (“What’s %d x %d? “ (num_1, num_2)) 
ans1 = input() 

If ans1 == prob1: 
    print (“That’s the correct answer! /n”) 
    correct = correct +1                
else: 
    print (“No I’m afraid the answer is %d. /n” & (prob1)) 

for n in range (3): 
    num_3 = randint (1, 10) 
    num_4 = randint (1, 10) 
    prob2 = num_3+num_4 

print (“What’s %d + %d? “ (num_3, num_4)) 
ans2 = input() 

If ans2 == prob2: 
    print (“That’s the correct answer! /n”) 
    correct = correct +1 
else: 
    print (“No I’m afraid the answer is %d. /n” & (prob2)) 

for m in range (4): 
    num_5 = randint (1, 10) 
    num_6 = randint (1, 10) 
    prob3 = num_5 - num_6 

print (“What’s %d - %d? “ (num_5, num_6)) 
ans3 = input() 

If ans3 == prob3: 
    print (“That’s the correct answer! /n”) 
    correct = correct +1 
else:                                
    print (“No I’m afraid the answer is %d. /n” & (prob3)) 

print (“I asked you 10 questions, you got %d of them right. “ %(correct)) 
exit() 
+0

您需要正确修复注记。 – timrau 2014-11-22 17:09:34

+0

我不认为编辑有帮助,看起来现在有一半程序没有显示。 – Redoubts 2014-11-22 17:12:57

+0

我已经添加了缩进 - 代码只粘贴了很长的包装空间而不是换行结尾 - 奇数。 – 2014-11-22 17:13:56

回答

2

探针变量是整数类型。你从用户那里得到的答案是一个字符串。

打印类型(ANS1)

打印类型(prob1)

,所以你需要将用户的输入转换为整数:

与测试此

if int(ans1)== prob1

相关问题