我的程序:一个数学测验,提示用户选择难度(初级,中级,高级),然后根据他们的选择生成五个问题(随机#' s)。
在我开始添加评论和文档字符串之前,它工作得很好(如果难以阅读,我很抱歉,教师需要过多的评论。)
我提前为代码的混乱而道歉(导师想要过多的评论)。
# 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.")
我收到此类型错误(在第一个print语句中):
当我添加评论时,我不确定是否搞砸了什么,但我似乎无法弄清楚它是什么。
答案 0 :(得分: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)
答案 1 :(得分:0)
您使用名为sum
的变量,但不定义它。 Python有一个名为sum
的内置函数 - 你真的应该改变你的变量名,这样你就不会遇到这类问题 - 而python尝试使用该函数进行%d
计算。重命名它my_sum
,你会得到一个不同的错误,说它没有定义。然后你可以解决这个问题!
答案 2 :(得分: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.**
...
...