【问题标题】:TypeError: 'str' object is not callable for user input [duplicate]TypeError:'str'对象不可用于用户输入[重复]
【发布时间】:2019-06-17 11:00:07
【问题描述】:

在我的第一个 if 语句中尝试获取用户输入时,我总是得到“TypeError: 'str' object is not callable”。

我尝试在网上和 stackoverflow 上进行搜索,但只找到了对我不起作用的修复程序,例如确保“()”在正确的位置。

import os
from uuid import uuid4

f = open("address.txt","w+")

input = input("Do you already have an address (y/n): ")

if input == "y":
    address = input("Please enter your address: ")
    f.write(address)
    f.close() 

if input == "n":
    address = "some default address"
    f.write(address)
    f.close() 

奇怪的是,第一个输入没有任何错误,对于第 9 行(地址 = ...),我得到“TypeError:'str' object is not callable”。

【问题讨论】:

    标签: python string typeerror


    【解决方案1】:

    因为您将 str 分配给与您的 input() 函数冲突的 input 变量。

    试试这样:

    import os
    from uuid import uuid4
    
    f = open("address.txt","w+")
    
    my_input = input("Do you already have an address (y/n): ")
    
    if my_input == "y":
        address = input("Please enter your address: ")
        f.write(address)
        f.close() 
    
    if my_input == "n":
        address = "some default address"
        f.write(address)
        f.close() 
    

    【讨论】:

    • 谢谢!现在一切正常
    【解决方案2】:

    你不能使用“输入”这个词作为变量名:

    尝试将 input = input("Do you already have an address (y/n): ") 更改为 inpt = input("Do you already have an address (y/n): ")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-07
      • 2019-03-05
      • 1970-01-01
      • 2015-02-15
      • 2020-09-16
      • 2019-06-11
      • 1970-01-01
      相关资源
      最近更新 更多