用Python开发一个简单的猜数字游戏

开发 后端
本文介绍如何使用Python制作一个简单的猜数字游戏。玩家将猜测一个数字。如果猜测是正确的,玩家赢。如果不正确,程序会提示玩家所猜的数字与实际数字相比是“大(high)”还是“小(low)”,如此往复直到玩家猜对数字。

 本文介绍如何使用Python制作一个简单的猜数字游戏。

游戏规则

玩家将猜测一个数字。如果猜测是正确的,玩家赢。如果不正确,程序会提示玩家所猜的数字与实际数字相比是“大(high)”还是“小(low)”,如此往复直到玩家猜对数字。

[[277485]]

准备好Python3

首先,需要在计算机上安装Python。可以从Python官网下载并安装。本教程需要使用最新版的Python 3(版本3.x.x)。

确保选中将Python添加到PATH变量的框。如果不这样做,将很难运行该程序。

现在,在设备上打开文本/代码编辑器。就个人而言,我偏好使用Brackets。 Windows上预装了Notepad, Mac OS包含TextEdit,而Linux用户可以使用Vim。

打开文本编辑器后,保存新文件。我将它命名为main.py,但你可以随意命名,只要它以.py结尾即可。

编码

本教程的说明将作为注释包含在代码中。 在Python中,注释以#开头并一直持续到行结束。

  1. from keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D 
  2. First, we need to import the 'random' module. 
  3. # This module contains the functionality we need to be able to randomly select the winning number. 
  4. import random 
  5. # Now, we need to select a random number. 
  6. # This line will set the variable 'correct' to be equal to a random integer between 1 and 10. 
  7. correct = random.randint(1, 10) 
  8. # Let's get the user'first guess using the 'input' function
  9. guess = input("Enter your guess: "
  10. Right now, the user's input is formatted as a string. 
  11. # We can format it as an integer using the 'int' function
  12. guess = int(guess) 
  13. # Let's start a loop that will continue until the user has guessed correctly. 
  14. # We can use the '!=' operator to mean 'not equal'
  15. while guess != correct: 
  16. # Everything in this loop will repeat until the user has guessed correctly. 
  17. # Let's start by giving the user feedback on their guess. We can do this using the 'if' statement. 
  18. # This statement will check if a comparison is true
  19. # If it is, the code inside the 'if' statement will run. 
  20. if guess > correct: 
  21. # This code will run if the user guessed too high. 
  22. # We can show a message to the user using the 'print' function
  23. print("You've guessed too high. Try guessing lower."
  24. else
  25. # The 'else' statement adds on to an 'if' statement. 
  26. # It will run if the condition of the 'if' statement is false
  27. In this case, it will run if the user guessed too low, so we can give them feedback. 
  28. print("You've guessed too low. Try guessing higher."
  29. # Now we need to let the user guess again. 
  30. # Notice how I am combining the two lines of guessing code to make just one line. 
  31. guess = int(input("Enter your guess: ")) 
  32. # If a user's guess is still incorrect, the code in the 'while' loop will be repeated. 
  33. # If they've reached this point in the code, it means they guessed correctly, so let's say that. 
  34. print("Congratulations! You've guessed correctly."

此外,可以随意更改程序中的任何内容。

例如,可以将正确的数字设置为1到100而不是1到10,可以更改程序在print()函数中所说的内容。你的代码想怎么写都可以。

运行程序

根据你的操作系统,打开命令提示符(Windows / Linux)或终端(Mac)。 按顺序尝试以下每个命令。 如果正确安装Python,其中至少有一个应该可以运行。

  1. python C:/Users/username/Desktop/main.py 
  2. py C:/Users/username/Desktop/main.py 
  3. python3 C:/Users/username/Desktop/main.py  

确保将C:/Users/username/Desktop/main.py替换为Python文件的完整路径。

程序运行后,可测试一下,玩几次! 完成操作后,按向上箭头键复制最后一个命令,然后按Enter即可再次运行。

以下是没有任何注释的代码版本:

  1. import random 
  2. correct = random.randint(1, 10) 
  3. guess = input("Enter your guess: "
  4. guess = int(guess) 
  5. while guess != correct: 
  6. if guess > correct: 
  7. print("You've guessed too high. Try guessing lower."
  8. else
  9. print("You've guessed too low. Try guessing higher."
  10. guess = int(input("Enter your guess: ")) 
  11. print("Congratulations! You've guessed correctly."
责任编辑:华轩 来源: 今日头条
相关推荐

2015-03-24 19:48:24

2021-04-22 09:57:37

Random方法游戏

2021-01-01 19:30:21

Python编程语言

2017-06-08 15:53:38

PythonWeb框架

2020-07-20 10:00:52

Python翻译工具命令行

2017-11-23 08:30:26

编程Python掷骰子游戏

2020-11-30 13:16:29

Python编程语言

2023-02-23 14:30:27

游戏Tcl

2022-03-24 07:57:58

Python水果忍者游戏

2023-02-13 08:26:28

猜数字Basic

2021-02-05 16:03:48

JavaScript游戏学习前端

2019-05-14 12:30:07

PythonPygame游戏框架

2021-10-04 18:49:46

Fortran 77语言猜数字

2021-01-12 06:42:50

Lua脚本语言编程语言

2011-09-08 13:41:53

Widget

2020-12-09 11:42:18

WiFi IoT鸿蒙开发

2009-09-11 09:11:09

2021-01-14 08:55:20

C语言编程

2021-01-03 16:30:34

Rust编程语言

2022-06-03 16:59:13

编程语言Awk
点赞
收藏

51CTO技术栈公众号