【问题标题】:I am having a problem with a recurring function in Python我在 Python 中遇到了重复函数的问题
【发布时间】:2019-03-08 04:22:00
【问题描述】:
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()

是的,我知道目前没有摆脱循环的方法,但我正在努力解决这个问题,我不知道这是否是问题,希望不是。如果有人有解决方案,我对大多数代码持开放态度。虽然我只有大约一年的经验,所以没有什么特别复杂的。谢谢!

【问题讨论】:

  • 为什么你的function名字和variable名字一样?
  • 为 fn 广告变量尝试不同的名称

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


【解决方案1】:

您可以更改函数名称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()

【讨论】:

    猜你喜欢
    • 2018-05-19
    • 1970-01-01
    • 2021-11-29
    • 2020-08-22
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    相关资源
    最近更新 更多