python中给定列表中的随机字符串值生成器

时间:2013-09-13 14:55:09

标签: python python-2.7

sports=['CRICKET','BADMINTON','TENNIS']
bollywood=['WAQT','GULAAL','MAQBOOL']
food=['RAVA DOSA','IDLI SAMBHAR','CENTURY EGG']
print """Choose Your Field
1. SPORTS
2. BOLLYWOOD
3. FOOD"""
field=raw_input('\n Enter Field :')

现在用户将进入该字段,然后应该有一些函数将从该给定字段(列表)返回随机值。

1 个答案:

答案 0 :(得分:1)

import random
myList = []
myList.append(['CRICKET','BADMINTON','TENNIS'])
myList.append(['WAQT','GULAAL','MAQBOOL'])
myList.append(['RAVA DOSA','IDLI SAMBHAR','CENTURY EGG'])

print """Choose Your Field
1. SPORTS
2. BOLLYWOOD
3. FOOD"""
field=raw_input('\n Enter Field :')

print random.choice(myList[int(field) - 1])

您还应该对输入进行错误检查,以确保它确实是一个有效的条目