【问题标题】:How to "Select" a correct user answer based on a random q.uestion generated如何根据生成的随机问题“选择”正确的用户答案
【发布时间】:2018-05-12 10:24:46
【问题描述】:
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 写下来?

【问题讨论】:

  • 您忘记添加部分,您实际上尝试使用 python 来获取解决方案!

标签: python


【解决方案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!")

希望这会有所帮助!有任何问题都可以问!

【讨论】:

  • 谢谢!您编写的代码进行了一些小改动,已经成功了,谢谢!
  • 糟糕,我似乎遇到了问题
  • 代码确实有效,但是当我输入答案时,例如如果它要求在 78 和 10 之间更高的值,我输入 78,但没有其他内容打印,只是数字
  • 顺便说一句,在您说这是正确答案之前有点早,请彻底检查!只有在你得到你想要的东西后才勾选它!无论如何,祝你好运!
猜你喜欢
  • 2015-11-02
  • 2022-11-23
  • 2021-05-05
  • 1970-01-01
  • 1970-01-01
  • 2012-08-11
  • 2020-10-13
  • 2017-12-13
  • 1970-01-01
相关资源
最近更新 更多