我的程序:一個數學測驗,提示用戶選擇難度(初級,中級,高級),然後生成五個問題(隨機#),具體取決於他們的選擇。Python程序的TypeError(字符串格式與%d)
這是工作完全正常,直到我開始添加註釋和文檔字符串(我道歉,如果這是很難用亂讀,講師需要註釋的過高量。)
我提前道歉的混亂的代碼(教師需要大量的評論)。
# Import randint so program can generate random numbers
from random import randint
'''
Set the variable choice equal to the users input for choosing which level
they want to do. Then, we created an if statement equal to beginner
(for user's input). The range is the amount of questions for that
particular type of problem (addition, multiplication, etc.)
n1 is a random number from the range 1 to 10, program picks two
The variable 'sum' is set to the value n1 plus n2
'''
choice = input("Choose Level: Beginner,Intermediate, or Advanced?").lower()
if choice == "beginner":
correct = 0
for i in range(2):
n1 = randint(1, 10)
n2 = randint(1, 10)
# Set variable 'ans' to the input "What is (n1) plus (n2)"
# %d = program generates a random number for the problem
# correct = correct + 1 -> adds one point to the user's overall score
ans = input("What's %d plus %d?" % (n1, n2))
if int(ans) == sum:
print("That's correct, well done!\n")
correct = correct + 1
else:
print("No it's not correct. The answer is %d.\n" % sum) ## < Here is where I'm getting the error message.**
for i in range(3):
n1 = randint(1, 10)
n2 = randint(1, 10)
difference = n1 - n2
ans = input("What's %d minus %d?" % (n1, n2))
if int(ans) == difference:
print("That's correct, well done!\n")
correct = correct + 1
else:
print("No, that's not correct. The answer is %d.\n" % difference)
# This is where the program calculates the score, and tells the user
# "Well Done", "Need more practice" or "Ask teacher for help".
if(correct/5 >= 2/3):
print("Well done!")
elif(correct/5 >= 1/3):
print("You need more practice.")
else:
print("Contact the instructor.")
if choice == "intermediate":
correct = 0
for i in range(3):
n1 = randint(1, 25)
n2 = randint(1, 25)
product = n1 * n2
ans = input("What's %d times %d?" % (n1, n2))
if int(ans) == product:
print("That's correct, well done!\n")
correct = correct + 1
else:
print("No, that's not correct. The answer is %d.\n" % product)
for i in range(2):
n1 = randint(1, 25)
n2 = randint(1, 25)
quotient = n1/n2
# For this section, we had to use a float input type and 'round' so that
# the program will take in a decimal point, and tell the user to round.
ans = float(input("What's %d divided by %d? (Round 2 decimal places)" \
% (n1, n2)))
if round(ans, 2) == round(quotient, 2):
print("That's correct, well done!\n")
correct = correct + 1
else:
print("No, that's not correct. The answer is %f.\n" % quotient)
if(correct/5 >= 2/3):
print("Well done!")
elif(correct/5 >= 1/3):
print("You need more practice.")
else:
print("Contact the instructor.")
if choice == "advanced":
correct = 0
for i in range(3):
n1 = randint(11, 20)
n2 = randint(1, 10)
modulus = n1 % n2
ans = input("What's %d modulus %d?" % (n1, n2))
if int(ans) == modulus:
print("That's correct, well done!\n")
else:
print("No, that's not correct. The answer is %d.\n" % modulus)
for i in range(2):
n1 = randint(1, 10)
n2 = randint(1, 10)
exponent = n1 ** n2
ans = input("What's %d to the power of %d? \
Don't need commas for answers)" % (n1, n2))
if int(ans) == exponent:
print("That's correct, well done!\n")
else:
print("No, that's not correct. The answer is %d.\n" % exponent)
if(correct/5 >= 2/3):
print("Well done!")
elif(correct/5 >= 1/3):
print("You need more practice.")
else:
print("Contact the instructor.")
我得到這種類型的錯誤(在第一個else print語句):
我不知道如果我搞砸的東西,當我添加了評論,但我似乎無法弄清楚它是什麼。
語法錯誤和錯誤類型是不同的。如果你得到TypeError,不要說「語法錯誤」! – tdelaney
您是否將這兩個星號添加到代碼中以便爲我們突出顯示它,還是它真的是**打印(...'? – tdelaney
@tdelaney對,對此感到抱歉,應該在發佈之前進行校對,並且兩個星號是要突出它的帖子。 – ch695