與小卡特一起學python 第1章 出發吧 1-2猜數遊戲

yarking207發表於2016-03-28
import random
secret = random.randint(1,99) #隨機神秘數
guess = 0  #遊戲者輸入的數字
tries = 0    #重試次數
print ("AHOY! I'm the Dread Pirate Roberts, and I have a secret!歡迎光臨,這是一個猜數遊戲!")
print ("It is a number from 1 to 99. I'll give you 6 tries.這是一個1-99的一個數字,您有六次機會!")
while guess != secret and tries < 6:
    guess = int(input("What's yer guess?請輸入你的數字"))#input要轉換成int 書中沒有註明,所以除錯時出錯。
    print(guess,secret,tries) #這行是我加入,想知道當前三個的數值是多少。
    if guess < secret:
        print ("Too low,ye scurvy dog!太小了哦,請重新輸入")
    elif guess  > secret:
        print("Too high,landlubber!太大了,請重新輸入")
        #這行空格
    tries = tries +1
       #這行空格
if guess == secret:
    print ("Avast!Ye got it! Found my secret,ye did!太棒了,恭喜猜對了。")
else:
    print("No more guesses!Better luck next time,matey!次數用盡,遊戲結束。")
    print("The sectet number was秘密數字是",secret)

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/220205/viewspace-2065327/,如需轉載,請註明出處,否則將追究法律責任。

相關文章