Last active
March 15, 2017 15:29
-
-
Save jiankaiwang/b275574a6be54962f9fa9a469484a851 to your computer and use it in GitHub Desktop.
Quickly Start Programming on Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Mon Feb 20 17:46:53 2017 | |
author: JianKaiWang | |
platform: python 3 | |
""" | |
# 引用外部套件 random | |
import random | |
# 產生要猜的數字, 使用套件方法 | |
guessNum = int(random.randrange(10, 99)) | |
# 變數儲存透過 standard input 取得數字 | |
guessNumber = 0 | |
# 變數 : numberList, 型態 : 串列 | |
guessNumberList = [] | |
# 函式宣告,回傳字串 | |
def GuessNum(guessNumber): | |
# 流程控制,判斷式 | |
if guessNumber > guessNum: | |
return("較大,猜小一點的數字") | |
elif guessNumber < guessNum: | |
return("較小,猜大一點的數字") | |
# 進入迴圈 | |
while True: | |
guessNumber = (int)(input("猜一數字:")) | |
if guessNumber == guessNum: | |
print("就是他 !!") | |
print("猜錯了 " + str(len(guessNumberList)) + " 次,分別如下") | |
# 迴圈第二種寫法,已知數目 | |
for i in range(0, len(guessNumberList), 1): | |
print("第 " + str(i + 1) + " 次是 : " + str(guessNumberList[i]) ) | |
# 離開主迴圈 | |
break | |
else: | |
print(GuessNum(guessNumber)) | |
# 加入串列 | |
guessNumberList.append(guessNumber); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment