如何利用Python開發(fā)一個(gè)簡(jiǎn)單的猜數(shù)字游戲
前言
本文介紹如何使用Python制作一個(gè)簡(jiǎn)單的猜數(shù)字游戲。
游戲規(guī)則
玩家將猜測(cè)一個(gè)數(shù)字。如果猜測(cè)是正確的,玩家贏。如果不正確,程序會(huì)提示玩家所猜的數(shù)字與實(shí)際數(shù)字相比是“大(high)”還是“小(low)”,如此往復(fù)直到玩家猜對(duì)數(shù)字。
準(zhǔn)備好Python3
首先,需要在計(jì)算機(jī)上安裝Python??梢詮腜ython官網(wǎng)下載并安裝。本教程需要使用最新版的Python 3(版本3.x.x)。
確保選中將Python添加到PATH變量的框。如果不這樣做,將很難運(yùn)行該程序。
現(xiàn)在,在設(shè)備上打開文本/代碼編輯器。就個(gè)人而言,我偏好使用Brackets。 Windows上預(yù)裝了Notepad, Mac OS包含TextEdit,而Linux用戶可以使用Vim。
打開文本編輯器后,保存新文件。我將它命名為main.py,但你可以隨意命名,只要它以.py結(jié)尾即可。
編碼
本教程的說(shuō)明將作為注釋包含在代碼中。 在Python中,注釋以#開頭并一直持續(xù)到行結(jié)束。
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.")
此外,可以隨意更改程序中的任何內(nèi)容。
例如,可以將正確的數(shù)字設(shè)置為1到100而不是1到10,可以更改程序在print()函數(shù)中所說(shuō)的內(nèi)容。你的代碼想怎么寫都可以。
運(yùn)行程序
根據(jù)你的操作系統(tǒng),打開命令提示符(Windows / Linux)或終端(Mac)。 按順序嘗試以下每個(gè)命令。 如果正確安裝Python,其中至少有一個(gè)應(yīng)該可以運(yùn)行。
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文件的完整路徑。程序運(yùn)行后,可測(cè)試一下,玩幾次! 完成操作后,按向上箭頭鍵復(fù)制最后一個(gè)命令,然后按Enter即可再次運(yùn)行。以下是沒有任何注釋的代碼版本:
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.")
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
用60行代碼實(shí)現(xiàn)Python自動(dòng)搶微信紅包
這篇文章主要介紹了用60行代碼實(shí)現(xiàn)Python自動(dòng)搶微信紅包,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Python+Selenium實(shí)現(xiàn)短視頻熱點(diǎn)爬取
隨著短視頻的大火,不僅可以給人們帶來(lái)娛樂,還有熱點(diǎn)新聞時(shí)事以及各種知識(shí),刷短視頻也逐漸成為了日常生活的一部分。本文將通過(guò)Pyhton依托Selenium來(lái)爬取短視頻熱點(diǎn),需要的可以參考一下2022-04-04python爬蟲智能翻頁(yè)批量下載文件的實(shí)例詳解
在本篇文章里小編給大家整理的是一篇關(guān)于python爬蟲智能翻頁(yè)批量下載文件的實(shí)例詳解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-02-02在阿里云服務(wù)器上配置CentOS+Nginx+Python+Flask環(huán)境
這篇文章主要介紹了在阿里云服務(wù)器上配置CentOS+Nginx+Python+Flask環(huán)境的教程,值得一提的是這里的方案用Nginx作反向代理而使用Gunicorn作wsgi服務(wù)器,需要的朋友可以參考下2016-06-06pd.DataFrame統(tǒng)計(jì)各列數(shù)值多少的實(shí)例
今天小編就為大家分享一篇pd.DataFrame統(tǒng)計(jì)各列數(shù)值多少的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12Python裝飾器實(shí)現(xiàn)方法及應(yīng)用場(chǎng)景詳解
這篇文章主要介紹了Python裝飾器實(shí)現(xiàn)方法及應(yīng)用場(chǎng)景詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03