如果列表中的答案正确或不正确,如何打印,然后添加到分数中

2024-10-03 02:32:43 发布

您现在位置: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”开始,我已经注释掉了所有其他选项)


Tags: theinnumberinputisrandomthiswhat
2条回答
  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))

python中的input()函数返回用户在控制台中键入的字符串。然后,您可以使用相等运算符==将输入字符串与正确答案进行比较。(当然,如果匹配,则为True)我已经完成了几行代码来演示:

      #Ask the User the Question
      print ('What is the smallest number in this list?', lists)

      #Get the User's response
      userAnswer = int(input(""))
      
      #Compare the response with the right answer
      if(userAnswer == min(lists)):
        #User was right
        print("Correct")
        score += 1
      else:
        #User was wrong
        print("Wrong")

将min()函数更改为max()、sum()或您自己的函数,以获得每个问题的正确答案

对于未来,许多事情可以帮助改进此代码:

  1. 对代码进行注释,给出代码的主要功能。我不是说每行一条注释,只是代码部分的摘要
  2. 使用任何语言的用户输入时,请确保对其进行压力测试。当用户输入字符串时,int(input(“”)使程序崩溃
  3. 利用函数、循环和变量。我见过程序员们因为过度使用if-else语句而陷入困境。不要避免必要的if-else语句,而是在答案和代码中使用模式

相关问题 更多 >