Python学习笔记——Python语法错误总结
发布日期:2021-05-14 10:19:28 浏览次数:16 分类:精选文章

本文共 3753 字,大约阅读时间需要 12 分钟。

Python������������������Python������������������

1������������1������

IndentationError: unexpected indent

������Python���������������������������������������������������������������������������Python���������������������������������������tab������������������������������������������

���������������������

  • ���if���������for������������������������������������������������������������������
  • ���������������������������������������������������
  • ���������������������������������������������

���������

favorite_languages = { 'jen': 'python', 'sarah': 'c', 'edward': 'C++', 'phil': 'python', } print("Sarah's favorite language is " + favorite_languages['sarah'].title() + ".")

������������������print������������������������favorite_languages���������������������������������������������������������������

���������Python������������������������������������������������������������������������������tab������������������������������������������������

2������������2������

NameError: name 'xxxx' is not defined

���������������������������������������������������������������������������������Python������������������������������������������������������������

���������������������

  • ������������������������������������������������������������
  • ���������������������������������������
  • ������������������������������������������

���������������������

  • ������������������������������������������
  • ������������������������������������������������
  • ������������������������������������������������������������

3������������3������

KeyboardInterrupt: KeyboardInterrupt

������������������������������������������������������������������������������������������������

��������������������������������������������������������������������� reckless���-code ���������������������������������������������

  • ������������������������������ reckles.ArgumentParser
  • ������������������������������try-except���������������
  • ������������������������������������������������������������

4������������4������

IndexError: list index out of range

���������������������������������������������������������������������������Python������������������������������������������������������

  • ������������������������������������������len()������������������
  • ������������������������ safeguards ���������������������
  • ������������������������try-except������������������������

���������

numbers = [1, 2, 3] try: print(numbers[3]) # ������������IndexError���������������4������������������������ except IndexError: print("Index out of range")

5������������5������

ValueError: invalid literal for int() with base 10: 'a'

������������������������������������������������������������������������������������������������������������������������������

���������������������

  • ���������������������������������������������������
  • ������try-except������������������������
  • ������������������������������������������

���������

try: num = int('abc') # ���������������ValueError���������'abc'��������������������� except ValueError as e: print(f"���������{e}")

6������������6������

UnboundLocalError: local variable refer to undefined variable

������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������

  • ������������������for������������������������������������������������������
  • ���������������������������������������
  • ������������������global���������������������������������

���������

def process_list(lst): for item in lst: if item % 2 == 0: print(f"���������������������{item}") del lst # ���������������������������������������������� # ������������������������������


������������Python���������������������������������������������������������������������������������������������

上一篇:Python学习笔记——函数(看到一半)
下一篇:Python学习笔记——字典

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2025年05月01日 17时54分10秒