每次都不同随机

时间:2018-01-11 07:19:27

标签: python

我如何让它每次都选择一个不同的问题?

import random

name = input("What is your name? ")

def main():

    def feeling():
        response = input("How are you feeling right now {name}?".format(name=name))
        if response == "tired":
            tired = ['I wish I can make you feel better.','I hope school is not making you feel stressed.','You deserve the right to relax.']
            print(random.choice(tired))
        else:
            exit()

    def homesick():
        response = input("Do you miss your home? ")
        if response == "yes":
            yes=["Don't worry, you will be home soon......",'I am protecting your family and loved ones, trust me on this.',"Your kingdoms has been waiting for a long time, they'd forgiven your mistakes"]
            print(random.choice(yes))
        else:
            exit()

    def miss():
         response = input("Who do you miss?")
         if response == "my mom":
             print("Mom will be in town soon")
         else:
             exit()

    prompts = [feeling, homesick, miss]
    random.choice(prompts)()

    main()

main()

1 个答案:

答案 0 :(得分:2)

random.shuffle做生意,洗牌。

import random

name = input("What is your name? ")

def main():

    def feeling():
        response = input("How are you feeling right now {name}?".format(name=name))
        if response == "tired":
            tired = ['I wish I can make you feel better.','I hope school is not making you feel stressed.','You deserve the right to relax.']
            print(random.choice(tired))
        else:
            exit()

    def homesick():
        response = input("Do you miss your home? ")
        if response == "yes":
            yes=["Don't worry, you will be home soon......",'I am protecting your family and loved ones, trust me on this.',"Your kingdoms has been waiting for a long time, they'd forgiven your mistakes"]
            print(random.choice(yes))
        else:
            exit()

    def miss():
         response = input("Who do you miss?")
         if response == "my mom":
             print("Mom will be in town soon")
         else:
             exit()

    prompts = [feeling, homesick, miss]
    random.shuffle(prompts)
    for prompt in prompts:
        prompt()

    main()

main()

说明:

random.shuffle(提示)获取一个列表(提示)并将其像一手牌一样洗牌,因此它们是随机顺序。

然后我们使用for循环来查看提示中的每个元素:"提示中的提示:"

对于每个提示,请使用" prompt()"

运行提示