学习类的实例化的时候遇到了AttributeError: 'str' object has no attribute 'input_text', 以下是报错的代码及修改正确的代码。

class shuru_1:

  def __init__(self, input_text):
    self.input_text = input_text   def repeat_input(self):
    print("输入的内容是:{}".format(self.input_text)) def main():
  input_text = input("请输入一个数字:")
  shuru_1.repeat_input(input_text) if __name__ == '__main__':
  main()

输出结果:

请输入一个数字:1
Traceback (most recent call last):
File "D:/PythonProject/20180928/test.py", line 17, in <module>
main()
File "D:/PythonProject/20180928/test.py", line 14, in main
shuru_1.repeat_input(input_text)
File "D:/PythonProject/20180928/test.py", line 7, in repeat_input
print("输入的内容是:{}".format(self.input_text))
AttributeError: 'str' object has no attribute 'input_text'

代码修改:

class shuru_1:

  def __init__(self, input_text):
    self.input_text = input_text   def repeat_input(self):
    print("输入的内容是:{}".format(self.input_text)) def main():
  input_text = input("请输入一个数字:")
  Shuru_1 = shuru_1(input_text)
  Shuru_1.repeat_input() if __name__ == '__main__':
  main()

输出结果:

请输入一个数字:1
输入的内容是:1

【Python-遇到的Error】AttributeError: 'str' object has no attribute 'input_text'的更多相关文章

  1. Python PyInstaller 打包报错:AttributeError: 'str' object has no attribute 'items'

    pyinstaller打包时报错:AttributeError: 'str' object has no attribute 'items' 网上查询,可能是setuptools比较老: 更新一下 p ...

  2. Django 运行报异常:AttributeError: 'str' object has no attribute 'get'

    Technorati Tags: Python,Django,Web 在使用django.contrib.auth用户机制进行用户的验证.登录.注销操作时,遇到这个异常. 首先是写了一个登录的视图,要 ...

  3. Django2.2报错 AttributeError: 'str' object has no attribute 'decode'

    准备将 Django 连接到 MySQL,在命令行输入命令 python manage.py makemigrations 后报错: AttributeError: 'str' object has ...

  4. Django项目与mysql交互进行数据迁移时报错:AttributeError: 'str' object has no attribute 'decode'

    问题描述 Django项目启动,当我们执行命令 python manage.py makemigrations 出现如下错误: File , in last_executed_query query ...

  5. celery:Unrecoverable error: AttributeError("'unicode' object has no attribute 'iteritems')

    环境描述 python2+django1.9下使用celery异步处理耗时请求. celery使用的是celery-with-redis这个第三方库,版本号为3.0. pip install cele ...

  6. python3.x运行的坑:AttributeError: 'str' object has no attribute 'decode'

    1.Python3.x和Python2.X版本有一些区别,我遇到了两个问题如下: a.第一个报:mysqlclient 1.3版本不对: 解决办法:注释掉这行即可: b.第二个报:字符集的问题: 报错 ...

  7. 解决编码问题:AttributeError: 'str' object has no attribute 'decode'

    1. 问题发现: 出现:读取文件,对其进行解码,出现错误,AttributeError: 'str' object has no attribute 'decode' 解释:属性错误,str对象不包含 ...

  8. Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法

    最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...

  9. 执行: python manage.py makemigrations报AttributeError: 'str' object has no attribute 'decode'

    找到错误代码(line146):query = query.encode(errors='replace') 解决方法:把decode改为encode即可.

随机推荐

  1. POJ-3349 Snowflake Snow Snowflakes---最小表示法

    题目链接: https://vjudge.net/problem/POJ-3349 题目大意: 每个雪花都有六个分支,用六个整数代表,这六个整数是从任意一个分支开始,朝顺时针或逆时针方向遍历得到的.输 ...

  2. Family Gathering at Christmas(思维题)

    Family Gathering at Christmas 时间限制: 1 Sec  内存限制: 128 MB提交: 13  解决: 4[提交] [状态] [讨论版] [命题人:admin] 题目描述 ...

  3. 如何让HttpWebRequest使用指定网络接口传输数据

    using System; using System.Net; class Program { public static void Main () { foreach (var ip in Dns. ...

  4. a=a+(a++);b=b+(++b);计算顺序,反汇编

    a=a+(a++); 013913BC mov eax,dword ptr [a] 013913BF add eax,dword ptr [a] 013913C2 mov dword ptr [a], ...

  5. 第42章 电源管理—实现低功耗—零死角玩转STM32-F429系列

    第42章     电源管理—实现低功耗 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fir ...

  6. 第13章 GPIO-位带操作—零死角玩转STM32-F429系列

    第13章     GPIO—位带操作 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fire ...

  7. 重定向跳出父Frame

    当session过期后可以用过滤器来设置重定向页面 代码如下: public class ActionFilter extends HttpServlet implements Filter {pri ...

  8. Linux nohup 关闭终端的时候,程序依然能在后台运行( linux重定向及nohup不输出的方法)

    先说一下linux重定向: 0.1和2分别表示标准输入.标准输出和标准错误信息输出,可以用来指定需要重定向的标准输入或输出.在一般使用时,默认的是标准输出,既1.当我们需要特殊用途时,可以使用其他标号 ...

  9. Intellij IDEA 查找接口实现类的快捷键

    查找接口的实现类: IDEA 风格 ctrl + alt +B 查看类或接口的继承关系: ctrl + h 1.IDEA_查找接口的实现 的快捷键 http://blog.csdn.net/u0100 ...

  10. MARK 一条关于Linux 运维方面个人向收藏网址

    吴钧泽博客 https://wujunze.com/archives.html Linux运维笔记 https://blog.linuxeye.cn/ Linux中文网 http://www.ppze ...