刽子手游戏Python

2024-04-26 23:03:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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))

如果还有什么我可以改变或改进的,请告诉我。 另外,当我试着把这个斜杠“\”当作一只胳膊和一条腿时,它不允许我这样做,它给了我一个错误,我不知道如何修复它。你知道吗


Tags: inforifdefchglobalprintcw
1条回答
网友
1楼 · 发布于 2024-04-26 23:03:21

你真的很接近,我认为最简单的解决方法是:

  • 添加全局变量列表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))

相关问题 更多 >