日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
用Python開發(fā)一個簡單的猜數(shù)字游戲

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

成都創(chuàng)新互聯(lián)服務(wù)項目包括銀海網(wǎng)站建設(shè)、銀海網(wǎng)站制作、銀海網(wǎng)頁制作以及銀海網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,銀海網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到銀海省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

游戲規(guī)則

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

準(zhǔn)備好Python3

首先,需要在計算機上安裝Python。可以從Python官網(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é)束。

 
 
 
  1. from keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D 
  2. # First, we need to import the 'random' module. 
  3. # This module contains the functionality we need to be able to randomly select the winning number. 
  4. import random 
  5. # Now, we need to select a random number. 
  6. # This line will set the variable 'correct' to be equal to a random integer between 1 and 10. 
  7. correct = random.randint(1, 10) 
  8. # Let's get the user's first guess using the 'input' function. 
  9. guess = input("Enter your guess: ") 
  10. # Right now, the user's input is formatted as a string. 
  11. # We can format it as an integer using the 'int' function. 
  12. guess = int(guess) 
  13. # Let's start a loop that will continue until the user has guessed correctly. 
  14. # We can use the '!=' operator to mean 'not equal'. 
  15. while guess != correct: 
  16. # Everything in this loop will repeat until the user has guessed correctly. 
  17. # Let's start by giving the user feedback on their guess. We can do this using the 'if' statement. 
  18. # This statement will check if a comparison is true. 
  19. # If it is, the code inside the 'if' statement will run. 
  20. if guess > correct: 
  21. # This code will run if the user guessed too high. 
  22. # We can show a message to the user using the 'print' function. 
  23. print("You've guessed too high. Try guessing lower.") 
  24. else: 
  25. # The 'else' statement adds on to an 'if' statement. 
  26. # It will run if the condition of the 'if' statement is false. 
  27. # In this case, it will run if the user guessed too low, so we can give them feedback. 
  28. print("You've guessed too low. Try guessing higher.") 
  29. # Now we need to let the user guess again. 
  30. # Notice how I am combining the two lines of guessing code to make just one line. 
  31. guess = int(input("Enter your guess: ")) 
  32. # If a user's guess is still incorrect, the code in the 'while' loop will be repeated. 
  33. # If they've reached this point in the code, it means they guessed correctly, so let's say that. 
  34. 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)該可以運行。

 
 
 
  1. python C:/Users/username/Desktop/main.py 
  2. py C:/Users/username/Desktop/main.py 
  3. python3 C:/Users/username/Desktop/main.py  

確保將C:/Users/username/Desktop/main.py替換為Python文件的完整路徑。

程序運行后,可測試一下,玩幾次! 完成操作后,按向上箭頭鍵復(fù)制最后一個命令,然后按Enter即可再次運行。

以下是沒有任何注釋的代碼版本:

 
 
 
  1. import random 
  2. correct = random.randint(1, 10) 
  3. guess = input("Enter your guess: ") 
  4. guess = int(guess) 
  5. while guess != correct: 
  6. if guess > correct: 
  7. print("You've guessed too high. Try guessing lower.") 
  8. else: 
  9. print("You've guessed too low. Try guessing higher.") 
  10. guess = int(input("Enter your guess: ")) 
  11. print("Congratulations! You've guessed correctly.") 

分享名稱:用Python開發(fā)一個簡單的猜數(shù)字游戲
標(biāo)題鏈接:http://m.5511xx.com/article/cdpephc.html