如何根据随机产生的问题“选择”正确的用户答案

2022-10-02 00:49:28 发布

您现在位置:Python中文网/ 问答频道 /正文

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)

我被困在这一点上,我试图找出一种方法,如果代码选择大/小 ,代码将识别生成的较大/较小的数字,因此要求用户根据问题输入答案,但是如何识别num1或num2是较大还是较小,从而编写一个条件来授予用户正确回答的分数

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

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

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


Tags: theinputifvaluerandombetweenfindquestionpleaserandinthighernum2num1useranswerquestiondecide
1条回答
网友
1楼 ·
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!")

希望这有帮助!有什么问题要问吗