当前位置:网站首页>猜数字游戏
猜数字游戏
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的数字进行猜数字游戏。循环结构可以重复那些需要被多次执行的程序,这样就大大减少了工作量,节约了人力,但在编写循环程序时,需要记得编写退出程序,以免一直循环。
边栏推荐
- Node foundation ~ node operation
- Leetcode(215)——数组中的第K个最大元素
- Would you like to go? Go! Don't hesitate if you like it
- Chain ide -- the infrastructure of the metauniverse
- Electronic Association C language level 1 34, piecewise function
- Is l1-029 too fat (5 points)
- Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
- There is no Chinese prompt below when inputting text in win10 Microsoft Pinyin input method
- [kubernetes series] kubesphere is installed on kubernetes
- User login function: simple but difficult
猜你喜欢
[Flink] temporal semantics and watermark
Detailed introduction to the big changes of Xcode 14
This article is enough for learning advanced mysql
SQL foundation 9 [grouping data]
Chain ide -- the infrastructure of the metauniverse
Experience installing VMware esxi 6.7 under VMware Workstation 16
手写简易版flexible.js以及源码分析
Blog stop statement
[Android reverse] function interception (use cache_flush system function to refresh CPU cache | refresh CPU cache disadvantages | recommended time for function interception)
I was pressed for the draft, so let's talk about how long links can be as efficient as short links in the development of mobile terminals
随机推荐
flask-sqlalchemy 循环引用
Oceanbase is the leader in the magic quadrant of China's database in 2021
OKR vs. KPI 一次搞清楚这两大概念!
[Mori city] random talk on GIS data (I)
Zephyr learning notes 1, threads
【FreeRTOS】FreeRTOS学习笔记(7)— 手写FreeRTOS双向链表/源码分析
提升复杂场景三维重建精度 | 基于PaddleSeg分割无人机遥感影像
21 examples of strategic goals to promote the rapid development of your company
This article is enough for learning advanced mysql
[Gurobi] 简单模型的建立
Heap concept in JVM
MySQL中的文本处理函数整理,收藏速查
Activiti common operation data table relationship
What are the work contents of operation and maintenance engineers? Can you list it in detail?
Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
What determines vacuum permittivity and vacuum permeability? Why do these two physical quantities exist?
Is l1-029 too fat (5 points)
墨者学院-phpMyAdmin后台文件包含分析溯源
Activiti常見操作數據錶關系
Devops Practice Guide - reading notes (long text alarm)