成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

開發 后端
本文介紹如何使用Python制作一個簡單的猜數字游戲。玩家將猜測一個數字。如果猜測是正確的,玩家贏。如果不正確,程序會提示玩家所猜的數字與實際數字相比是“大(high)”還是“小(low)”,如此往復直到玩家猜對數字。

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

游戲規則

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

[[277485]]

準備好Python3

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

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

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

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

編碼

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

  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'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."

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

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

運行程序

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

  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文件的完整路徑。

程序運行后,可測試一下,玩幾次! 完成操作后,按向上箭頭鍵復制最后一個命令,然后按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."
責任編輯:華軒 來源: 今日頭條
相關推薦

2015-03-24 19:48:24

2021-04-22 09:57:37

Random方法游戲

2021-01-01 19:30:21

Python編程語言

2017-06-08 15:53:38

PythonWeb框架

2020-07-20 10:00:52

Python翻譯工具命令行

2023-02-13 08:26:28

猜數字Basic

2023-02-23 14:30:27

游戲Tcl

2017-11-23 08:30:26

編程Python擲骰子游戲

2020-11-30 13:16:29

Python編程語言

2022-03-24 07:57:58

Python水果忍者游戲

2021-02-05 16:03:48

JavaScript游戲學習前端

2021-10-04 18:49:46

Fortran 77語言猜數字

2021-01-12 06:42:50

Lua腳本語言編程語言

2009-09-11 09:11:09

2011-09-08 13:41:53

Widget

2020-12-09 11:42:18

WiFi IoT鴻蒙開發

2021-01-03 16:30:34

Rust編程語言

2021-01-14 08:55:20

C語言編程

2019-05-14 12:30:07

PythonPygame游戲框架

2020-06-04 12:55:44

PyTorch分類器神經網絡
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日日操夜夜操视频 | av乱码 | 欧美专区日韩 | 欧美高清视频一区 | 亚洲精品一区中文字幕 | 天天干狠狠操 | aaa在线观看 | 久久草在线视频 | 黄在线 | 久久精品91久久久久久再现 | 久久久涩 | 久久久久久国产精品 | 日本精品一区二区 | av超碰| 国产一区二区精品在线观看 | 欧美日韩成人 | 99久久精品一区二区成人 | 成人午夜影院 | 欧美久久久久久久久中文字幕 | 久久一本 | 久在线精品视频 | 午夜手机在线视频 | 免费观看黄 | 国产亚洲精品久久久久久牛牛 | 久久综合一区 | 91av视频在线观看 | 久久久片| 国产1区2区在线观看 | 青娱乐自拍 | 91青青草视频 | 欧美精品一区三区 | 不卡一区二区三区四区 | 综合色播 | 欧美日韩在线视频一区 | 性精品 | www国产成人免费观看视频,深夜成人网 | 日韩精品一区二区三区 | 偷拍第一页 | 我要看黄色录像一级片 | 污污免费网站 | 久久精品日产第一区二区三区 |