回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在为一个学校项目创建一个简单的Hangman实现,我目前的任务是让一个字母在猜对的时候在单词中显示出来。我已经有了根据单词中字母的数量生成空格的代码,以及我所需要的几乎所有其他游戏组件,但是我不知道如何用正确的字母替换空格。</p>
<p>如果你能简单地解释一下,我将不胜感激,因为我对编程还很陌生。如果可能的话,这样我就不用修改太多代码了。</p>
<p>这是我的代码:</p>
<pre><code>import random
name = str(input("What's your name?"))
print("Hello,", name + "!")
failures = 0
allowed = 1
guessed = str()
wordlist = ['hangman', 'dinner', 'computer', 'america', 'olympics', 'football', 'minecraft', 'jacket', 'cabbage', 'electricity', 'dog',
'pasta', 'japan', 'water', 'programming', 'anaconda', 'onehunga', 'name', 'windows', 'curtains', 'bieber', 'kirito',
'montenegro', 'wheel', 'civilization', 'physics', 'bluebird' 'table', 'ACDC', 'guardian yam' 'mario', 'parachute', 'agario', 'obama',
'youtube', 'putin', 'dairy', 'christianity', 'club penguin', 'oskahlavistah', 'coins', 'agitating', 'jumping', 'eating',
'your mom', 'executive', 'car', 'jade', 'abraham', 'sand', 'silver', 'uranium', 'oscar is gay', 'bioshock', 'fizzle', 'moonman', 'watermelon',
'WAHAHAHAHAHA', 'steve jobs', 'extreme', 'weeaboo jones', 'hot damn', name]
def correct(guess):
if guess in word:
if guess not in guessed:
print("Correct")
return(True)
else:
if guess not in word and len(guess) == 1 and guess in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ':
if guess not in guessed:
print("Incorrect!")
return(False)
print("Guess this word!")
print("You can input any letter from A to Z and the space key.")
wordnumber = random.randint(0, len(wordlist))
word = (wordlist[wordnumber])
print("_ "*len(word))
while failures < allowed:
guess = str(input("Guess a letter!"))
if len(guess) != 1 or guess not in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ':
print("That is not a letter, try again.")
if guess in guessed:
print("You have already guessed that letter, try again.")
iscorrect = correct(guess)
guessed = guessed, guess
if iscorrect == True:
print("Word display still in development")
if iscorrect == False:
print("You suck")
failures = failures+1
print("You have", allowed , "guesses left.")
if failures == allowed:
replay = str(input("Press 1 to play again, press 2 to exit."))
if replay == 1:
break
else:
quit()
#Now all I have to do is find a way to display positions of correct letters.
</code></pre>