python - 如果答案在列表中正确或不正确,如何打印然后添加到分数
问题描述
编程新手,不确定如何打印用户对列表问题的回答是否正确,然后将其添加到他们正在进行的分数中,该分数将在程序结束时显示。
#number list test program
import random
import statistics
choosequestion = random.randint(1,4)
print('Welcome to the number list test')
print('e) Easy')
print('m) Medium')
print('h) Hard')
difficulty = input('Difficulty: ')
if difficulty == 'e':
print('Easy difficulty selected')
score = 0
questions = 2
quantity = 3
minimum = 1
maximum = 5
lists = random.sample(range(minimum, maximum), quantity)
if choosequestion == 1:
print ('What is the smallest number in this list?', lists)
finalmin = min = int(input(""))
elif choosequestion == 2:
print ('What is the biggest number in this list?', lists)
finalmax = max = int(input(""))
elif choosequestion == 3:
print ('What is the sum of numbers in this list?', lists)
finalsum = sum = int(input(""))
elif choosequestion == 4:
print ('What is the average of the numbers in this list?', lists)
average = statistics.mean = int(input(""))
##elif difficulty == 'm':
## print('Medium difficulty selected')
##
##elif difficulty == 'h':
## print ('Medium difficulty selected')
任何帮助都会很棒,谢谢(运行程序时选择“e”开始,我已经注释掉了所有其他选项)
解决方案
- 使用 for 循环重复提问。
- 当用户输入答案时,计算程序中的真实答案并比较结果,以得分。
你可以参考下面的代码。
#number list test program
import random
import statistics
choosequestion = random.randint(1,4)
print('Welcome to the number list test')
print('e) Easy')
print('m) Medium')
print('h) Hard')
difficulty = input('Difficulty: ')
if difficulty == 'e':
print('Easy difficulty selected')
score = 0
questions = 2
quantity = 3
minimum = 1
maximum = 5
for i in range(0,questions):
lists = random.sample(range(minimum, maximum), quantity)
if choosequestion == 1:
print ('What is the smallest number in this list?', lists)
if int(input(""))==min(lists):
score+=1
print("Correct answer")
else:
print("Wrong answer")
elif choosequestion == 2:
print ('What is the biggest number in this list?', lists)
if int(input(""))==max(lists):
score+=1
print("Correct answer")
else:
print("Wrong answer")
elif choosequestion == 3:
print ('What is the sum of numbers in this list?', lists)
if int(input(""))==sum(lists):
score+=1
print("Correct answer")
else:
print("Wrong answer")
elif choosequestion == 4:
print ('What is the average of the numbers in this list?', lists)
if int(input(""))==sum(lists)/len(lists):
score+=1
print("Correct answer")
else:
print("Wrong answer")
print("Your final score is : "+str(score))
推荐阅读
- html - 尝试在移动设备上滚动时,Div 只是向上拖动?
- c++ - 我无法从文件中正确加载文本
- swift - 防止要求使用麦克风
- vb.net - 尝试在 Firefox 中检索和解密登录时出错
- c# - C# 使用 HttpContext.Current.Request 在移动浏览器上请求时无法获取 QueryString e
- windows - 桌面快捷方式创建者 - 可通过电子邮件分发
- python - Python日期未在sql server中显示时间
- android - Android:打字时保持TextInputEditText在视图中
- python - 熊猫绘图——忽略时间范围
- javascript - 查询性能慢:使用 $lookup 和 $and/$or 的 MongoDB