当前位置:网站首页>猜数字游戏
猜数字游戏
2022-07-04 07:41:00 【算法与编程之美】
0 引言
在python中,可以自己编写一些小程序,进行一些有趣的小游戏。
1 问题
猜数字游戏:
1.系统随机生成一个1~100的数字;
2.用户共有5次机会猜;
3.如果用户猜测数字大于系统给出的数字,打印"too big"
4.如果用户猜测数字小于系统给出的数字,打印"too small"
5.如果用户猜测的数字等于系统给出的数字,打印"恭喜中奖",并退出循环
2 方法
毫无疑问,本题需要使用循环来解答。首先需要使用“from random import*”来随机生成一个1-100的数字。在循环中,分别是“too big”,“too small”以及“恭喜中奖”。循环最多只有五次。
3 实验结果与讨论
通过实验、实践等证明提出的方法是有效的,是能够解决开头提出的问题。
代码清单 1
from random import* num=randint(1,101) for i in range(5): a=eval(input('请输入一个数字:')) if a>num: print('too big') if a<num: print('too small') if a==num: print('恭喜中奖') break |
4 结语
针对本次使用python编写一个有趣的小程序,我们使用了循环结构和“from random import*”来随机生成一个1-100的数字进行猜数字游戏。循环结构可以重复那些需要被多次执行的程序,这样就大大减少了工作量,节约了人力,但在编写循环程序时,需要记得编写退出程序,以免一直循环。
边栏推荐
- This monitoring system can monitor the turnover intention and fishing all, and the product page has 404 after the dispute appears
- System architecture design of circle of friends
- Routing decorator of tornado project
- There is no Chinese prompt below when inputting text in win10 Microsoft Pinyin input method
- Introduction to sap commerce cloud B2B organization function
- OKR vs. KPI 一次搞清楚这两大概念!
- Valentine's Day is coming! Without 50W bride price, my girlfriend was forcibly dragged away...
- Thesis learning -- time series similarity query method based on extreme point characteristics
- MYCAT middleware installation and use
- Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
猜你喜欢
How to use MOS tube to realize the anti reverse connection circuit of power supply
Rhcsa day 3
Zephyr learning notes 1, threads
Thesis learning -- time series similarity query method based on extreme point characteristics
Xcode 14之大变化详细介绍
Unity opens the explorer from the inspector interface, selects and records the file path
神经网络入门(下)
zabbix 5.0监控客户端
两年前美国芯片扭捏着不卖芯片,如今芯片堆积如山祈求中国帮忙
JVM中堆概念
随机推荐
21 examples of strategic goals to promote the rapid development of your company
L1-030 one gang one (15 points)
What determines vacuum permittivity and vacuum permeability? Why do these two physical quantities exist?
Introduction to rce in attack and defense world
Basic DOS commands
PCIE知识点-010:PCIE 热插拔资料从哪获取
How to send mail with Jianmu Ci
Introduction to sap commerce cloud B2B organization function
[Mori city] random talk on GIS data (I)
One of the general document service practice series
Docker install MySQL
Guoguo took you to write a linked list, and the primary school students said it was good after reading it
zabbix 5.0监控客户端
Is l1-029 too fat (5 points)
[web security] nodejs prototype chain pollution analysis
Life planning (flag)
Comparison between applet framework and platform compilation
Text processing function sorting in mysql, quick search of collection
SQL foundation 9 [grouping data]
MYCAT middleware installation and use