Python 使用随机模块从列表中随机选择并执行某些操作
我试图让程序从列表中随机打印一个项目,然后让程序要求用户在有真/假条件的地方输入一些信息,然后让用户返回到开始位置,在那里他们可以选择一个新的“轨迹” 我遇到的问题是,由于所有的帮助,只选择了“listone”,而不是随机选择列表Python 使用随机模块从列表中随机选择并执行某些操作,python,random,Python,Random,我试图让程序从列表中随机打印一个项目,然后让程序要求用户在有真/假条件的地方输入一些信息,然后让用户返回到开始位置,在那里他们可以选择一个新的“轨迹” 我遇到的问题是,由于所有的帮助,只选择了“listone”,而不是随机选择列表 import random questlist = ['questone', 'questtwo', 'questthree'] random.choice(questlist) def start(): print('Hi, welcome to the
import random
questlist = ['questone', 'questtwo', 'questthree']
random.choice(questlist)
def start():
print('Hi, welcome to the race track')
pick = input('pick a track')
displaytrack()
def displaytrack():
tracks = ('track 1', 'track 2', 'track 3')
print(tracks)
if 'questone':
ans = input('how old are u')
if ans == 3:
print('Correct')
print('Thanks for using')
start()
else:
print('Incorrect')
start()
if 'questtwo':
ans = input('How nice are u')
if ans == 'very':
print('Correcterino')
print('Thanks for using')
start()
else:
print('Wrongerino')
start()
if 'questthree':
ans = input('How tall are u')
if ans == 'pretty':
print('sixfoot')
print('Thanks for using')
start()
else:
print('no feet')
start()
您需要存储选项的输出,并在条件表达式中使用它:
choice = random.choice(questlist)
替换
if 'questone':
与
以此类推,否则,
if
表达式只是检查字符串'questone'
是否为None
,它的计算结果将始终为True
使用该行而不是当前的第三行
PC_Choice = random.choice(questlist)
然后使用:如果PC_Choice==questone
像这样:
if PC_Choice == 'questone':
ans = input('how old are u')
if ans == 3:
print('Correct')
print('Thanks for using')
start()
else:
print('Incorrect')
start()
然后也改变另外两个,这将使它给出一个随机选择
if PC_Choice == 'questone':
ans = input('how old are u')
if ans == 3:
print('Correct')
print('Thanks for using')
start()
else:
print('Incorrect')
start()