首页 > 解决方案 > 如果答案在列表中正确或不正确,如何打印然后添加到分数

问题描述

编程新手,不确定如何打印用户对列表问题的回答是否正确,然后将其添加到他们正在进行的分数中,该分数将在程序结束时显示。

#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”开始,我已经注释掉了所有其他选项)

标签: python

解决方案


  1. 使用 for 循环重复提问。
  2. 当用户输入答案时,计算程序中的真实答案并比较结果,以得分。

你可以参考下面的代码。

#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))

推荐阅读