<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                在講述[有關list的時候,提到做游戲的事情](https://github.com/qiwsir/ITArticles/blob/master/BasicPython/115.md),后來這個事情一直沒有接續。不是忘記了,是在想在哪個階段做最合適。經過一段時間學習,看官已經不是純粹小白了,已經屬于python初級者了。現在就是開始做那個游戲的時候了。 ## [](https://github.com/qiwsir/ITArticles/blob/master/BasicPython/129.md#游戲內容猜數字游戲)游戲內容:猜數字游戲 太簡單了吧。是的,游戲難度不大,不過這個游戲中蘊含的東西可是值得玩味的。 ### 游戲過程描述 1. 程序運行起來,隨機在某個范圍內選擇一個整數。 2. 提示用戶輸入數字,也就是猜程序隨即選的那個數字。 3. 程序將用戶輸入的數字與自己選定的對比,一樣則用戶完成游戲,否則繼續猜。 4. 使用次數少的用戶得勝. ## [](https://github.com/qiwsir/ITArticles/blob/master/BasicPython/129.md#分析)分析 在任何形式的程序開發之前,不管是大還是小,都要進行分析。即根據功能需求,將不同功能點進行分解。從而確定開發過程。我們現在做一個很小的程序,也是這樣來做。 ### 隨機選擇一個數 要實現隨機選擇一個數字,可以使用python中的一個隨機函數:random。下面對這個函數做簡要介紹,除了針對本次應用之外,還擴展點,也許別處看官能用上。 還是要首先強化一種學習方法,就是要學會查看幫助文檔。 ~~~ >>> import random #這個是必須的,因為不是內置函數 >>> dir(random) ['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', 'WichmannHill', '_BuiltinMethodType', '_MethodType', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_acos', '_ceil', '_cos', '_e', '_exp', '_hashlib', '_hexlify', '_inst', '_log', '_pi', '_random', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'division', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'jumpahead', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate'] >>> help(random.randint) Help on method randint in module random: randint(self, a, b) method of random.Random instance Return random integer in range [a, b], including both end points. ~~~ 耐心地看文檔,就明白怎么用了。不過,還是把主要的東西列出來,但仍然建議看官在看每個函數的使用之前,在交互模式下通過help來查看文檔。 **隨機整數:** ~~~ >>> import random >>> random.randint(0,99) 21 ~~~ **隨機選取0到100間的偶數:** ~~~ >>> import random >>> random.randrange(0, 101, 2) 42 ~~~ **隨機浮點數:** ~~~ >>> import random >>> random.random() 0.85415370477785668 >>> random.uniform(1, 10) 5.4221167969800881 ~~~ **隨機字符:** ~~~ >>> import random >>> random.choice('qiwsir.github.io') 'g' ~~~ **多個字符中選取特定數量的字符:** ~~~ >>> import random random.sample('qiwsir.github.io',3) ['w', 's', 'b'] ~~~ **隨機選取字符串:** ~~~ >>> import random >>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] ) 'lemon' ~~~ **洗牌:**把原有的順序打亂,按照隨機順序排列 ~~~ >>> import random >>> items = [1, 2, 3, 4, 5, 6] >>> random.shuffle(items) >>> items [3, 2, 5, 6, 4, 1] ~~~ 有點多了。不過,本次實驗中,值用到了random.randint()即可。多出來是買一送一的(哦。忘記了,沒有人買呢,本課程全是白送的)。 關鍵技術點之一已經突破。可以編程了。再梳理一下流程。畫個圖展示: (備注:這里我先懶惰一下吧,看官能不能畫出這個程序的流程圖呢?特別是如果是一個初學者,流程圖一定要自己畫哦。剛才看到網上一個朋友說自己學編程,但是邏輯思維差,所以沒有學好。其實,畫流程圖就是幫助提高邏輯思維的一種好方式,請畫圖吧。) 圖畫好了,按照直觀的理解,下面的代碼是一個初學者常常寫出來的(老鳥們不要噴,因為是代表初學者的)。 ~~~ #!/usr/bin/env python #coding:utf-8 import random number = random.randint(1,100) print "請輸入一個100以內的自然數:" input_number = raw_input() if number == int(input_number): print "猜對了,這個數是:" print number else: print "錯了。" ~~~ 上面的程序已經能夠基本走通,但是,還有很多缺陷。 最明顯的就是只能讓人猜一次,不能多次。怎么修改,能夠多次猜呢?動動腦筋之后看代碼,或者看官在自己的代碼上改改,能不能實現多次猜測? 另外,能不能增強一些友好性呢,讓用戶知道自己輸入的數是大了,還是小了。 根據上述修改想法,新代碼如下: ~~~ #!/usr/bin/env python #coding:utf-8 import random number = random.randint(1,100) print "請輸入一個100以內的自然數:" input_number = raw_input() if number == int(input_number): print "猜對了,這個數是:" print number elif number > int(input_number): print "小了" input_number = raw_input() elif number < int(input_number): print "大了" input_number = raw_input() else: print "錯了。" ~~~ 嗯,似乎比原來進步一點點,因為允許用戶輸入第二次了。同時也告訴用戶輸入的是大還是小了。但,這也不行呀。應該能夠輸入很多次,直到正確為止。 是的。這就要用到一個新的東西:循環。如果看官心急,可以google一下while或者for循環,來進一步完善這個游戲,如果不著急,可以等等,隨后我也會講到這部分。 這個游戲還沒有完呢,即使用了循環,后面還會繼續。
                  <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>

                              哎呀哎呀视频在线观看