为什么代码会产生这个答案? [蟒蛇]

时间:2014-11-22 17:07:28

标签: python

基本上我是在设置代码,但由于某些随机原因,它总是说答案是错误的。

所以,说它是10 x 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()

1 个答案:

答案 0 :(得分:2)

probe变量是整数类型。你从用户那里得到的答案是一个字符串。用以下方法测试:

  

打印类型(ans1)

     

打印类型(prob1)

因此您需要将用户的输入转换为整数:

  

如果int(ans1)== prob1