PYTHON需要帮助简化 - 从生成预先设定的名单随机物品

问题描述 投票:1回答:1

我知道有一个更简单的方法来做到这一点。它被认为是一个“人妖”之类的游戏,你产生从列表中5点随机的颜色和用户输入的颜色。每次打开它应该保持第一颜色相同,并添加一个新的随机颜色,这是我的麻烦进来,任何帮助将不胜感激!

import random
# Create a list of colors.
colors = ['Yellow', 'Green', 'Red', 'Blue']

# Generate random colors from the list. 
color1 = random.choice(colors)
color2 = random.choice(colors)
color3 = random.choice(colors)
color4 = random.choice(colors)
color5 = random.choice(colors)


# Simon displays the 5 color choices, adding 1 each turn. 

print "Simon says: ", color1
raw_input ("What did Simon say? ")

print "Simon says: ", color1, color2
raw_input ("What did Simon say? ")

print "Simon says: ", color1, color2, color3
raw_input ("What did Simon say? ")

print "Simon says: ", color1, color2, color3, color4
raw_input ("What did Simon say? ")

print "Simon says: ", color1, color2, color3, color4, color5
raw_input ("What did Simon say? ")

我第一次尝试这个样子,但没有工作之一:

# Create a list of colors.
colors = ['Yellow', 'Green', 'Red', 'Blue']


# Display the color list, adding one random color each turn. Prompt the
# user to enter the colors after each is added.
for i in range (5):
    import random
    color = random.choice(colors)

    print "Simon says: " ,color

    raw_input ("What did Simon say? ")
    colors.append(colors)
    print "Simon says: " , (color)
python python-2.x
1个回答
0
投票

你几乎没有。你的问题是,试图修改原来的4色列表。只是初始化为游戏一个新的空列表:

# Create a list of colors.
colors = ['Yellow', 'Green', 'Red', 'Blue']

# Display the color list, adding one random color each turn. Prompt the
# user to enter the colors after each is added.
chosen_colors = []
for i in range (5):
    import random
    color = random.choice(colors)
    chosen_colors.append(color)
    print "Simon says: " ,chosen_colors
    raw_input ("What did Simon say? ")

现在,你应该找到一个方法来检查,如果答案正确与否。

© www.soinside.com 2019 - 2024. All rights reserved.