我在python中编写一个刽子手程序,我正在尝试打印用户猜测的字母,但我无法弄清楚如何做,似乎没有任何工作。我尝试了一个函数和一个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))
如果还有什么我可以改变或改进让我知道。 此外,当我试图把这个斜线“\”作为一条胳膊和一条腿它不会让我,它给了我一个错误,我不知道如何解决这个问题。
答案 0 :(得分:0)
你真的很接近,我认为这是解决问题的最简单方法:
letters
\\
以形成左臂和腿我还提出了一些其他建议以及评论 希望这会有所帮助。
secretword=""
currentword=""
cw=""
hangcount=0
letters=[] # declare letters list
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 ("| / \\") # double backslashes \\ for left leg
elif hangcount==5:
print ("----------")
print ("| |")
print ("| ---")
print ("| | |")
print ("| ---")
print ("| /|")
print ("| |")
print ("| / \\") # double backslashes
elif hangcount==6:
print ("----------")
print ("| |")
print ("| ---")
print ("| | |")
print ("| ---")
print ("| /|\\") # double backslashes for left arm
print ("| |")
print ("| / \\") # and left leg
def clearscreen():
for i in range(50):
print('\n')
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
clearscreen()
print ("Welcome to Hangman!")
print
secretword=raw_input("Enter word:")
secretword=secretword.lower()
initCurrentword()
while (currentword!=secretword and hangcount<6):
global letter # use global letters list
clearscreen()
if len(letters) != 0:
# display guessed letters (set() to remove duplicates)
print("Guessed letters:", ', '.join(set(letters)))
if hangcount == 0:
print ("\n" * 4) # empty space for hangman (for consitent layout)
hanging()
displayCurrentword()
letter=raw_input("Enter a letter:")
letter=letter.lower()
letters.append(letter) # add current letter to letters list
find_replace(letter)
displayCurrentword()
if currentword==secretword:
print ("Congrats you win!")
else:
clearscreen()
print("Guessed letters:", ', '.join(set(letters))) # display guessed letters
print ("----------")
print ("| |")
print ("| ---")
print ("| | |")
print ("| ---")
print ("| /|\\") # double backslashes
print ("| |")
print ("| / \\") # double backslashes
print ("You killed him!")
print ("The word was %s"%(secretword))