python - 当你猜对时,数字猜测器打印错误
问题描述
所以我正在做一个数字猜测器,当我做对时,它会打印出“错误”的声明,有人可以帮我吗?
#randomly select number
import random
#Selects number
numbers = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50
#tell the user to pick a number
guess = input("I am think of a number between 1 and 50, Guess what number ")
num = random.choice(numbers) #selecting the number
#prints if you got it right
if num == guess:
print("Yes you go it! You rock.")
else:
print("WRONG! I was thinking of", num, "Try again! Better luck next time.")
解决方案
你应该做两件事
- 转换
guess
为整数guess = int(input("..."))
或if num == int(guess)
. (这将解决您遇到的问题)。 - 与其制作数字列表并随机选择,不如使用
random.randint()
.num = random.randint(1,50)
. 然后您可以删除该numbers
行
推荐阅读
- python - 滚动时间窗口特征——与 Pandas 的数据争论
- java - Spring Boot 服务中的 Fluent 接口
- php - 根据其他数组的值对数组进行排序
- javascript - 为 !purge 命令添加权限
- configuration - 在 Marklogic 中将数据库和应用服务器配置从 ML 8 导入到 ML10
- spring - 未调用基本控制器并在 Spring MVC 中出现 404 错误
- java - 找到2个给定整数中位数之差的递归方法
- python - 为什么我的 Python 代码没有正确评估布尔值?
- sql - 更新查询在存储过程中运行缓慢
- python - 在 Flask 模板中显示来自 MySQL 的图像