简体   繁体   English

如何让 random.choice 在 Python 中打印相同的结果?

[英]How do I make the random.choice print the same result in Python?

First off I want to say thank you for taking the time to help others.首先我想说谢谢你花时间帮助别人。

I need help getting the random.choice to print the same result from the list.我需要帮助获取 random.choice 以从列表中打印相同的结果。 I am now working on a text adventure game where players can make and deliver pizzas to the customer.我现在正在开发一款文字冒险游戏,玩家可以在其中制作披萨并将其运送给顾客。

Here is part of my code where the boss in the game ask me to make a pizza for a customer:这是我的代码的一部分,游戏中的老板让我为顾客做披萨:

def making_pizza(self):
    while True:
        print("Joe:" + mPlayer.name + " we have a customer by the name of  " + self.name)
        time.sleep(a)
        print("Joe: He would like a " + str(self.pizza_want))
        time.sleep(a)
        print("The address is " + self.address)
        time.sleep(a)
        accept = input('Do you accept? (Yes/No):')
        if accept == 'Yes' or accept == 'yes':
            pizza_menu()
        elif accept == 'No' or accept == 'no':
            print("Joe: Fine I'll find someone else for the job.")

Heres the method call:下面是方法调用:

random.choice(customer_list).making_pizza()

Now after the player finish making the pizza he has to deliver the pizza to the correct address:现在玩家完成披萨制作后,他必须将披萨送到正确的地址:

def delivering(self, a, b, c, d,):
        choice_input = input(f"""

                        1 - {a}
                        2 - {b}
                        3 - {c}
                        4 - {d}
                        5 - {self.address}

            **** Choose the customer house to deliver the pizza ****

But when I use random.choice(customer_list) as an argument to call the delivering method it shows another random self.address from the customer list.但是,当我使用 random.choice(customer_list) 作为参数来调用交付方法时,它会显示来自客户列表的另一个随机 self.address。 How do I get the random.choice to show the correct address from the first method?如何让 random.choice 从第一种方法中显示正确的地址? which is the making_pizza method.这是making_pizza 方法。

Thanks!谢谢!

You should 'store' your choice as a variable:您应该将您的选择“存储”为变量:

choice = random.choice(customer_list).making_pizza()
print(choice)

because otherwise it prints another random choice and now it stores it in a variable so you can re-use that variable to print it.因为否则它会打印另一个随机选择,现在它将它存储在一个变量中,这样您就可以重新使用该变量来打印它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM