如何将条件附加到随机选择中?

时间:2017-12-08 12:41:05

标签: python-3.x if-statement random

如果孩子在顽皮的名单上,我希望选择5,但是选择6将被列为好名单。我的问题是分配给列表是随机选择的元组值。

import random, time # allowing use of random and delays
choice=[
'eddie',
'dan',
'erin',
'josh',
]

choices2=[
'naughty list',
'nice list'
]

choices3=[
'merry xmas anyhoooo!',
'cannot win them all!',
'have fun dude!',
'man\'s not got?',
]

choices4=[
'must have been when you pushed the granny over',
'could have been that cookie you stole?!',
'probably because you were cheeky to a teacher'
]

choices5=[
'must be because you are an angel',
'must be because you keep your room tidy',
'Santa just never saw you'
]
while True:
    input('\npress enter to check who is on the list')
    print('checking...')
    time.sleep(2)
    print(random.choice(choice))
    time.sleep(2)

    print(random.choice(choices2))
    if random.choice =='naughty list':
        print(random.choice(choices4))
    elif choices2=='nice list':
        print(random.choice(choices5))
    else:
        print('wrong')
    time.sleep(2)
    print(random.choice(choices3))

1 个答案:

答案 0 :(得分:0)

更新的代码:

 import random, time # allowing use of random and delays
choice1=[
'eddie',
'dan',
'erin',
'josh',
]

choices2=[
'naughty list',
'nice list'
]

choices3=[
'merry xmas anyhoooo!',
'cannot win them all!',
'have fun dude!',
'man\'s not got?',
]

choices4=[
'must have been when you pushed the granny over',
'could have been that cookie you stole?!',
'probably because you were cheeky to a teacher'
]

choices5=[
'must be because you are an angel',
'must be because you keep your room tidy',
'Santa just never saw you'
]
while True:
    input('\npress enter to check who is on the list')
    print('checking...')
    time.sleep(2)
    print(random.choice(choice1))
    time.sleep(2)

    list1 = random.choice(choices2)
    print(list1)
    if list1 =='naughty list':
        print(random.choice(choices4))
    elif list1=='nice list':
        print(random.choice(choices5))
    else:
        print('wrong')
    time.sleep(2)
    print(random.choice(choices3))

此外,选择是随机函数在内部使用,因此将列表名称更改为choice1