2013-12-22 93 views
0

我正在一個文字遊戲工作。用戶的目的是在5次嘗試中猜測5個字母的單詞。用戶可以知道第一個字母。如果他沒有得到正確的單詞,但是如果他在正確的地方有一封信,他就會知道這一點。蟒蛇改善文字遊戲

這是我的代碼:

import random 
list_of_words = ["apple","table", "words", "beers", "plural", "hands"] 
word = random.choice(list_of_words) 

attempts = 5 

for attempt in range(attempts): 
    if attempt == 0: 
     tempList = list(word[0] + ("." * 4)) 
     print("The first letter of the word we are looking for: %s" % "".join(tempList)) 

    answer = raw_input("What is the word we are looking for?:") 
    if len(answer) != 5: 
     print ('Please enter a 5 letter word') 

    Else: 
     if answer != word: 
      wordlist = list(word) 
      answerlist = list(answer) 
      for i in range(min(len(wordlist), len(answerlist))): 
       if wordlist[i] == answerlist[i]: 
        tempList[i] = wordlist[i] 
      print(tempList) 

     else: 
      print("correct, you have guessed the word in:", attempt, "attempts") 

if answer != word: 
    print("Sorry maximum number of tries, the word is: %s" % word) 

我對這個代碼的兩個問題:

第一個是一個小問題:如果用戶給出一個6或4個字母的單詞,它仍然會打印這個單詞。雖然我寧願讓這個詞被忽略,並且這個嘗試沒有被使用。

如果一個字母給出正確(也是第一個字母),它就不會成爲反饋的標準部分。試圖得到這個溫度,但它的工作效果不好。

任何建議來清理我的代碼也感激!

感謝您的關注

回答

1

我對代碼做了一些修改,現在它按照你的規範工作了。我還寫了幾條解釋性評論:

import random 

list_of_words = ["apple", "table", "words", "beers", "plural", "hands"] 
word = random.choice(list_of_words) 

# changed the loop to a 'while', because i don't want to count the invalid length answers 
# and wanted to exit the loop, when the user guessed correctly 
attempts = 5 
attempt = 0 
correct = False 
while attempt < attempts and not correct: 
    if attempt == 0: 
     # i stored a working copy of the initial hint (ex: "w....") 
     # i'll use this to store the previously correctrly guessed letters 
     tempList = list(word[0] + ("." * 4)) 
     print("The first letter of the word we are looking for: %s" % "".join(tempList)) 

    answer = raw_input("What is the word we are looking for?:") 
    if len(answer) != 5: 
     print("Please enter a 5 letter word") 
    else: 
     if answer != word: 
      # i simplified this loop to comparing the wordlist and answerlist and update templist accordingly 
      wordlist = list(word) 
      answerlist = list(answer) 
      for i in range(min(len(wordlist), len(answerlist))): 
       if wordlist[i] == answerlist[i]: 
        tempList[i] = wordlist[i] 
      print(tempList) 
     else: 
      correct = True 
      print("Correct, you have guessed the word in %s attempts" % (attempt + 1)) 
     attempt += 1 

if answer != word: 
    # also i used string formatting on your prints, so is prints as a string, and not as a tuple. 
    print("Sorry maximum number of tries, the word is: %s" % word) 
+0

@ zord。哇,這是偉人,非常感謝!我的第一個版本也包含「while」,但後來改變了。 '最後猜測','效果更好。謝謝,我很感激!歡呼聲 – user3119123

+0

歡迎您! :) – zord

1

代碼有幾個問題。

現在只有1個。我注意到在樣本輸出中,您輸入了五個字母詞(beeds和bread),它仍然打印出Please enter a 5 letter word

這兩條線:

if len(answer) != 4: 
    print ('Please enter a 5 letter word') 

當然這應該是:

if len(answer) != 5: 
    print ('Please enter a 5 letter word') 
    continue 

這會趕上輸入無效,並再次去圓循環。

1

回答您的具體問題:

  1. 你需要讓你的input圍繞for循環,保持用戶在該循環,直到他們進入適當的長度
  2. 的話如果你移動猜字母正確的地方,通過猜測"abcde"然後"fghij"等來贏得微不足道。你需要仔細考慮你的規則是什麼;你可以有一個單獨的列表「在猜中的字母在答案中,但在錯誤的地方」並向用戶顯示。
  3. 要保留所有以前猜到的字符的顯示版本,請保留顯示字符列表:display = ["." for letter in answer],並隨時更新。

其他問題,您有:

  1. 太多字長(尤其是len("plural") != 5)的硬編碼;你應該把你的代碼改寫成使用這個詞的長度(這使得它更加靈活)。
  2. 你只告訴用戶他們已經贏得了,如果他們猜測整個答案。如果他們用重疊的字母來看它會怎麼樣?你可以測試if all(letter != "." for letter in display):看他們是否有這樣的答案。
  3. 您的列表理解[i for i in answer if answer in word]永遠不會分配給任何東西。