【问题标题】:How to print a random string from a list, when given an input? (Python)给定输入时,如何从列表中打印随机字符串? (Python)
【发布时间】:2022-09-25 09:10:43
【问题描述】:

我对编码很陌生,所以如果答案很简单,我很抱歉,但我目前正在尝试编写一个魔术 8 球游戏。我希望计算机在用户提供输入时从我制作的列表中提供一个随机字符串给用户。这是我到目前为止所做的代码:

import random
print(\"Welcome to the Magic 8 Ball Game!\")

#Create phrases the maigc 8 ball will say to the user
phrases = [\"Ask again\", \"Reply hazy, try again.\", \"I do see that in your near future...\", \"My sources say no\", \"Very possible\", \"Yes. Very soon.\"]
#Ask the user to ask a question to start the game
answer = input(\"The game has started. Ask a question.\\n\")
#Make a loop


for i in answer:

     print(random.choice(phrases))

当我运行代码时,它不会给用户一个字符串,而是随机给出多个字符串。我想我可能没有正确使用 for 循环......

    标签: python


    【解决方案1】:

    它应该是一个无限循环,它会在某个条件下中断,例如当用户键入 none 或某些内容时

    【讨论】:

      猜你喜欢
      • 2011-07-19
      • 2017-02-08
      • 2017-04-02
      • 2016-03-21
      • 2016-04-11
      • 1970-01-01
      • 2014-04-25
      • 2023-03-17
      • 2021-04-11
      相关资源
      最近更新 更多