学习类的实例化的时候遇到了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. Zabbix 2.4安装部署

    目  录 第1章 Zabbix Server安装... 1 1.1 Zabbix Server环境... 1 1.2 Zabbix Server软件... 1 1.3 Zabbix server配置. ...

  2. fast rcnn的实例

    http://www.cnblogs.com/louyihang-loves-baiyan/p/4906690.html https://saicoco.github.io/object-detect ...

  3. python剑指offer最小的K个数

    题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 思路: 使用快排中的partition思想. ①我们设定part ...

  4. 下载Xcode历史版本方法

    1.打开链接:https://developer.apple.com/download/more 进入页面 2.在搜索框输入Xcode,回车搜索.如图,找到各种版本Xcode 搜索Xcode 3.双击 ...

  5. Raect Router 4 的使用 (1)

    本文来自于官方文档,属于意译而非直译 基本组件 React Router 有三种类型的组件,分别是:react-router.react-router-dom.react-router-native ...

  6. Java分享笔记:Map集合(接口)的基本方法程序演示

    package pack02; import java.util.*; public class MapDemo { public static void main(String[] args) { ...

  7. BDC备忘

    更新模式,有下列可选值(更新模式常用的是S)   "A" 异步更新.被调用程序的更新按照没有指定 COMMIT WORK 语句和 AND WAIT 附加的方式执行.       也 ...

  8. Centos下使用Docker部署MySql

    本文讲述 CentOS 系统 Docker 中安装 MySql 的过程 步骤 1. 拉取 Docker Hub 官方提供的mysql镜像 docker pull mysql:5.7 2.运行容器 do ...

  9. js将人民币数字转大写

    function numberToUpper(money) { var cnNums = new Array("零", "壹", "贰", ...

  10. JZOJ 5919. 逛公园

    Description            琥珀色黄昏像糖在很美的远方,思念跟影子在傍晚一起被拉长……Description      小 B 带着 GF 去逛公园,公园一共有 n 个景点,标号为 ...