欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

千鋒扣丁學(xué)堂Python培訓(xùn)之開發(fā)一個(gè)簡單猜數(shù)字游戲

2019-09-23 10:36:43 5691瀏覽

今天千鋒扣丁學(xué)堂Python培訓(xùn)老師給大家分享一篇關(guān)于如何利用Python開發(fā)一個(gè)簡單的猜數(shù)字游戲的詳細(xì)介紹,文中通過示例代碼介紹的非常詳細(xì)下面我們一起來看一下吧。



游戲規(guī)則

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

準(zhǔn)備好Python3

首先,需要在計(jì)算機(jī)上安裝Python??梢詮腜ython官網(wǎng)下載并安裝。本教程需要使用最新版的Python3(版本3.x.x)。

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

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

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

編碼

本教程的說明將作為注釋包含在代碼中。在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ù)中所說的內(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)行后,可測試一下,玩幾次!完成操作后,按向上箭頭鍵復(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.")

以上就是關(guān)于千鋒扣丁學(xué)堂Python培訓(xùn)之開發(fā)一個(gè)簡單猜數(shù)字游戲的全部內(nèi)容了,想要了解更多關(guān)于Python和人工智能方面內(nèi)容的小伙伴,請關(guān)注扣丁學(xué)堂Python培訓(xùn)官網(wǎng)、微信等平臺,扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育平臺為您提供權(quán)威的Python開發(fā)環(huán)境搭建視頻,Python培訓(xùn)后的前景無限,行業(yè)薪資和未來的發(fā)展會越來越好的,扣丁學(xué)堂老師精心推出的Python視頻教程定能讓你快速掌握Python從入門到精通開發(fā)實(shí)戰(zhàn)技能。扣丁學(xué)堂Python技術(shù)交流群:279521237。


扣丁學(xué)堂微信公眾號                          Python全棧開發(fā)爬蟲人工智能機(jī)器學(xué)習(xí)數(shù)據(jù)分析免費(fèi)公開課直播間


      【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】         【掃碼進(jìn)入Python全棧開發(fā)免費(fèi)公開課】



查看更多關(guān)于"Python開發(fā)資訊"的相關(guān)文章>

標(biāo)簽: Python培訓(xùn) Python視頻教程 Python在線視頻 Python學(xué)習(xí)視頻 Python培訓(xùn)班

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費(fèi)咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關(guān)村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部