首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >随机/循环挑战

随机/循环挑战
EN

Stack Overflow用户
提问于 2016-05-03 01:16:37
回答 1查看 237关注 0票数 1

提前谢谢你的回答。所以,我才刚开始学习Python,现在面临着一个困扰我的挑战。这是一项挑战:

目的是编写一个模拟幸运饼干的程序。程序应该在每次运行时随机显示五种独特的命运之一。 我的解决方案:

代码语言:javascript
复制
# Program simulates fortune cookie.
# Displays one of five unique fortunes at random

"""
Fortunes:
* You will meet someone that will change your life today.
* Invest in lottery this week because you will win big.
* You will get a call from someone with great influence this week.
* Purchase chinese food as you will read a fortune that will come to pass.
* Good news about an inheritance will come shortly.
"""

# Steps:

# Display a Welcome message explaining what the Fortune cookie program is  about, and how users can use it.
# Import random module to randomize the messages.
# Employ loop to repeat.

#Welcome

print("\t\t\n<<<Welcome to Your Fortune Cookie.>>>")
print("\t*To see what the Fortune Cookie Ginie has in store for you.")
print("\t*Ok, here we go...\n")


print(input("Press Enter to Reveal your Fortune: "))

#random module
import random
fortune1 = random.randint(1, 6)

#loop body

fortune1 < 1
while True:
print("You will meet someone that will change your life today.")
print(input(">>Press Enter again to Reveal another Fortune: "))
if fortune1 == 2:
    print("Fortune: Invest in lottery this week because you will win big.")
    print(input(">>Press Enter again to Reveal another Fortune: "))
elif fortune1 == 3:
    print("Fortune: You will get a call from someone of great influence    this week.")
    print(input(">>Press Enter again to Reveal another Fortune: "))
elif fortune1 == 4:
    print("Fortune: Purchase chinese food as you will read a fortune that will come to pass.")
    print(input(">>Press Enter again to Reveal another Fortune: "))
elif fortune1 == 4:
    print("Fortune: Good news! An inheritance will come shortly.")
    print(input(">>Press Enter again to Reveal another Fortune: "))
elif fortune1 == 5:
    print("Fortune: Aha! You will win something this weekend")
    print(input(">>Press Enter again to Reveal another Fortune: "))
else:
    print("Let's check again...")
fortune1 += 1
print("That's all your fortune")

虽然我想以不同的方式运行它,但是程序有点运行。我想我的问题是:我还有别的方法可以做到吗?再次感谢你的答复。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-03 01:27:49

另一种方法是有一个输出列表,然后用random.choice(list)从列表中随机选择一个输出。示例:

代码语言:javascript
复制
import random

fortunes = ['fortune1', 'fortune2', 'fortune3']

while True:
    input("Press enter to receive a fortune. ")
    print(random.choice(fortunes))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36994089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档