【问题标题】:typeError: str object not callable-pythontypeError:str 对象不可调用-python
【发布时间】:2017-10-11 01:53:21
【问题描述】:
input=input("select the garment you purchased type 1 for shirt, 2 for pants, 3 for coats: ")

def color(call):
   if call==123:
      colorin=input("Enter your shirt colour, 1 for red, 2 for blue, 3 for green")
    elif call==345:
       colorin=input("Enter your pants colour 1 for black, 2 for blue, 3 for white")
    elif call==678:
       clorin=input("Enter your coat colour 1 for black, 2 for grey, 3 for skyblue")
    else:
       print("invalid")
    return colorin

def shirt(sh):
   if int(sh)==1:
      print("shirt red, cost=$5")
   elif int(sh)==2:
      print("shirt blue, cost=$6")
   elif int(sh)==3:
      print("shirt green, cost=$7")
   else:
      print("enter a valid color")
   return print("Thank you for shopping")

def pants(pa):
   if int(pa)==1:
      print("pant black, cost=$10")
   elif int(pa)==2:
      print("pant blue, cost=$11")
   elif int(pa)==3:
      print("pant white, cost=$12")
   else:
      print("enter a valid color")
   return print("Thank you shopping")

def coat(ca):
   if int(ca)==1:
      print("coat black, cost=$50")
   elif int(ca)==2:
      print("coat grey, cost=$55")
   elif int(ca)==3:
      print("coat skyblue, cost=$60")
   else:
      print("enter a valid color")
   return print("Thank you shopping")



if int(input) == 1:
   sc = color(123)
   las=shirt(sc)
else:
   pass
if int(input) == 2:
   pc=color(456)
   lap=pants(pc)
else:
   pass
if int(input) == 3:
   cc=color(678)
   lac=coat(cc)
else:
   print("enter a valid number")

代码结束

错误:

      select the garment you purchased type 1 for shirt, 2 for pants, 3 for coats: 1
    Traceback (most recent call last):
   File "c:\users\abdul samad\documents\visual studio 2017  \rojects\PythonApplication7\PythonApplication7\PythonApplication7.py", line 50, in <module>
    sc = color("123")
    File "c:\users\abdul samad\documents\visual studio 2017\Projects\PythonApplication7\PythonApplication7\PythonApplication7.py", line 5, in color
    colorin=input("Enter your shirt colour, 1 for red, 2 for blue, 3 for green")
    TypeError: 'str' object is not callable
    Press any key to continue . . .

【问题讨论】:

  • 另外,修正你的代码缩进
  • 是的,因为在第一行你做了input=input(...)。现在变量input 引用了input 函数返回的任何字符串,该函数现在被隐藏并且无法访问。
  • 更改了变量并且它正在工作。谢谢。
  • 代码不能正常工作,请你帮我解决它。

标签: python typeerror


【解决方案1】:

不要将变量的名称与函数 input 相同

【讨论】:

    【解决方案2】:

    查看了您的代码。不太明白你第一行的目的:

    input=input("select the garment you purchased type 1 for shirt, 2 for pants, 3 for coats: ")
    

    这可能会用您在命令行中键入的任何字符串覆盖 input() 函数。结果,后面的函数调用 input() 与那个字符串一起,这不是一个函数,也不是可调用的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-19
      • 2020-10-03
      • 2017-09-19
      • 1970-01-01
      • 2012-12-09
      • 2017-12-26
      相关资源
      最近更新 更多