2017-07-08 56 views
0

所以我很新的python和我正在做一个hang子手程序。这是我迄今为止的代码。我需要用户输入来告诉程序使用哪个列表?

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存储从列表中随机选择的单词。

+0

如果你的目标是让用户输入密码,在这种情况下的列表,你可以使用'的eval()'函数。然而,这是非常危险的,不应该被其他人使用的代码使用。使用eval基本上是将一个字符串转换为真正的代码。你不能用一个数字打印清单,比如''Option 1 Animal'''?它似乎更加用户友好和安全。 – Mikael

+0

@Mikael我和''Option 1. Animal“'有什么区别?你能否详细说明一下? – Keizzerweiss

回答

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) 
+0

因此,当'catagory = int(input())'的代码时,代码根据输入中究竟是什么来分配一个整数值?用户只需键入与类别匹配的数字即可?我想我要问的是int(input())发现是什么整数? '.Press'代码的作用是什么? – Keizzerweiss

+0

好吧,我玩了一下,弄清楚了我在第一条评论中的要求。只是让人们知道他们将来是否阅读过这些内容。我很困惑,因为我认为用户仍然在输入动物,但用户输入的数字是上面的代码。 '.Press'没有任何意义,它只是@Shaon shaonty决定编写字符串的方式。 '.Press'不会将任何内容分配给数字'1'。 – Keizzerweiss

0

您可以使用字典将您的列表映射到特定值。这样,您可以访问类别,programaticlly:

>>> 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' 
>>> 
+0

好的,我一直在阅读字典,我认为这是最干净的方式。在这一点上,我并不熟悉很多进口产品和它们的功能。 – Keizzerweiss

+0

我会玩一下 – Keizzerweiss

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

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

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! 
>>> 
相关问题