最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何利用Python開發(fā)一個簡單的猜數(shù)字游戲

 更新時間:2019年09月22日 15:05:26   作者:讀芯術(shù)  
這篇文章主要給大家介紹了關(guān)于如何利用Python開發(fā)一個簡單的猜數(shù)字游戲的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

前言

本文介紹如何使用Python制作一個簡單的猜數(shù)字游戲。

游戲規(guī)則

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

準備好Python3

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

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

現(xiàn)在,在設(shè)備上打開文本/代碼編輯器。就個人而言,我偏好使用Brackets。 Windows上預(yù)裝了Notepad, Mac OS包含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)容。你的代碼想怎么寫都可以。

運行程序

根據(jù)你的操作系統(tǒng),打開命令提示符(Windows / Linux)或終端(Mac)。 按順序嘗試以下每個命令。 如果正確安裝Python,其中至少有一個應(yīng)該可以運行。

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文件的完整路徑。程序運行后,可測試一下,玩幾次! 完成操作后,按向上箭頭鍵復(fù)制最后一個命令,然后按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.")

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。

相關(guān)文章

  • python數(shù)據(jù)持久存儲 pickle模塊的基本使用方法解析

    python數(shù)據(jù)持久存儲 pickle模塊的基本使用方法解析

    這篇文章主要介紹了python數(shù)據(jù)持久存儲 pickle模塊的基本使用方法解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • pycharm出現(xiàn)No?pyvenv.cfg?file錯誤的問題解決

    pycharm出現(xiàn)No?pyvenv.cfg?file錯誤的問題解決

    本文主要介紹了pycharm出現(xiàn)No?pyvenv.cfg?file錯誤的問題解決,主要是通過恢復(fù)歷史記錄中的未刪除狀態(tài)來解決,下面就來詳細的介紹一下,感興趣的可以了解一下
    2025-05-05
  • 基于Python實現(xiàn)在控制臺查看excel的內(nèi)容

    基于Python實現(xiàn)在控制臺查看excel的內(nèi)容

    這篇文章主要為大家詳細介紹了如何基于Python實現(xiàn)在控制臺查看excel的內(nèi)容,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12
  • Python3使用requests包抓取并保存網(wǎng)頁源碼的方法

    Python3使用requests包抓取并保存網(wǎng)頁源碼的方法

    這篇文章主要介紹了Python3使用requests包抓取并保存網(wǎng)頁源碼的方法,實例分析了Python3環(huán)境下requests模塊的相關(guān)使用技巧,需要的朋友可以參考下
    2016-03-03
  • Python Django切換MySQL數(shù)據(jù)庫實例詳解

    Python Django切換MySQL數(shù)據(jù)庫實例詳解

    這篇文章主要介紹了Python Django切換MySQL數(shù)據(jù)庫實例詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • python 實現(xiàn)多維數(shù)組(array)排序

    python 實現(xiàn)多維數(shù)組(array)排序

    今天小編就為大家分享一篇python 實現(xiàn)多維數(shù)組(array)排序,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • python 矩陣增加一行或一列的實例

    python 矩陣增加一行或一列的實例

    下面小編就為大家分享一篇python 矩陣增加一行或一列的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • keras 使用Lambda 快速新建層 添加多個參數(shù)操作

    keras 使用Lambda 快速新建層 添加多個參數(shù)操作

    這篇文章主要介紹了keras 使用Lambda 快速新建層 添加多個參數(shù)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • pytorch版本PSEnet訓(xùn)練并部署方式

    pytorch版本PSEnet訓(xùn)練并部署方式

    這篇文章主要介紹了pytorch版本PSEnet訓(xùn)練并部署方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Python?如何引用不確定的函數(shù)

    Python?如何引用不確定的函數(shù)

    在Python中,引用不確定的函數(shù)通常意味著我們可能在運行時才知道要調(diào)用哪個函數(shù),或者我們可能想根據(jù)某些條件動態(tài)地選擇不同的函數(shù)來執(zhí)行,下面給大家分享Python?如何引用不確定的函數(shù),感興趣的朋友跟隨小編一起看看吧
    2024-07-07

最新評論

任丘市| 义马市| 滨州市| 成安县| 乌拉特中旗| 隆化县| 辽阳市| 津市市| 镇赉县| 库伦旗| 江达县| 朝阳区| 航空| 鲁甸县| 扶沟县| 应用必备| 上犹县| 和田市| 新巴尔虎右旗| 留坝县| 珲春市| 宜兴市| 通许县| 乐至县| 宜川县| 和静县| 安平县| 桦川县| 砀山县| 双城市| 澄迈县| 攀枝花市| 达州市| 宁安市| 天等县| 沭阳县| 咸宁市| 葫芦岛市| 琼中| 门头沟区| 桂东县|