【发布时间】:2015-11-20 05:18:55
【问题描述】:
我正在为一个学校项目创建一个简单的 Hangman 实现,我目前的任务是让一个字母在正确猜测的情况下显示在单词中。我已经有代码可以根据单词中的字母数量以及我需要的游戏的几乎所有其他组件生成空格,但我不知道如何用正确的字母替换空格。
如果你能保持简单和解释,我将不胜感激,因为我在编程方面还是新手。如果可能的话,这样我就不必过多地更改我的代码了。
这是我的代码:
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.
【问题讨论】:
-
如果你能给出你所拥有的
string,你想替换什么,输出什么而不是整个程序会更容易 -
我给出整个代码的原因是我们可以找出一个不会影响代码的答案。无论如何,我拥有的字符串是 wordnumber = random.randint(0, len(wordlist)) word = (wordlist[wordnumber]) print("_ "*len(word))
-
如果我知道如何格式化那将是很棒的 xD
-
欢迎新用户!如果您对我的回答感到满意,请接受并点赞。