我在Python中的重复功能有问题

时间:2019-03-08 04:22:01

标签: python python-3.x function loops if-statement

Traceback (most recent call last):
  File "main.py", line 217, in <module>
    prompt()
  File "main.py", line 83, in prompt
    prompt()
TypeError: 'str' 

这是输入以下代码后出现的错误:

def prompt():
  print("What would you like to do?")
  prompt = input("")
  if prompt.upper() == "M":
    maps()
    prompt()
  elif prompt.upper() == "D":
    print("You go down.")
    spc()
    prompt()
  elif prompt.upper() == "U":
    print("You go up.")
    spc()
    prompt()
  elif prompt.upper() == "L":
    print("You go left.")
    spc()
    prompt()
  elif prompt.upper() == "R":
    print("You go right.")
    spc()
    prompt()
  else:
    print("You cannot do that.")
    spc()
    prompt()

是的,我知道目前没有办法摆脱循环,但是我正在努力解决这一问题,我不知道这是否是希望的问题。如果有人有解决方案,我将接受大多数代码。尽管我只有大约一年的经验,所以没有什么非常复杂的。谢谢!

1 个答案:

答案 0 :(得分:0)

您可以更改函数prompt的名称或变量prompt的名称。这样,您的问题就会得到解决。

def prmpt():
  print("What would you like to do?")
  prompt = input("")
  if prompt.upper() == "M":
    maps()
    prmpt()
  elif prompt.upper() == "D":
    print("You go down.")
    spc()
    prmpt()
  elif prompt.upper() == "U":
    print("You go up.")
    spc()
    prmpt()
  elif prompt.upper() == "L":
    print("You go left.")
    spc()
    prmpt()
  elif prompt.upper() == "R":
    print("You go right.")
    spc()
    prmpt()
  else:
    print("You cannot do that.")
    spc()
    prmpt()