<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          python實(shí)現(xiàn)簡單猜單詞游戲

          共 2278字,需瀏覽 5分鐘

           ·

          2020-12-29 12:42

          本文實(shí)例為大家分享了python實(shí)現(xiàn)猜單詞游戲的具體代碼,供大家參考,具體內(nèi)容如下

          電腦根據(jù)單詞列表隨機(jī)生成一個單詞,打印出這個單詞長度個 ‘ _ ' ,玩家隨機(jī)輸入一個這個單詞可能包含的英文字母,如果玩家猜對了,電腦則會在正確的空格處填寫這個字母,如果沒有猜對,游戲次數(shù)就減一。如果玩家在游戲次數(shù)減為零前猜對這個單詞的所有字母,則玩家獲勝,否則玩家輸?shù)舯荣悺?/p>

          1. from?random?import*

          2. words?=?'tiger lion wolf elephant zebra ducksheep rabbit mouse'.split()

          3. #得到要猜的神秘單詞

          4. def?getWord(wordList):

          5. n?=?randint(0,len(wordList)-1)

          6. return?wordList[n]

          7. #游戲界面

          8. def?display(word,wrongLetters,rightLetters,chance):

          9. print('你還有{:n}次機(jī)會'.format(chance).center(40,'-'))

          10. print('已經(jīng)猜錯的字母:'+?wrongLetters)

          11. print()

          12. blanks?=?'_'*len(word)

          13. for?i?in?range(len(word)):

          14. if?word[i]?in?rightLetters:

          15. blanks?=?blanks[:i]?+?word[i]?+blanks[i+1:]

          16. for?i?in?blanks:

          17. print(i+' ',end='')

          18. print()

          19. print()

          20. #從玩家的輸入得到一個猜測的字母

          21. def?getLetter(alreadyGuessed):

          22. while?True:

          23. print('請輸入一個可能的字母:')

          24. guess?=?input()

          25. guess?=?guess.lower()

          26. if?guess[0]?in?alreadyGuessed:

          27. print('你已經(jīng)猜過這個字母了!')

          28. elif?guess[0]?not?in?'qwertyuiopasdfghjklzxcvbnm':

          29. print('請輸入一個英文字母!(a-z)')

          30. else:

          31. return?guess[0]

          32. #是否再玩一次

          33. def?playAgain():

          34. print('是否在玩一次?(y/n)')

          35. s?=?input()

          36. s?=?s.lower()

          37. if?s[0]?==?'y':

          38. return?1

          39. return?0

          40. #游戲初始化

          41. wrongLetters?=?''

          42. rightLetters?=?''

          43. word?=?getWord(words)

          44. chance?=?6?#初始為6次機(jī)會

          45. done?=?False

          46. while?True:

          47. display(word,wrongLetters,rightLetters,chance)

          48. guess?=?getLetter(wrongLetters+rightLetters)

          49. if?guess?in?word:

          50. rightLetters?=?rightLetters+?guess

          51. foundAll?=?True

          52. for?i?in?range(len(word)):

          53. if?word[i]?not?in?rightLetters:

          54. foundAll?=?False

          55. break

          56. if?foundAll:

          57. print('你真棒,這個單詞就是'+?word?+',你贏了!')

          58. done?=?True

          59. else:

          60. wrongLetters?=?wrongLetters?+?guess

          61. chance?=?chance?-?1

          62. if?chance?==?0:

          63. display(word,wrongLetters,rightLetters,chance)

          64. print("你已經(jīng)沒有機(jī)會了!你一共猜錯了"+str(len((wrongLetters))+"次,猜對了"+str(len(rightLetters))+"次,正確的單詞是:"+?word)

          65. done?=?True

          66. if?done:

          67. if?playAgain():

          68. wrongLetters?=?''

          69. rightletters?=?''

          70. word?=?getWord(words)

          71. chance?=?6?#初始為6次機(jī)會

          72. done?=?0

          73. else:

          74. break

          再為大家提供一段代碼:python猜單詞游戲,作為補(bǔ)充,感謝原作者的分享。

          1. import?random

          2. WORDS?=?("math","english","china","history")

          3. right?=?'Y'

          4. print("歡迎參加猜單詞游戲!")

          5. while?right=='Y'?or?right=='y':

          6. word=random.choice(WORDS)

          7. correct=word

          8. newword?=?''

          9. while?word:

          10. pos=random.randrange(len(word))

          11. newword+=word[pos]

          12. #將word單詞下標(biāo)為pos的字母去掉,取pos前面和后面的字母組成新的word

          13. word?=?word[:pos]+word[(pos+1):]?#保證隨機(jī)字母出現(xiàn)不會重復(fù)

          14. print("你要猜測的單詞為:",newword)

          15. guess?=?input("請輸入你的答案:")

          16. count=1

          17. while?count<5:

          18. if?guess!=correct:

          19. guess?=?input("輸入的單詞錯誤,請重新輸入:")

          20. count+=1

          21. else?:

          22. print("輸入的單詞正確,正確單詞為:",correct)

          23. break

          24. if?count?==?5:

          25. print("您已猜錯5次,正確的單詞為:",correct)

          26. right?=?input("是否繼續(xù),Y/N:")

          以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助

          1. 歡迎大家點(diǎn)贊,留言,轉(zhuǎn)發(fā),轉(zhuǎn)載,感謝大家的相伴與支持

            萬水千山總是情,點(diǎn)個【在看】行不行

          2. *聲明:本文于網(wǎng)絡(luò)整理,版權(quán)歸原作者所有,如來源信息有誤或侵犯權(quán)益,請聯(lián)系我們刪除或授權(quán)事

          瀏覽 52
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  高潮毛片| 依人在线大香蕉 | 午夜小网站 | 潮喷在线 | 久久色国产在线 |