2017-06-21 42 views
4

前天我剛剛開始編寫代碼(這讓我成爲一個嚴重的新手),並且我想我會嘗試製作自己的Hang子手遊戲,但是以及我只是不知道我到目前爲止做了什麼錯誤!^_ ^這就是它:初學Hang子手遊戲:不能讓它工作

L = ["cat", "dog", "rabbit"] 
from random import randrange 
random_index = randrange(0,len(L)) 
w = L[random_index] 
W = list(w) 
a = input() 
tries = 1 
print(W) 
while len(W) != 0 and tries<10: 
    if a in W: 
     print("yes") 
     W.remove(a) 
     tries += 1 
     a = input() 
    elif a not in W: 
     print("no") 
     tries += 1 
     a = input() 
else: 
    if len(W) == 0: 
     print("Well done! Your word was") 
     print(w) 
    elif tries == 10: 
     print("You died!") 

我認爲這個問題是來自我的循環啄「而LEN(W)!= 0」,因爲一切都很好與輸入部分,它只是不停止的時候這應該! (這意味着什麼時候應該沒什麼可猜的了!) 所以我希望有人能夠很好地浪費他今天的兩分鐘幫助我解決基本不太有趣的問題!提前致謝!

+0

您需要實際描述發生了什麼(什麼你預計會發生)。你也應該嘗試先自己調試它(例如:在懷疑錯誤的地方添加'print'語句) – UnholySheep

+0

雖然不是最好的解決方案,但你可以在while循環的exit語句中添加一個break。 ..我傾向於避免在一個while循環中使用多個邏輯語句。再次,可能不是最好的解決方案,但... – Doug

+3

你的代碼應該在每個循環的開始處要求一個新的輸入,而不是結束。 –

回答

0

當你猜到前一個循環結尾的最後一個字母時,當前循環會告訴你猜測是正確的,然後再詢問另一個字母。嘗試更多的東西像這樣

L = ["cat", "dog", "rabbit"] 
from random import randrange 
random_index = randrange(0,len(L)) 
w = L[random_index] 
W = list(w) 
tries = 0 
print(W) 
while len(W) != 0 and tries<10: 
    a = input() 
    if a in W: 
     print("yes") 
     W.remove(a) 
     tries += 1 
    elif a not in W: 
     print("no") 
     tries += 1 
else: 
    if len(W) == 0: 
     print("Well done! Your word was") 
     print(w) 
    elif tries == 10: 
     print("You died!") 
+0

您可以刪除'elif'並使用'else'。並且'try + = 1'對於'if'和'else'都可能是共同的 – kuro

+0

非常感謝!我的a = input()只是錯位!^_^ – HaighatheGoose

+0

@ kuro是的,但這些都不是真正的邏輯變化,我不想混淆我的回答 –

1
  • ,你可以有一個以上的字母

  • random.choice(L)的變量名是很容易,L[random.randrange(len(L))]

所以

from random import choice 

def show_word(target_word, remaining_letters, blank="-"): 
    print("".join(blank if ch in remaining_letters else ch.upper() for ch in target_word)) 

words = ["cat", "dog", "rabbit"] 
target_word = choice(words) 
remaining_letters = set(target_word) 

print("Let's play Hangman!") 

for round in range(1, 11): 
    show_word(target_word, remaining_letters) 
    guess = input("Guess a letter: ").strip().lower() 
    if guess in remaining_letters: 
     print("You got one!") 
     remaining_letters.remove(guess) 
     if not remaining_letters: 
      break 
    else: 
     print("Sorry, none of those...") 

if remaining_letters: 
    print("You died!") 
else: 
    print("You solved {}! Well done!".format(target_word.upper())) 
+0

專家的方式來做到這一點,很好。加一! :d –

0

如果我沒有理解好你的問題,你可以解決你的問題是這樣,還記得你可以給信息用戶在輸入法添加一個提示:

L = ["cat", "dog", "rabbit"] 
from random import randrange 
random_index = randrange(0,len(L)) 
w = L[random_index] 
W = list(w) 
tries = 1 
print(W) 
while len(W) != 0 and tries<10: 
    a = input("select word") 
    if a in W: 
    print("yes") 
    W.remove(a) 
    tries += 1 
    else: 
    print("no") 
    tries += 1 

    if len(W) == 0: 
    print("Well done! Your word was") 
    print(w) 
    elif tries == 10: 
    print("You died!")