<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Python 3:猜數字 > 原文: [https://javabeginnerstutorial.com/python-tutorial/python-3-guess-the-number-2/](https://javabeginnerstutorial.com/python-tutorial/python-3-guess-the-number-2/) 現在,讓我們編寫一個腳本,該腳本實現一個基本的“猜數字”游戲。 游戲規則為: * 用戶選擇是否要猜測 1 到 100 或 1 到 1000 之間的數字 * 根據數字范圍,用戶擁有固定數目的猜測 * 應用生成一個數字進行猜測 * 用戶輸入一個數字 * 如果這是秘密號碼,則應用祝賀用戶并詢問他/她是否想再玩一輪 * 否則,應用會告訴用戶密碼是否小于或大于提供的密碼 * 猜測數增加 * 如果用戶用盡了所有猜測,則應用告訴他/她的秘密號碼,并詢問用戶是否要再玩一輪 如您所見,規則很簡單,但實現起來似乎很復雜。 由您決定如何處理錯誤的輸入類型(無數字)。 好吧,這并不像聽起來那樣困難。 讓我們看一下我的解決方案的一些示例輸出: ```py Should the secret number between 1 and 100 or 1 and 1000? 100 You have chosen 100, you will have 7 guesses to find the secret number. I have chosen the secret number... What's your guess? 34 The secret number is higher... What's your guess? 54 The secret number is higher... What's your guess? 66 Congrats, you have Won! The secret number was 66 Do you want to play another round? (yes / no) yes Should the secret number between 1 and 100 or 1 and 1000? 1000 You have chosen 1000, you will have 10 guesses to find the secret number. I have chosen the secret number... What's your guess? 500 The secret number is lower... What's your guess? 400 The secret number is lower... What's your guess? 300 The secret number is higher... What's your guess? 350 The secret number is higher... What's your guess? 375 The secret number is lower... What's your guess? 370 The secret number is lower... What's your guess? 360 The secret number is higher... What's your guess? 365 The secret number is lower... What's your guess? 364 The secret number is lower... What's your guess? 363 The secret number is lower... Sorry, you lose. The secret number was 362 Do you want to play another round? (yes / no) no ``` 如您所見,當猜測用完時,應用不會停止,并且如果用戶獲勝或失敗,它會顯示一條消息。 讓我們看一下代碼。 ```py __author__ = 'GHajba' import random while True: while True: try: max_number = int(input('Should the secret number between 1 and 100 or 1 and 1000? ')) except ValueError: print("This was not a number!") continue if max_number != 100 and max_number != 1000: continue else: break if max_number == 100: guess_count = 7 else: guess_count = 10 print('You have chosen {}, you will have {} guesses to find the secret number.'.format(max_number, guess_count)) secret_number = random.randint(1, max_number) print('I have chosen the secret number...') guesses = 0 while guess_count - guesses: try: guesses += 1 guessed = int(input("What's your guess? ")) except ValueError: continue if guessed == secret_number: print('Congrats, you have Won!') break elif guessed > secret_number: print('The secret number is lower...') else: print('The secret number is higher...') else: print("Sorry, you lose.") print("The secret number was ", secret_number) answer = '' while answer.lower() not in ['yes', 'no', 'y', 'n']: answer = input("Do you want to play another round? (yes / no) ") if 'no' == answer or 'n' == answer: break ``` 如您所見,代碼非常繁瑣,因為我們包含許多循環來驗證輸入并處理主游戲循環。 也許您以后會發現此代碼根本不可讀。 為了解決這個問題,我們將學習函數,然后重構這部分代碼以使用函數。 但是,此腳本也有一些替代解決方案。 例如,您可以將中間的`while`循環更改為`for`循環: ```py for guesses in range(guess_count): ``` 在這種情況下,循環遍歷猜測范圍,如果到達最后一個數字,則循環結束。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看