• Python编程:基础学习常见错误整理


    # Python学习之错误整理:

    # 错误一:
    # TypeError: cannot concatenate 'str' and 'int' objects
    # 不能连接str和int对象
    age = 23
    message = "Happy " + age + "rd Birthday!"
    # 分析:
    # 这是一个类型错误,意味着Python无法识别你使用的信息。在这个示例中,Python发现你使
    # 用了一个值为整数( int )的变量,但它不知道该如何解读这个值。Python知道,这个变
    # 量表示的可能是数值23,也可能是字符2和3。像上面这样在字符串中使用整数时,需要显式地指
    # 出你希望Python将这个整数用作字符串。为此,可调用函数 str() ,
    # 它让Python将非字符串值表示为字符串:
    #解决方法: message = "Happy " + str(age) + "rd Birthday!"
    print(message);

    # 错误二:
    # IndexError: list index out of range
    # 索引错误:列表索引超出范围
    # Python试图向你提供位于索引3处的元素,但它搜索列表 motorcycles 时,却发现索引3处没有
    # 元素。鉴于列表索引差一的特征,这种错误很常见。有些人从1开始数,因此以为第三个元素的
    # 索引为3;但在Python中,第三个元素的索引为2,因为索引是从0开始的。
    motorcycles = ['honda', 'yamaha', 'suzuki'];
    print(motorcycles[3]);

    # 错误三:
    # IndentationError: expected an indented block
    # 缩进错误:预期一个缩进快(意思需要一个缩进快)
    magicians = ['alice', 'david', 'carolina'];
    for magician in magicians:
    print(magician);

    # 错误四:
    # IndentationError: unexpected indent
    # 缩进错误:意外缩进(这里不应需要缩进)
    message = "Hello Python world!";
    print(message);

    # 错误五:
    # TypeError: 'tuple' object does not support item assignment
    dimensions = (200,50);
    print(dimensions);
    print(dimensions[0]);
    print(dimensions[1]);
    # TypeError: 'tuple' object does not support item assignment
    # 类型错误:元组对象不支持元素值重新分配,也就是不能尝试去修改元组中的任一个元素的值
    # dimensions[0] = 250;
    print(dimensions);

    ---------------------
    作者:上善若水
    来源:CSDN
    原文:https://blog.csdn.net/btt2013/article/details/54237412
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    求欧拉路径模版 fleury算法
    回学校前的计划
    高斯消元模版
    usaco 3.2 Stringsobits 数位dp
    dijkstra模版
    codeforces AIM Tech Round (Div. 2)
    bnuoj 51275 并查集按深度合并建树
    bzoj3674: 可持久化并查集
    poj2104 求区间第k大 可持久化线段树
    Miller_Rabin判断素数模版
  • 原文地址:https://www.cnblogs.com/jack1989/p/9841975.html
Copyright © 2020-2023  润新知