Python 解析值为';t在显示时显示正确的值
每次用户的答案正确时,我都会尝试将正确答案计数增加1。但是,当解析为display_result()函数时,正确的函数将显示“0 correct” 无论我如何努力,我都无法让它正常工作,所以非常感谢您的帮助Python 解析值为';t在显示时显示正确的值,python,python-3.x,Python,Python 3.x,每次用户的答案正确时,我都会尝试将正确答案计数增加1。但是,当解析为display_result()函数时,正确的函数将显示“0 correct” 无论我如何努力,我都无法让它正常工作,所以非常感谢您的帮助 code removed for academic integrity 如果用户正确回答了3个问题中的1个,我希望答案是“您正确回答了3个问题中的1个。” 当前,它将显示您在菜单_option()中正确回答了3个问题中的0个。您从未修改计数,因此它将保持为0。两个简单的修复方法。更改为:
code removed for academic integrity
如果用户正确回答了3个问题中的1个,我希望答案是“您正确回答了3个问题中的1个。”
当前,它将显示您在菜单_option()
中正确回答了3个问题中的0个。您从未修改计数,因此它将保持为0。两个简单的修复方法。更改为:
count = check_solution(user_solution, real_solution, count)
return count
或者只是
return check_solution(user_solution, real_solution, count)
我注意到的另一件事是:在get\u user\u input()
中,需要返回递归调用的结果:
else:
print("Invalid input, please try again")
return get_user_input()
有许多问题:
- 你正在做
correct=menu选项(选项,correct)
而你应该像correct+=
- 在
菜单\u选项中
您从未分配给计数
,我认为它应该是计数=检查解决方案(…)
- 对于
索引==5
,您不应该执行返回选项
,因为这将添加到正确的
最后,代码按照我的预期运行(需要python3.6+):
!/usr/bin/env python3
随机输入
def get_user_input():
尽管如此:
尝试:
index=int(输入(“输入您的选择:”)
如果0
您的第一个要点不正确,因为检查解决方案()
更新计数器:count=count+1
@johnnymapp是的,但我没有看到它在外部定义,加上这是一种不好的做法。我没有正确返回count,但不幸的是这是学校的挑战,所以我不允许更改函数名、参数变量或main()中的任何内容太棒了!它对每个索引输入调用了两次check_solution()函数,做了一些小的调整,但现在起作用了:D。
#!/usr/bin/env python3
import random
def get_user_input():
while True:
try:
index = int(input("Enter your choice: "))
if 0 < index < 6:
return index
except ValueError:
print("Invalid input, should be Integer.\n")
else:
print("Invalid input, please try again")
def get_user_solution(problem):
while True:
print("Enter your answer")
user_solution = input(f"{problem} = ")
try:
return float(user_solution)
except ValueError:
print("Invalid input, should be float\n")
def check_solution(user_solution, solution, count):
if user_solution == solution:
print("Correct.")
return count + 1
else:
print("Incorrect.")
return count
def menu_option(index, count):
first_num = random.randrange(1, 21)
second_num = random.randrange(1, 21)
if index == 1:
problem = f"{first_num} + {second_num}"
real_solution = first_num + second_num
print(real_solution)
user_solution = get_user_solution(problem)
return check_solution(user_solution, real_solution, count)
if index == 2:
problem = f"{first_num} - {second_num}"
real_solution = first_num - second_num
print(real_solution)
user_solution = get_user_solution(problem)
return check_solution(user_solution, real_solution, count)
if index == 3:
# blah blah blah, repeated code but removed for neatness
pass
if index == 5:
option = 5
return option
def display_result(total, correct):
if total == 0:
print("You answered 0 questions with 0 correct")
print("Your score is 0.0%")
else:
percentage = round(correct / total * 100, 2)
print(
f'You answered {total} questions with {correct} correct.\n'
f'Your score is {percentage}%'
)
def display_intro():
pass
def display_menu():
pass
def display_separator():
print('-'*20)
def main():
display_intro()
display_menu()
display_separator()
option = get_user_input()
total = 0
correct = 0
while option != 5:
total = total + 1
correct = menu_option(option, correct)
option = get_user_input()
print("Exit the quiz.")
display_separator()
display_result(total, correct)
if __name__ == "__main__":
main()