导读热词

前言

本文介绍如何使用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.")

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。

相关文章

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

python猜数字游戏编程入门_如何利用Python开发一个简单的猜数字游戏相关推荐

  1. 游戏编程入门(9):开发 Henway(小鸡过马路) 游戏

    应用前面学到的子画面知识,开发一个小鸡过马路游戏. 接上文 游戏编程入门(8):管理子画面 Henway 游戏的玩法 Henway 游戏的主角是一只小鸡,它急需从一条繁忙的高速公路的一边到达另一边.H ...

  2. 《Pygame游戏编程入门》学习——第4章 用户输入:Bomb Catcher游戏

    <Pygame游戏编程入门>学习--第4章 用户输入:Bomb Catcher游戏 第4章 挑战[^1] 问题1. Bomb Catching游戏太小了,玩起来不是很过瘾.毕竟,它只是一个 ...

  3. python游戏最简单代码-如何利用Python开发一个简单的猜数字游戏

    前言 本文介绍如何使用Python制作一个简单的猜数字游戏. 游戏规则 玩家将猜测一个数字.如果猜测是正确的,玩家赢.如果不正确,程序会提示玩家所猜的数字与实际数字相比是"大(high)&q ...

  4. 游戏编程入门(6):开发 Brainiac(记忆对对碰) 游戏

    接上文 游戏编程入门(5):使用键盘和鼠标控制游戏 这个游戏是记忆游戏,将隐藏的方块配对.即点击一下方块,就会翻转出图片页,然后记忆几秒,回归成方块,要将相同图片的方块进行配对. 本章内容包括: 如果 ...

  5. 游戏编程入门(完):开发 Stunt Jumper(摩托车跳跃)游戏

    Stunt Jumper 是一个横向滚动的摩托车跳跃游戏,玩家必须仔细控制摩托车手的速度,以便帮助它越过一排汽车. 本章内容包括: Stunt Jumper 的基本思路 如何设计 Stunt Jump ...

  6. python简单游戏编程入门_[翻译]Python游戏编程入门

    自己学习翻译,原文链接. 此文为Tutorial Team成员 Julian Meyer, 一个13岁的Python开发者所写. 你可以去Google+或者Twitter找到他. 你有没有想过,一个计 ...

  7. python游戏编程入门电子书-请问自学 Python 有必要买课程吗?

    自学Python没有必要买课程,因为现在网上Python相关教程已经足够多了! 当然,自学Python务必先确定方向,即学习Python用来做什么?写爬虫?自动化?人工智能还是数据分析?如果没有确立方 ...

  8. 利用JavaScript实现一个简单的猜数字游戏

    问题描述:程序随机生成一个1到10之间的数字,然后让用户随机输入一个数字,如果用户输入的 数字过大,则提示用户猜的数字过大,请往小的猜,如果用户输入的数字过小,则提示用户输入的数字过小,请往大的猜:当 ...

  9. python如何实现微信自动聊天_如何利用python实现微信智能聊天功能,具体该怎么做?...

    这个实现不难,需要先安装一下itchat库,之后注册一下图灵机器人,最后就可以编程实现微信智能聊天功能了,下面我简单介绍一下实现过程,实验环境win10+python3.6+pycharm5.0,主要 ...

最新文章

  1. XDC约束技巧——CDC篇
  2. 利用.net4.0的dynamic特性制造的超级简单的微信SDK
  3. Hadoop学习--Hive安装与配置
  4. IBM云计算带我们进入新服务经济时代
  5. Django之Model操作
  6. MySQLdb.cursors AttributeError: 'module' object has
  7. mysql5.7.17完全卸载_MySQL5.7完全卸载步骤详解
  8. java基础(七)--- set
  9. OFFICE专业增强版2016 32位安装VISIO出错记录
  10. 前端分页功能(通用)
  11. ffmpeg 转换flv压缩大小_[转]ffmpeg转换参数和对几种视频格式的转换分析
  12. CS224n笔记——Subword Model(十二)
  13. L130被围绕的区域
  14. 如何用ssh隧道绕过防火墙
  15. 6种纯css3徽章样式
  16. DWI_preprocessing DTI_reduce commands
  17. Java学习 --- 设计模式七大原则的依赖倒转原则
  18. oracle已经创建表空间不存在,oracle表空间的创建、删除、查看、表空间不存在、及修改默认表空间详解...
  19. SQL21天自学通pdf 百度云盘
  20. thingsboard使用地图和表可视化资产数据~看完还不会 你pan我

热门文章

  1. 西门子定位器的气动连接
  2. 快速给小程序加上人性化的「添加到我的小程序」提示
  3. 论文降重的网站和工具
  4. 编译原理--正则文法与正则表达式
  5. 登过《Vogue》,当过战地记者,Lee Miller的传奇人生
  6. 【小猫爪】AUTOSAR学习笔记14-EcuM模块
  7. 时空动力学是大脑和思维的“共同货币”吗?
  8. 数字黑手党始末:僵尸网络Kelihos背后的传奇
  9. 验证电路之TPS23753A设计
  10. python一般做什么游戏好玩_只需要9步100行Python代码就可以实现一个简单又好玩的弹球游戏...