如何利用Python开发一个简单的猜数字游戏
作者:读芯术 发布时间:2022-05-21 20:38:08
前言
本文介绍如何使用Python制作一个简单的猜数字游戏。
游戏规则
玩家将猜测一个数字。如果猜测是正确的,玩家赢。如果不正确,程序会提示玩家所猜的数字与实际数字相比是“大(high)”还是“小(low)”,如此往复直到玩家猜对数字。
准备好Python3
首先,需要在计算机上安装Python。可以从Python官网下载并安装。本教程需要使用最新版的Python 3(版本3.x.x)。
确保选中将Python添加到PATH变量的框。如果不这样做,将很难运行该程序。
现在,在设备上打开文本/代码编辑器。就个人而言,我偏好使用Brackets。 Windows上预装了Notepad, Mac OS包含TextEdit,而Linux用户可以使用Vim。
打开文本编辑器后,保存新文件。我将它命名为main.py,但你可以随意命名,只要它以.py结尾即可。
编码
本教程的说明将作为注释包含在代码中。 在Python中,注释以#开头并一直持续到行结束。
from keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D
# First, we need to import the 'random' module.
# This module contains the functionality we need to be able to randomly
select the winning number.
import random
# Now, we need to select a random number.
# This line will set the variable 'correct' to be equal to a random
integer between 1 and 10.
correct = random.randint(1, 10)
# Let's get the user's first guess using the 'input' function.
guess = input("Enter your guess: ")
# Right now, the user's input is formatted as a string.
# We can format it as an integer using the 'int' function.
guess = int(guess)
# Let's start a loop that will continue until the user has guessed
correctly.
# We can use the '!=' operator to mean 'not equal'.
while guess != correct:
# Everything in this loop will repeat until the user has guessed
correctly.
# Let's start by giving the user feedback on their guess. We can do
this using the 'if' statement.
# This statement will check if a comparison is true.
# If it is, the code inside the 'if' statement will run.
if guess > correct:
# This code will run if the user guessed too high.
# We can show a message to the user using the 'print' function.
print("You've guessed too high. Try guessing lower.")
else:
# The 'else' statement adds on to an 'if' statement.
# It will run if the condition of the 'if' statement is false.
# In this case, it will run if the user guessed too low, so we can give
them feedback.
print("You've guessed too low. Try guessing higher.")
# Now we need to let the user guess again.
# Notice how I am combining the two lines of guessing code to make just
one line.
guess = int(input("Enter your guess: "))
# If a user's guess is still incorrect, the code in the 'while' loop
will be repeated
.# If they've reached this point in the code, it means they guessed
correctly, so let's say that.
print("Congratulations! You've guessed correctly.")
此外,可以随意更改程序中的任何内容。
例如,可以将正确的数字设置为1到100而不是1到10,可以更改程序在print()函数中所说的内容。你的代码想怎么写都可以。
运行程序
根据你的操作系统,打开命令提示符(Windows / Linux)或终端(Mac)。 按顺序尝试以下每个命令。 如果正确安装Python,其中至少有一个应该可以运行。
python C:/Users/username/Desktop/main.py
py C:/Users/username/Desktop/main.py
python3 C:/Users/username/Desktop/main.py
确保将C:/Users/username/Desktop/main.py替换为Python文件的完整路径。程序运行后,可测试一下,玩几次! 完成操作后,按向上箭头键复制最后一个命令,然后按Enter即可再次运行。以下是没有任何注释的代码版本:
import random
correct = random.randint(1, 10)
guess = input("Enter your guess: ")
guess = int(guess)
while guess != correct:
if guess > correct:
print("You've guessed too high. Try guessing lower.")
else:
print("You've guessed too low. Try guessing higher.")
guess = int(input("Enter your guess: "))
print("Congratulations! You've guessed correctly.")
来源:https://juejin.im/post/5d8701d3e51d453c11684d1f
猜你喜欢
- PHP在运行时, 针对严重程度不同的错误,会给以不同的提示。 eg:在$a没声明时,直接相加,值为NULL,相加时当成0来算.但是,却提示N
- 最近在改一个嵌入在frame中的页面的时候,使用了jquery做效果,而页面本身也绑定了onload事件。改完后,Firefox下测试正常流
- 思路:队列使用说明:multiprocessing.Queue()#用于进程间通信,单主进程与子进程无法通信(使用进程池时尽量不要使用这个)
- 以下是SQL连接代码: 代码如下:Function Open_conn() dim Conn,Strconn&
- exam = { 'math': '95', 'eng': '96',
- 阅读上一章:Chapter 10 应用CSSChapter 11 打印样式先前在第10章中,讨论了几种为文档应用CSS的方法,这一章是要研究
- 动态语言与静态语言有很多不同,最大的特性之一就是可以实现动态的对类和实例进行修改,在Python中,我们创建了一个类后可以对实例和类绑定心的
- 在 .NET Framework 2.0中,ConfigurationManager类新增了两个属性AppSe
- 前言由与上不了学,教我们Mastercam的老师提前给我们布置了4道习题。对我们太好了,谢谢老师�没办法,干就完了。只是要求附上制作过程视频
- ConfigParser库的使用及遇到的坑背景:这几天想在接口测试中增加logging打印功能,在testerHome正好发现有人分享自己的
- 继承是面向对象编程的一个重要的方式,通过继承,子类就可以扩展父类的功能。在python中一个类能继承自不止一个父类,这叫做python的多重
- 引用类型(Reference)在许多计算机语言中都被使用,而且是作为一个非常强大而实用的特性存在。它有类似指针(Pointer)的实现,却又
- 按照本文操作和体会,会对sql优化有个基本最简单的了解,其他深入还需要更多资料和实践的学习: 1. 建表: 代码如下:creat
- 我就废话不多说了,还是直接看代码吧!#!/usr/bin/env python3#coding = utf-8def is_triangle
- 各种asp字符串处理函数,包括:把字符串换为char型数组,把一个数组转换成一个字符串,检查源字符串str是否以chars开头,检查源字符串
- jQuery 1.4 源码 449 行(core.js 431 行),判断是否为函数的方法如下(思路来源于 Douglas Crockfor
- 目的对字符串的每个字符进行处理,其实每个字符(Char)就是一个长度为1的字符串。方法1.使用内建函数list()>>>
- 本文实例总结了PHP中非常有用却鲜有人知的函数。分享给大家供大家参考,具体如下:PHP里有非常丰富的内置函数,很多我们都用过,但仍有很多的函
- 在CSS布局中,ul,li的运用是非常常见的。配合DIV与CSS,创建无表格布局。 其实Dreamweaver中也有自带的CSS布局的范例,
- 功能: 1、 允许/限制对表的修改 2、 自动生成派生列,比如自增字段 3、 强制数据一致性 4、 提供审计和日志记录 5、 防止无效的事务