首页 > 解决方案 > 如何根据生成的随机问题“选择”正确的用户答案

问题描述

num1 = random.randint(1,20)
num2 = random.randint(1,20)
question = ["Bigger" , "Smaller"]
questiondecide = random.choice(question))
if questiondecide == "Higher":
userAnswer=input("Please find the Higher value between" + num1 "and" + 
num2)
elif questiondecide == "Lower":
userAnswer=input("Please find the Lower value between" + num1 "and" + 
num2)

我坚持这一点,我试图找出一种方法,如果代码选择 Bigger/smaller ,代码将识别生成的更大/更小的数字,因此要求用户根据问题输入答案,但是我如何确定 num1 或 num2 是更大还是更小,从而编写一个条件来奖励用户正确回答的分数?

基本上想要这个:(可能的算法)

if set of numbers generated is higher
require user to input bigger number
userMarks = userMarks + 1

依此类推,如果询问较小的数字,我该如何用 Python 写下来?

标签: python

解决方案


import random
from random import randint

num1 = random.randint(1,20)
num2 = random.randint(1,20)

question = ["Bigger" , "Smaller"]
questiondecide = random.choice(question)


biggest = max(num1, num2)
smallest = min(num1, num2)

if questiondecide == "Bigger":
    userAnswer = input("Please find the Higher value between " + str(num1) + " and " + str(num2) + ":" + "\n")
print(biggest)
if userAnswer == biggest:
    print("This is the biggest number")
    print("and you have chosen the bigger number!")
elif userAnswer == smallest:
    print("This is the smallest number")
    print("but you have not chosen the bigger number!")

elif questiondecide == "Smaller":
   userAnswer = input("Please find the Lower value between " + str(num1) + " and " + str(num2) + ":" + "\n")
if userAnswer == biggest:
    print("This is the biggest number")
    print("but you have not chosen the smaller number!")
elif userAnswer == smallest:
    print("This is the smallest number")
    print("and you have chosen the smaller number!")

希望这可以帮助!有什么问题可以问!


推荐阅读