2016-10-01 38 views
-1

我的程序:一個數學測驗,提示用戶選擇難度(初級,中級,高級),然後生成五個問題(隨機#),具體取決於他們的選擇。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語句):

enter image description here

我不知道如果我搞砸的東西,當我添加了評論,但我似乎無法弄清楚它是什麼。

+1

語法錯誤和錯誤類型是不同的。如果你得到TypeError,不要說「語法錯誤」! – tdelaney

+0

您是否將這兩個星號添加到代碼中以便爲我們突出顯示它,還是它真的是**打印(...'? – tdelaney

+0

@tdelaney對,對此感到抱歉,應該在發佈之前進行校對,並且兩個星號是要突出它的帖子。 – ch695

回答

0

您正在使用sum作爲變量名稱,但是會隱藏內置函數sum。將您的變量名稱更改爲其他名稱,錯誤將消失。

隨着if int(ans) == sum:,您所做的只是將int(ans)sum函數本身進行比較,而不是在傳遞一些數字時返回的結果。你想要做類似if int(ans) == sum(n1, n2):。你應該做的,而不是雖然,這筆保存到變量中(未命名sum,然後用它替換所有sum■對於例如:

_sum = sum(n1, n2) 
... 
if int(ans) == _sum: 
... 
print("No it's not correct. The answer is %d.\n" % _sum) 
0

您使用一個名爲sum變量,但不Python有一個叫sum的內建函數 - 你真的應該改變你的變量名,所以你不會遇到這種類型的問題 - python嘗試使用這個函數來計算%d,重命名它my_sum,你會得到一個不同的錯誤說它沒有定義。然後你可以修復它!

0

你需要改變一些值。

在行

ans = input("What's %d plus %d?" % (n1, n2)) 

添加

result = n1 + n2 
if int(ans) == result: 
    print("That's correct, well done!\n") 
     correct = correct + 1 
    else: 
     print("No it's not correct. The answer is %d.\n" % result) # Here is where I'm getting the error message.** 

... 
...