赞
踩
题目:如下图所示
代码展示:
"""
Author:mll
Content:春节集五福
Date:2020-01-17
"""
import random
# 定义一个列表存储五福
five_blessings = ['爱国福', '和谐福', '富强福', '友善福', '敬业福']
# 打印前言
print('开始集福啦~~~')
# 定义一个函数来实现集五福
def collect_blessings():
patriotic_blessing = 0 # 爱国
harmonious_blessing = 0 # 和谐
friendly_blessing = 0 # 友善
dedicated_blessing = 0 # 敬业
prosperity_blessing = 0 # 富强
while True:
input('按下键获取五福') # 定义一个用户按下Enter键输入
choice = random.choice(five_blessings) # 随机获取列表中的一个元素
print('获取到:' + choice) # 打印随机获取的福
print('当前拥有的福:')
if choice == '爱国福':
patriotic_blessing += 1 # 每次获取一个福就累加一次
print('爱国福:\t' + str(patriotic_blessing) + '富强福:\t' + str(prosperity_blessing) + '和谐福:\t' + str(
harmonious_blessing) + '友善福:\t' + str(friendly_blessing) + '敬业福:\t' + str(dedicated_blessing))
elif choice == '富强福':
prosperity_blessing += 1
print('爱国福:\t' + str(patriotic_blessing) + '富强福:\t' + str(prosperity_blessing) + '和谐福:\t' + str(
harmonious_blessing) + '友善福:\t' + str(friendly_blessing) + '敬业福:\t' + str(dedicated_blessing))
elif choice == '和谐福':
harmonious_blessing += 1
print('爱国福:\t' + str(patriotic_blessing) + '富强福:\t' + str(prosperity_blessing) + '和谐福:\t' + str(
harmonious_blessing) + '友善福:\t' + str(friendly_blessing) + '敬业福:\t' + str(dedicated_blessing))
elif choice == '友善福':
friendly_blessing += 1
print('爱国福:\t' + str(patriotic_blessing) + '富强福:\t' + str(prosperity_blessing) + '和谐福:\t' + str(
harmonious_blessing) + '友善福:\t' + str(friendly_blessing) + '敬业福:\t' + str(dedicated_blessing))
else:
dedicated_blessing += 1
print('爱国福:\t' + str(patriotic_blessing) + '富强福:\t' + str(prosperity_blessing) + '和谐福:\t' + str(
harmonious_blessing) + '友善福:\t' + str(friendly_blessing) + '敬业福:\t' + str(dedicated_blessing))
collect_blessings()
代码截图:
运行截图:
总结:本题目有几个知识点需要掌握的是:1.random.choice()函数随机从一个序列中取出一个元素 2、整体思路每次按一次enter键随机产生的福,最后打印次数累加,这个跟前面题目从1+……100的思路类似 3、字符串的拼接需要str()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。