我目前正在嘗試做一個分區程序,詢問隨機分區的問題。有兩件事情阻止我這樣做:1)我的程序認爲,按某事劃分的所有東西總是爲0。 8除以2 = 0. 2)我需要劃分沒有浮點數,比如144/5。所以在這裏,它是:如何改進我的分區程序? (Easy Python)
import sys
import random
guessRight=0
guessWrong=0
while True:
num1 = random.randint(0,12) #probably messed up here
num2 = random.randint(12,144) #and here
print "To exit this game type 'exit'"
theyputinstuffhere = raw_input("What is " + str(num2) + " divided by " + str(num1) + "? ") #maybe messed up here
if theyputinstuffhere == "exit":
print "Now exiting game!"
sys.exit()
elif int(theyputinstuffhere) == num1/num2: #maybe messed this whole elif too
print num1/num2
print "Correct!"
guessRight=guessRight+1
print "You have gotten " + str(guessRight) + " answer(s) right and you got " + str(guessWrong) + " wrong"
else:
print "Wrong! The correct answer is: " + str(num1/num2)
guessWrong=guessWrong+1
print "You have gotten " + str(guessRight) + " answer(s) right and you got " + str(guessWrong) + " wrong"
這是它目前打印:
To exit this game type 'exit'
What is 34 divided by 11? #I type any number (e.g. 3)
Wrong! The correct answer is: 0
You have gotten 0 answer(s) right and you got 1 wrong
'num1'總是比'num2'小。你可能混淆了這些值。 – Matthias
請注意,提示符(「num2除以num1」)和檢查(num1/num2)不是說同樣的事情。 –
@Matthias查看輸入行,似乎正是這種情況。在該行中,它打印'/'。 –
JAB