当前位置:   article > 正文

python12个题_python小题目练习(十二)

集五福python编程题

题目:如下图所示

6c3bae0f2478eb8ea7f631fdaf2a0ba8.png

代码展示:

"""

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

代码截图:

65d969b5e6b819d2ba3e95f1ba3f729b.png

fbdcca5cf536671d3780d72707beb902.png

运行截图:

d31553d92b1f035a0d2c16633c604a08.png

总结:本题目有几个知识点需要掌握的是:1.random.choice()函数随机从一个序列中取出一个元素  2、整体思路每次按一次enter键随机产生的福,最后打印次数累加,这个跟前面题目从1+……100的思路类似  3、字符串的拼接需要str()

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号