我正在用python编写一个hangman程序,我试图打印用户一直猜测的字母,但我不知道怎么做,似乎什么都不起作用。我尝试了一个函数和一个for循环,但它没有打印任何东西,在这一点上我不知道如何做它。这是我目前的代码:
secretword=""
currentword=""
cw=""
hangcount=0
def hanging():
if hangcount==1:
print ("----------")
print ("| |")
print ("| ---")
print ("| | |")
print ("| ---")
elif hangcount==2:
print ("----------")
print ("| |")
print ("| ---")
print ("| | |")
print ("| ---")
print ("| |")
print ("| |")
elif hangcount==3:
print ("----------")
print ("| |")
print ("| ---")
print ("| | |")
print ("| ---")
print ("| |")
print ("| |")
print ("| /")
elif hangcount==4:
print ("----------")
print ("| |")
print ("| ---")
print ("| | |")
print ("| ---")
print ("| |")
print ("| |")
print ("| //")
elif hangcount==5:
print ("----------")
print ("| |")
print ("| ---")
print ("| | |")
print ("| ---")
print ("| /|")
print ("| |")
print ("| //")
elif hangcount==6:
print ("----------")
print ("| |")
print ("| ---")
print ("| | |")
print ("| ---")
print ("| /|/")
print ("| |")
print ("| //")
def clearscreen():
for i in range(50):
print
def displayCurrentword():
global currentword
cw=""
for ch in currentword:
cw=cw+ch+" "
print (cw)
def initCurrentword():
global currentword
currentword=""
for ch in secretword:
currentword=currentword+"_"
def find_replace(letter):
global currentword
found=False
cw=""
for i in range(0,len(secretword)):
if secretword[i]==letter:
found=True
cw=cw+letter
else:
cw=cw+currentword[i]
currentword=cw
if found==False:
global hangcount
hangcount+=1
print ("Welcome to Hangman!")
print
secretword=raw_input("Enter word:")
secretword=secretword.lower()
initCurrentword()
while (currentword!=secretword and hangcount<6):
clearscreen()
hanging()
displayCurrentword()
letter=raw_input("Enter a letter:")
letter=letter.lower()
find_replace(letter)
displayCurrentword()
if currentword==secretword:
print ("Congrats you win!")
else:
clearscreen()
print ("----------")
print ("| |")
print ("| ---")
print ("| | |")
print ("| ---")
print ("| /|/")
print ("| |")
print ("| //")
print ("You killed him!")
print ("The word was %s"%(secretword))
如果还有什么我可以改变或改进的,请告诉我。 另外,当我试着把这个斜杠“\”当作一只胳膊和一条腿时,它不允许我这样做,它给了我一个错误,我不知道如何修复它。你知道吗
你真的很接近,我认为最简单的解决方法是:
letters
\\
形成左臂和左腿我包括了一些其他的建议和评论。
希望这有帮助。你知道吗
相关问题 更多 >
编程相关推荐