亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

如何利用Python開(kāi)發(fā)一個(gè)簡(jiǎn)單的猜數(shù)字游戲

系統(tǒng) 1858 0

前言

本文介紹如何使用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。可以從Python官網(wǎng)下載并安裝。本教程需要使用最新版的Python 3(版本3.x.x)。

確保選中將Python添加到PATH變量的框。如果不這樣做,將很難運(yùn)行該程序。

現(xiàn)在,在設(shè)備上打開(kāi)文本/代碼編輯器。就個(gè)人而言,我偏好使用Brackets。 Windows上預(yù)裝了Notepad, Mac OS包含TextEdit,而Linux用戶可以使用Vim。

打開(kāi)文本編輯器后,保存新文件。我將它命名為main.py,但你可以隨意命名,只要它以.py結(jié)尾即可。

如何利用Python開(kāi)發(fā)一個(gè)簡(jiǎn)單的猜數(shù)字游戲_第1張圖片

編碼

本教程的說(shuō)明將作為注釋包含在代碼中。 在Python中,注釋以#開(kāi)頭并一直持續(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)容。你的代碼想怎么寫(xiě)都可以。

如何利用Python開(kāi)發(fā)一個(gè)簡(jiǎn)單的猜數(shù)字游戲_第2張圖片

運(yùn)行程序

根據(jù)你的操作系統(tǒng),打開(kāi)命令提示符(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)行。以下是沒(méi)有任何注釋的代碼版本:

            
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ì)腳本之家的支持。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 6080亚洲精品一区 | 特黄aa级毛片免费视频播放 | 日本午夜免费理论片 | 亚洲色吧 | youjizz欧美粗又大中国 | 曰曰鲁夜夜免费播放视频 | 欧美xxxx成人免费视频 | 成人亚洲国产 | 国产小福利 | 免费在线一级毛片 | 日本特级 | 久久这里只精品国产99热 | porno日本xxxx | 狠狠色综合久久婷婷色天使 | 国产美女久久 | 国产色视频一区 | 国产精品婷婷久久爽一下 | 欧美成人免费午夜影视 | 国产精品久久一区二区三区 | 97午夜影院| 欧美日本高清动作片www网站 | 久久精品国产久金国产思思 | 久久久不卡 | 国产亚洲精品九九久在线观看 | 精品欧美成人bd高清在线观看 | 99久久免费精品视频 | 成人短视频在线在线观看 | 999精品久久久中文字幕蜜桃 | 国产激情 | 精品久久国产 | 久久99精品久久久 | 国产视频最新 | 亚洲爱爱久久精品 | 高清不卡| 欧美日韩一区二区三 | 久久精品伊人 | 国产人成久久久精品 | 日本毛片高清免费视频 | 9久久免费国产精品特黄 | 真实子伦视频不卡 | 女人18毛片特级一级免费视频 |