python - 我的 base 10 或一般代码有什么问题
问题描述
我正在尝试制作一个简单的彩色游戏,但无法正常工作。
import random
color = ["Red", "Blue", "Green", "Yellow"]
name = input('Hi,Whats your name?')
print ("Well",name, "I am thinking of a color, can you guess it?")
guess1 = int(input()) # convert input from string to integer
while guess1 != color:
if guess1 > color:
print ('Wrong color.')
guess1 = int(input()) # asks user to take another guess
print("Good job, you got it!")
在以 10 为基数时,颜色存在错误。
解决方案
import random
# All colors we are checking
colors = ["Red", "Blue", "Green", "Yellow"]
# Select one random color
target_color = random.choice(colors)
name = input('Hi,Whats your name?')
print ("Well", name, "I am thinking of a color, can you guess it?")
# Get input as a string
guess = input()
# While we are wrong
while guess != target_color:
print ('Wrong color.')
# We try to check another color
guess = input()
print("Good job, you got it!")
推荐阅读
- windows - 获取 Windows 程序的内存地址范围
- node.js - AJAX 调用中断,NodeJS,Express,Handlebars
- html - Changing the function of a button based on if a user is logged in
- mysql - mysql - 根据表中列值最高的 id 连接表行 - 多个连接和条件
- python - 我正在尝试制作一个等待用户消息的不和谐机器人,如果提到三个标签,它会给出滴答反应
- flutter - Flutter 中的 Overlay 冻结了应用程序
- php - laravel api 门检查
- asp.net - 从 ViewModel 和字符串类型的 Image 属性将 Image 动态绑定到 Crystal Report
- python-3.x - 任务已销毁,但每个请求都处于待处理状态
- vue.js - vue中使用dropzone.js,调用带有图片文件名的函数