首页 > 解决方案 > 我的 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 为基数时,颜色存在错误。

标签: python

解决方案


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!")

推荐阅读