Python程序的TypeError(字符串格式为%d)

2024-04-19 22:37:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我的程序:一个数学测验,提示用户选择难度(初学者、中级、高级),然后根据他们的选择生成五个问题(随机#)

在我开始添加注释和doc字符串之前,它一直工作得很好(我很抱歉,如果很难阅读这些乱七八糟的内容,那么讲师需要大量的注释。)

我为代码的混乱提前道歉(讲师需要大量的评论)

# 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

我不确定我在添加评论时是否把事情弄糟了,但我似乎不知道是什么


Tags: thetoforinputifiselseprint
3条回答

使用名为sum的变量,但不定义它。Python有一个名为sum的内置函数—您真的应该更改变量名,这样就不会出现这种类型的问题—Python尝试使用该函数进行%d计算。重命名它my_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)

你需要改变一些值

直列

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.**

...
...

相关问题 更多 >