2014-11-22 88 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

相關問題