python經典小程序:猜數字游戲


 

 

#猜數字游戲

import random   #impor語句導入random模塊

guessor=0;
print("#"*30)   #輸出30個”#“(”##############################“)用於裝飾輸出代碼,同下
print("hello! \nwhat is you name?")    #“\n”換行
print("#"*30)
Myname=input("please enter your name:")
print("*"*30)
print("hello {} \nlet's play game!".format(Myname))
print("*"*30)
number=random.randint(1,20) #調用random函數中的randit函數(用於生成指定范圍的隨機數)
print("well~"+Myname+",please guess the number between 1 and 2.\n")

print("You have six guesses.\n")


while guessor<=6:   #while循環(循環到滿足條件為止)
    print("tack a guess")
    guess=input("please enter your number:")
    print("^"*30)
    guess=int(guess)

    guessor=guessor+1

    if guess==number:
        print("good! your are right")
        break
    if guess<number:
        print("\ntoo low")
        print("%"*30)
    if guess>number:
        print("\ntoo big")
        print("%"*30)
print("\nYou've run out of guesses!")

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM