台北來的土包子
 
Python – Guess the number -猜數字

Python – Guess the number -猜數字

#Guess Number use While
#https://www.george.tw/2019/10/07/python-guess-the-number
import random num=random.randint(1,100) guessFlag=False while guessFlag==False: guess=int(input("Enter a number:")) if guess==num: print("God job") guessFlag=True elif guess > num : print ("Too high") elif guess < num: print ("Too low")

Flow:

  • Step 1. Import random library
  • Step 2. Generate a random number between 1 and 100
  • Step 3. Set a flag named “guessFlag” to control when leave this while-loop
  • Step 4. Ask user to pick up a number, put this number into variable named “guess”
  • Step 5. Check the number, if “guess” bigger then “num” show “Too high”, if “guess” samller then “num” show “Too low”, if “guess” equal “num” show “Good job” and set “guessFlag” = true then leave while-loop

Source code: GitHub
# My English suck , if you find some errors, please tell me, thanks.

流程:
1. 載入 random 函式庫
2. 產生一個介於 1 到 100 的亂數
3. 設定一個變數名為 guessFlag, 其值為 False, 用它來控制何時該離開迴圈
4. 讓使用者輸入一個數字
5. 檢查數字,如果數字太大顯示”To High”,數字太小顯示 “Too Low”, 如果正確顯示 “Good job” , 並將 guessFlag 設為 true, 之後離開迴圈

原始碼: GitHub

發表迴響