• <del id="a8uas"></del>
    • 千鋒教育-做有情懷、有良心、有品質的職業教育機構

      400-811-9990
      手機站
      千鋒教育

      千鋒學習站 | 隨時隨地免費學

      千鋒教育

      掃一掃進入千鋒手機站

      領取全套視頻
      千鋒教育

      關注千鋒學習站小程序
      隨時隨地免費學習課程

      上海
      • 北京
      • 鄭州
      • 武漢
      • 成都
      • 西安
      • 沈陽
      • 廣州
      • 南京
      • 深圳
      • 大連
      • 青島
      • 杭州
      • 重慶
      當前位置:合肥千鋒IT培訓  >  技術干貨  >  用Python開發一個簡單的猜數字游戲

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

      來源:千鋒教育
      發布人:xqq
      時間: 2023-11-05 21:40:12

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

      游戲規則

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

      準備好Python3

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

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

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

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

      編碼

      本教程的說明將作為注釋包含在代碼中。在Python中,注釋以#開頭并一直持續到行結束。

      fromkeras.layersimportConv2D,MaxPooling2D,GlobalAveragePooling2D

      #First,weneedtoimportthe'random'module.

      #Thismodulecontainsthefunctionalityweneedtobeabletorandomlyselectthewinningnumber.

      importrandom

      #Now,weneedtoselectarandomnumber.

      #Thislinewillsetthevariable'correct'tobeequaltoarandomintegerbetween1and10.

      correct=random.randint(1,10)

      #Let'sgettheuser'sfirstguessusingthe'input'function.

      guess=input("Enteryourguess:")

      #Rightnow,theuser'sinputisformattedasastring.

      #Wecanformatitasanintegerusingthe'int'function.

      guess=int(guess)

      #Let'sstartaloopthatwillcontinueuntiltheuserhasguessedcorrectly.

      #Wecanusethe'!='operatortomean'notequal'.

      whileguess!=correct:

      #Everythinginthisloopwillrepeatuntiltheuserhasguessedcorrectly.

      #Let'sstartbygivingtheuserfeedbackontheirguess.Wecandothisusingthe'if'statement.

      #Thisstatementwillcheckifacomparisonistrue.

      #Ifitis,thecodeinsidethe'if'statementwillrun.

      ifguess>correct:

      #Thiscodewillruniftheuserguessedtoohigh.

      #Wecanshowamessagetotheuserusingthe'print'function.

      print("You'veguessedtoohigh.Tryguessinglower.")

      else:

      #The'else'statementaddsontoan'if'statement.

      #Itwillruniftheconditionofthe'if'statementisfalse.

      #Inthiscase,itwillruniftheuserguessedtoolow,sowecangivethemfeedback.

      print("You'veguessedtoolow.Tryguessinghigher.")

      #Nowweneedtolettheuserguessagain.

      #NoticehowIamcombiningthetwolinesofguessingcodetomakejustoneline.

      guess=int(input("Enteryourguess:"))

      #Ifauser'sguessisstillincorrect,thecodeinthe'while'loopwillberepeated.

      #Ifthey'vereachedthispointinthecode,itmeanstheyguessedcorrectly,solet'ssaythat.

      print("Congratulations!You'veguessedcorrectly.")

      此外,可以隨意更改程序中的任何內容。

      例如,可以將正確的數字設置為1到100而不是1到10,可以更改程序在print()函數中所說的內容。你的代碼想怎么寫都可以。

      運行程序

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

      pythonC:/Users/username/Desktop/main.py

      pyC:/Users/username/Desktop/main.py

      python3C:/Users/username/Desktop/main.py

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

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

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

      importrandom

      correct=random.randint(1,10)

      guess=input("Enteryourguess:")

      guess=int(guess)

      whileguess!=correct:

      ifguess>correct:

      print("You'veguessedtoohigh.Tryguessinglower.")

      else:

      print("You'veguessedtoolow.Tryguessinghigher.")

      guess=int(input("Enteryourguess:"))

      print("Congratulations!You'veguessedcorrectly.")

      以上內容為大家介紹了用Python開發一個簡單的猜數字游戲,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。http://www.mobiletrain.org/

      聲明:本站稿件版權均屬千鋒教育所有,未經許可不得擅自轉載。

      猜你喜歡LIKE

      Python查看模塊中的屬性

      2023-11-05

      Python之三目運算

      2023-11-05

      Python 之模塊重載的五種方法

      2023-11-05

      最新文章NEW

      使用Black自由格式化Python

      2023-11-05

      入門Python的4大陷阱

      2023-11-05

      python經典最短代碼實現排序的功能

      2023-11-05

      相關推薦HOT

      更多>>

      快速通道 更多>>

      最新開班信息 更多>>

      網友熱搜 更多>>