我確實需要幫助來修復此代碼。基本上,這是一個hang子手遊戲,用戶可以猜測字母或單詞。(Python)幫助修復Hang子手遊戲
如果用戶輸入一個字母,它將像普通的hang子手遊戲一樣工作。然而,最後的機會(猜測#6)必須是一個詞。如果用戶在猜測#6之前使用了字母猜測並得到了正確的結果,用戶就不會猜測這個單詞。
用戶可以事先猜出這個單詞,但如果它錯了,他們將失去兩次機會。所以如果他們一開始就猜錯詞,他們將有4次機會,但最後的機會仍然是猜測。
編輯:該程序現在只有5個字母猜測是對還是錯,但最終的猜測仍然是一個單詞猜測,我們仍然有可選的早期單詞猜測。櫃檯現在似乎在工作,但我不確定如何區分這些單詞。 (編輯:我意識到還有一個問題要附加單詞。)
這是我當前的代碼。 我改變了它的部分(計數器< 5)在用戶猜測。 (計數器== 5)的部分與舊的代碼相似。
import random
wordlist = 'artist breeze circle decent enroll filthy growth honest invest kernel letter narrow meteor policy pursue roster runway scheme ripple toddle wobbly zeroes'.upper().split()
random.shuffle(wordlist)
counter = 0
def draw_board():
#Display words here
for i in secret_word:
if i in correct:
print(i, end=' ')
else:
print('_', end=' ')
print("\n")
print("*** MISSES ***")
for i in incorrect:
print(i, end=' ')
print('\n*********************')
def user_guess():
#For user to input guess
global counter
secret_word = wordlist.pop()
while(counter < 5):
guess = input("Guess a letter or word\n: ").upper()
if(len(guess) > 1):
guess_list = list(guess)
if(guess_list == secret_list):
correct.append(guess_list)
else:
counter = counter + 1
elif guess in secret_word:
correct.append(guess)
else:
incorrect.append(guess)
return counter
if(counter == 5):
wordguess = input("Enter your word\n: ").upper()
if(guess == secret_word):
correct.append(wordguess)
else:
counter = counter + 1
print(counter)
return counter
def check_win():
#Check if user has won or not
global counter
if(counter > 5):
return 'loss'
for i in secret_word:
if i not in correct:
return 'no win'
return 'win'
#pop is used to retrieve a word from word list
secret_word = wordlist.pop()
secret_list = list(secret_word)
correct = []
incorrect = []
print("DEBUG: %s" % secret_word)
while True:
draw_board()
user_guess()
counter = counter + 1
win_condition = check_win()
if win_condition == 'loss':
print("You lose!")
break
elif win_condition == 'win':
print("You win!")
代碼的具體問題是什麼?它出錯了哪裏? –
@Peter當用戶輸入一個單詞時,程序不會檢測到,所以即使用戶輸入正確的單詞,它也會繼續遊戲。儘管機會超過6,它也會持續不斷,所以在櫃檯或邏輯上肯定存在缺陷。對不起,沒有正確處理這個問題。我編輯了這個問題。 – Sar