学习类的实例化的时候遇到了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. 四大CPU体系结构ARM、X86/Atom、MIPS、PowerPC

    http://blog.csdn.net/wangjianno2/article/details/52140936 RISC(reduced instruction set computer,精简指令 ...

  2. Zabbix3.0部署实践

    Zabbix3.0部署实践   Zabbix3整个web界面做了一个全新的设计. 1.1Zabbix环境准备 [root@linux-node1 ~]# cat /etc/redhat-release ...

  3. 阿里云主机ss

    https://promotion.aliyun.com/ntms/act/vm/aliyun-group/buy.html?group=HdcwGIaf6i

  4. jenkins 执行shell命令出错command not found 和No such file or directory

    [root@localhost usr]# sh test.sh command not found -bash: cd: usr: No such file or directory 这里碰到了一个 ...

  5. Ecliplse 重命名后web.xml 报错Attribute "xmlns" was already specified for element "web-app".

      报错信息:Attribute "xmlns" was already specified for element "web-app" 由于项目的重命名,出现 ...

  6. Java学习过程中的收获

    1. String <--> Date 这种转换要用到java.text.SimpleDateFormat类 字符串转换成日期类型: 方法1: 也是最简单的方法 Date date=new ...

  7. java异常处理 throw RuntimeException时不需要同时方法中声明抛出throws 异常等待调用者catch进行捕获 子父类异常问题

    package com.swift.exception1; public class Demo_Exception { public static void main(String[] args) { ...

  8. 基于mybatis设计简单信息管理系统2

    1.空指针异常 public class CanvasServlet extends HttpServlet { private CanvasService canvasService; privat ...

  9. python代码实现截图识字

    有次网上down了个截图实识的软件,发现一天只能用10次要钱才能解锁免费,很气,作为一个菜鸡怎么能忍受 于是自己用python写了个简单的小工具,分享给大家 代码如下: 先安装库 from PIL i ...

  10. php 操作 mysql 实现批量执行mysql语句 mysql文件

    <?php /** * 批量运行sql文件 * 正则分隔是重点 preg_split("/;[\r\n]+/", filecontent) */ $config = requ ...