我需要用户输入来告诉程序使用哪个列表?

时间:2017-07-08 04:44:35

标签: python

所以我对python很新,我正在制作一个刽子手程序。这是我到目前为止的代码。

import random
print('Welcome to hangman')
print('Type one of the following catagories')
Animal=['Cat','Dog','Bird','Cow','Fish','Lizard']
Clothing=['Shirt','Jeans','Sweatshirt','Shoes','Hat','Scarf']
Weather=['Rain','Snow','Sunny','Sleet','Windy','Storms']
Colors=['Red','Blue','Green','Purple','Yellow','Grey']
print('Animal, Clothing, Weather, Colors')

catagory=input()
randomwordindex=random.randint(1,7)

它说category=input()我希望用户输入是我上面列出的列表之一。然后我可以使用category随机整数来选择一个随机单词来开始游戏。如何将input()作为列表而不是字符串值?

编辑:好的,我感谢你们。

这是我目前的代码。我最终可能会添加一个for循环并清理我的while循环,所以它只有一行。

import random

print('Welcome to hangman')
print('Type one of the following catagories')

Animal=['Cat','Dog','Bird','Cow','Fish','Lizard']
Clothing=['Shirt','Jeans','Sweatshirt','Shoes','Hat','Scarf']
Weather=['Rain','Snow','Sunny','Sleet','Windy','Stormy']
Colors=['Red','Blue','Green','Purple','Yellow','Grey']

print('Type 1 for Animal, 2 for Clothing, 3 for Weather, 4 for Colors')

catagory=int(input())

while catagory > 4:
    print('Your input isn\'t one of the catagories. Make sure your choice is a number from 1 to 4.')
    print('Try entering again')
    print('Type 1 for Animal, 2 for Clothing, 3 for Weather, 4 for Colors')
    catagory=int(input())

while catagory == 0: 
    print('Your input isn\'t one of the catagories. Make sure your choice is a number from 1 to 4.')
    print('Try entering again')
    print('Type 1 for Animal, 2 for Clothing, 3 for Weather, 4 for Colors')
    catagory=int(input())

if catagory == 1: secretword=random.choice(Animal)
if catagory == 2: secretword=random.choice(Clothing)
if catagory == 3: secretword=random.choice(Weather)
if catagory == 4: secretword=random.choice(Colors)

现在变量secretword存储从列表中随机选择的单词。

4 个答案:

答案 0 :(得分:3)

您可以使用Python字典,也可以添加选项并从用户处获取选项编号。

import random
print('Welcome to hangman')
print('Type one of the following catagories')

Animal=['Cat','Dog','Bird','Cow','Fish','Lizard']
Clothing=['Shirt','Jeans','Sweatshirt','Shoes','Hat','Scarf']
Weather=['Rain','Snow','Sunny','Sleet','Windy','Storms']
Colors=['Red','Blue','Green','Purple','Yellow','Grey']

print('1.Press 1 for Animal, 2.Press 2 for Clothing')
print('3.Press 3 for Weather, 4.Press 4 for Colors')

catagory=int(input())

# you can iterrate this
if catagory == 1: random.choice(Animal)
elif catagory == 2: random.choice(Clothing)
elif catagory == 3: random.choice(Weather)
elif catagory == 4: random.choice(Colors)

randomwordindex=random.randint(1,7)

答案 1 :(得分:0)

您可以使用字典将列表映射到特定值。这样,你可以访问一个类别,编程:

>>> categories = {
    'animal': ['Cat','Dog','Bird','Cow','Fish','Lizard'],
    'clothing': ['Shirt','Jeans','Sweatshirt','Shoes','Hat','Scarf'],
    'weather': ['Rain','Snow','Sunny','Sleet','Windy','Storms'],
    'colors': ['Red','Blue','Green','Purple','Yellow','Grey'],
    }
>>> 
>>> catagory = input()
animal
>>> categories[catagory]
['Cat', 'Dog', 'Bird', 'Cow', 'Fish', 'Lizard']
>>> 

此外,您应该使用random.choice()代替从列表中获取随机元素:

>>> from random import choice
>>> choice(categories[catagory])
'Fish'
>>> 

答案 2 :(得分:0)

使用while块验证响应。

valid_options = ['Animal', 'Clothing' , 'Weather', 'Colors' ]

while True:
    opt = input('Enter: ') 
    if opt in valid_options:
        break
    else:
        print('Not a valid option. Valid options are: %s', % ",".join(valid_options))

在此代码之后,您可以使用基督教代码来映射类别的值。

答案 3 :(得分:0)

您可以检查给定的输入字符串是否为有效选项,否则显示有效选项并等待用户输入正确选项之一或退出。

您也可以通过字典显示而不是硬编码来显示密钥。

import sys
import random

categories = {
    'animal': ['Cat','Dog','Bird','Cow','Fish','Lizard'],
    'clothing': ['Shirt','Jeans','Sweatshirt','Shoes','Hat','Scarf'],
    'weather': ['Rain','Snow','Sunny','Sleet','Windy','Storms'],
    'colors': ['Red','Blue','Green','Purple','Yellow','Grey'],
    'exit' : []
    }
keys = ""

print('Welcome to hangman')
print('Type one of the following categories')
for key, value in categories.items():
    keys = keys + key + " "
print(keys)
category = input()
while (True):
    if (category.isalpha() and (category.lower() in categories.keys())):
        if (category.lower() == 'exit'):
            print("Thanks for playing hangman!")
            sys.exit()
        print(categories[category.lower()])
        randomwordindex=random.randint(1,7)
        print(randomwordindex)
        ## TODO : play hangman, for now break to avoid infinite loop
        break
    else:
        print("valid options are: " + keys)
        category = input()

示例运行

Welcome to hangman
Type one of the following categories
animal clothing weather colors exit 
anim
valid options are: animal clothing weather colors exit 
clo
valid options are: animal clothing weather colors exit 
1231
valid options are: animal clothing weather colors exit 
acsa12
valid options are: animal clothing weather colors exit 
animal
['Cat', 'Dog', 'Bird', 'Cow', 'Fish', 'Lizard']
7
>>> 

=============== RESTART ~/hangman.py ===============
Welcome to hangman
Type one of the following categories
animal clothing weather colors exit 
asn
valid options are: animal clothing weather colors exit 
1231
valid options are: animal clothing weather colors exit 
sadxasdn
valid options are: animal clothing weather colors exit 
exit
Thanks for playing hangman!
>>>