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)
我坚持这一点,我试图找出一种方法,如果代码选择 Bigger/smaller ,代码将识别生成的更大/更小的数字,因此要求用户根据问题输入答案,但是我如何确定 num1 或 num2 是更大还是更小,从而编写一个条件来奖励用户正确回答的分数?
基本上想要这个:(可能的算法)
if set of numbers generated is higher
require user to input bigger number
userMarks = userMarks + 1
依此类推,如果询问较小的数字,我该如何用 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!")
希望这可以帮助!有什么问题可以问!
推荐阅读
- keras - 使用 Talos 和 flow_from_directory 对图像进行超参数优化
- python - 满足要求时是否可以重用线程
- python - MySQL 数据使用谷歌云 SQL 和 SQLAlchemy 进行缓存
- java - 我无法在 Java-Android 中使用 FTPClient 连接到我的 FTP 服务器的主机
- spring - 删除 logback 依赖后,Spring 输出控制台消失了
- python - 在 python 中使用 Selenium 的 Ajax 请求
- android-x86 - 如何在 VMware Fusion 中运行的 Android-x86 VM 上设置自定义分辨率
- java - 将 Json 反序列化为 java 对象
- javascript - 不调用 express.js 中的自定义错误处理程序
- node.js - $filter 所有没有“类别”的邮件