欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

如何利用Python開發一個簡單的猜數字游戲

系統 1720 0

前言

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

游戲規則

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

準備好Python3

首先,需要在計算機上安裝Python。可以從Python官網下載并安裝。本教程需要使用最新版的Python 3(版本3.x.x)。

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

現在,在設備上打開文本/代碼編輯器。就個人而言,我偏好使用Brackets。 Windows上預裝了Notepad, Mac OS包含TextEdit,而Linux用戶可以使用Vim。

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

如何利用Python開發一個簡單的猜數字游戲_第1張圖片

編碼

本教程的說明將作為注釋包含在代碼中。 在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()函數中所說的內容。你的代碼想怎么寫都可以。

如何利用Python開發一個簡單的猜數字游戲_第2張圖片

運行程序

根據你的操作系統,打開命令提示符(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.")
          

總結

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


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产成人aaa在线视频免费观看 | 日日操av | 漂流教室免费观看韩国电影 | 毛片免费观看 | 色老头xxxwww作爱视频 | 欧美精品在线观看 | 日韩欧美在线看 | 日韩在线视频免费观看 | 国产三级在线观看视频 | 韩国美女丝袜一区二区 | 国产日韩精品一区 | 婷婷免费在线 | 99亚洲视频 | av色偷偷| 欧美金妇欧美乱妇视频 | 国产精品久久久久久亚洲色 | 欧美精品一区二区三区在线 | 国产精品热 | 国产激情久久久久久熟女老人AV | av免费不卡国产观看 | 浮力影院在线 | 成人做爽爽爽爽免费国产软件 | 国产在线观看91一区二区三区 | 国产专区在线视频 | 久久免费看少妇高潮A片麻豆 | 免费色在线 | 久久成人一区二区 | 亚洲欧美另类在线观看 | 亚洲综合激情另类小说区 | 丰满少妇久久久久久久 | 午夜网页 | 欧美人成在线视频 | 国内精品久久久久影院老司 | 久草在线视频精品 | 国产一国产一有一级毛片 | 91精品国产高清久久久久久91 | 欧美成在人线a免费视频 | 免费黄网站在线看 | 久久成人一区二区 | 午夜视频在线观看视频 | 极品xxxx欧美一区二区 |