私はハングマンゲームを作っています。私は言葉を繰り返し、手紙のすべての繰り返しを私のリストに追加しようとしています。たとえば「hello」という単語:ユーザーが「l」を入力すると、すべてのリストがリストに追加されます。今は1つの "l"しか見つけられません。ユーザーが "l"をもう一度入力すると、2番目の "l"が見つかります。Python:Hang Man game
私はまた、彼らは以前にすでにそれを入力した場合、ユーザは、別の文字を入力することができないようにしたい。
私は二つのリストに正しい推測とすべての推測を保存間違った推測のための1つを持っています。たとえば、ユーザーが「hello」で「h」を入力した場合
「h」は正しい推測であるため、[h]に追加されますが、「h」にもう一度入力するとリストに追加されます["h"、 "h"]。間違ったボックスは、同じ方法で動作しますが、間違った単語に対しては動作します。 "hello"という単語に "z"を入力すると、間違ったボックスに["z"]と表示されます。ここで
は私のコードです:
import random
user_input = ""
turns = 5
print("Welcome to Advanced Hang Man!")
print("Use your brain to unscramble the word without seeing its order!")
words = ["hello","goolge","czar","gnat","relationship","victor","patric","gir","foo","cheese"]
# Picks a random word from the list and prints the length of it
random_word = (random.choice(words))
random_word_legnth = (len(random_word))
print("Hint! The length of the word is",random_word_legnth)
hold_random_word = [i for i in random_word]
while turns != 0 and set(right_guess) != set(hold_random_word):
user_input = input("Please type your guess one letter at a time:")
right_guess = []
wrong_guess = []
#Calculating every input
if len(user_input) == 1 and user_input.isalpha():
for i in user_input:
if i in hold_random_word:
right_guess.append(i)
else:
wrong_guess.append(i)
print("Correct guess", ''.join(right_guess))
print("Wrong guess", ''.join(wrong_guess))
を説明することができます。それで十分ですか? –
@CurrentlyVictorはいくつかの提案を追加しました。私はこれが助けてくれることを願っています。 – Aditya