Python 每次都是不同的随机数

Python 每次都是不同的随机数,python,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":

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

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

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()
说明:

随机。洗牌(提示)接受一个列表(提示)并像一手牌一样洗牌,所以它们是随机顺序的

然后,我们使用for循环查看提示中的每个元素:“提示中的for prompt:”


对于每个提示,使用“prompt()”

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()
说明:

随机。洗牌(提示)接受一个列表(提示)并像一手牌一样洗牌,所以它们是随机顺序的

然后,我们使用for循环查看提示中的每个元素:“提示中的for prompt:”


对于每个提示,使用“prompt()”

运行提示,您的意思是如何以随机顺序询问所有3个问题?然后使用循环检查这篇文章。它应该会帮助你:请不要破坏你的帖子。通过在Stack Exchange网络上发布,您已授予Stack Exchange在下不可撤销的权利,以分发该内容(即,无论您未来的选择如何)。根据堆栈交换策略,帖子的非故意破坏版本是发布的版本,因此,任何故意破坏都将恢复。如果你想了解更多关于删除帖子的信息,请参阅:你是说你是怎么问所有3个问题的,但都是随机顺序的吗?然后使用循环检查这篇文章。它应该会帮助你:请不要破坏你的帖子。通过在Stack Exchange网络上发布,您已授予Stack Exchange在下不可撤销的权利,以分发该内容(即,无论您未来的选择如何)。根据堆栈交换策略,帖子的非故意破坏版本是发布的版本,因此,任何故意破坏都将恢复。如果您想了解有关删除帖子的更多信息,请参阅:当然!现在编辑帖子。如果你有任何其他问题,请告诉我。当然!现在编辑帖子。如果你有任何其他问题,请告诉我。