利用Python開發猜數字遊戲

原文鏈接:https://www.linuxprobe.com/python-number-guessing.html
導讀 本文介紹如何使用Python製作一個簡單的猜數字遊戲。玩家將猜測一個數字。如果猜測是正確的,玩家贏。如果不正確,程序會提示玩家所猜的數字與實際數字相比是“大(high)”還是“小(low)”,如此往復直到玩家猜對數字。

本文介紹如何使用Python製作一個簡單的猜數字遊戲。

遊戲規則

玩家將猜測一個數字。如果猜測是正確的,玩家贏。如果不正確,程序會提示玩家所猜的數字與實際數字相比是“大(high)”還是“小(low)”,如此往復直到玩家猜對數字。

利用Python開發猜數字遊戲利用Python開發猜數字遊戲

準備好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://www.linuxprobe.com/python-number-guessing.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章