Python异常 ValueError的问题详解

这篇文章主要介绍了Python异常 ValueError的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Python异常 ValueError

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

试图将一个与数字无关的类型转化为整数,会抛出该异常。

>>> int("99 years ago.")
Traceback (most recent call last):
  File "", line 1, in 
ValueError: invalid literal for int() with base 10: '99 years ago.'

规避方法:int函数参数应该合法使用。int函数使用传送门:Python中的int函数使用

ValueError: too many values to unpack (expected 2)

试图遍历字典时同时遍历键和值。

>>> demo = {"China": "Beijing", "Japan": "Tokyo"}
>>> for k, v in demo:
...     print(k, v)
...
Traceback (most recent call last):
  File "", line 1, in 
ValueError: too many values to unpack (expected 2)

Python只允许对字典key的遍历,因此上面的遍历方式是错误的。

规避方法

方法一:使用dict[key]的方式同时获取value

>>> demo = {"China": "Beijing", "Japan": "Tokyo"}
>>> for key in demo:
...     print(key, demo[key])
...
China Beijing
Japan Tokyo

方法二:使用items方法

>>> demo = {"China": "Beijing", "Japan": "Tokyo", "the United States": "Washington D.C."}
>>> for key, value in demo.items():
...     print(key, value)
...
China Beijing
Japan Tokyo
the United States Washington D.C.
ValueError: binary mode doesn't take an encoding argument

试图以二进制模式读取文件时指定编码方式。

>>> with open("protoc-gen-go", "rb+", encoding="utf-8") as file:
...     data = file.read()
...
Traceback (most recent call last):
  File "", line 1, in 
ValueError: binary mode doesn't take an encoding argument

规避方法:避免使用encoding关键字

>>> with open("protoc-gen-go", "rb+") as file:
...     data = file.read(10)
...
>>> data
b'\xcf\xfa\xed\xfe\x07\x00\x00\x01\x03\x00'
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表是一种常见的数据结构,用于存储一系列的元素。相比于数组,链表的插入和删除操作更加高效。在Python中,可以使用类来实现链表。 一个链表由一个个节点组成,每个节点包含两个部分:数据和指向下一个节点的指针。 下面是一个简单的链表的示例: ```python class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if self.head is None: self.head = new_node else: current = self.head while current.next is not None: current = current.next current.next = new_node def insert(self, data, position): if position < 0 or position > self.length(): raise ValueError("Invalid position") new_node = Node(data) if position == 0: new_node.next = self.head self.head = new_node else: current = self.head for _ in range(position - 1): current = current.next new_node.next = current.next current.next = new_node def remove(self, data): if self.head is None: raise ValueError("LinkedList is empty") if self.head.data == data: self.head = self.head.next else: current = self.head while current.next is not None: if current.next.data == data: current.next = current.next.next return current = current.next raise ValueError("Data not found") def length(self): count = 0 current = self.head while current is not None: count += 1 current = current.next return count def print_list(self): current = self.head while current is not None: print(current.data, end=" ") current = current.next print() ``` 上述代码中,`Node`类表示链表中的节点,包含`data`和`next`两个属性。`LinkedList`类表示链表,包含`head`属性作为链表的头节点。 `append`方法用于向链表末尾添加一个节点。如果链表为空,新节点将成为头节点;否则,遍历链表直到找到末尾节点,将新节点添加在其后。 `insert`方法用于在指定位置插入一个节点。如果位置为0,新节点将成为头节点;否则,遍历链表到达指定位置,将新节点插入在当前节点之后。 `remove`方法用于删除链表中的某个节点。如果要删除的节点是头节点,将头节点指向下一个节点;否则,遍历链表直到找到要删除的节点,将当前节点的`next`指针指向要删除节点的下一个节点。 `length`方法用于计算链表的长度。 `print_list`方法用于打印链表中的所有元素。 可以使用以下代码创建一个链表并进行操作: ```python # 创建链表 my_list = LinkedList() # 添加元素 my_list.append(1) my_list.append(2) my_list.append(3) # 插入元素 my_list.insert(4, 1) # 打印链表 my_list.print_list() # 输出: 1 4 2 3 # 删除元素 my_list.remove(2) # 打印链表 my_list.print_list() # 输出: 1 4 3 # 计算链表长度 print(my_list.length()) # 输出: 3 ``` 这是链表的基本实现,你可以根据需要进行扩展和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值