1. 程式人生 > >猜數遊戲的程式碼和實驗文件中的說明,為了增加程式碼的複用性,將猜數字遊戲封裝為函式GuessSecret(maxtimes),將允許猜數字的最大次數maxtimes作為引數。在呼叫GuessSecret時

猜數遊戲的程式碼和實驗文件中的說明,為了增加程式碼的複用性,將猜數字遊戲封裝為函式GuessSecret(maxtimes),將允許猜數字的最大次數maxtimes作為引數。在呼叫GuessSecret時

from random import *

def GuessSecret(maxtimes):
    n=0;
    x = 0
    secret = randint(0, 100)
    print("——————————歡迎參加猜數字遊戲,遊戲現在開始————————————")
    while n<maxtimes and x!=secret:
        n+=1;
        x=int(input("數字區間0-100,請輸入你猜的數字:"))
        print("你輸入的數字是:",x)
        if x==secret:
            print("你猜了{}次,猜對了,真厲害".format(n))
        else:
            if(x>secret):
                print("太大了!")
            else:
                print("太小了!")
    print("遊戲結束")

maxts=eval(input("請輸入猜數字的最大次數:"))
#print(maxts)
GuessSecret(maxts)