python常见问题--2

出现error:

Traceback (most recent call last):

File "D:\1yts\Exercise_Python\homework_python\第一章\1.3.py", line 9, in <module>

input_number1,input_number2 = int(input().split())

TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'

错误分析:

应该是出现在 input_number1,input_number2 = int(input().split())

这一行我将两个值都直接转换成int,导致误认为是list,将string的list直接全部转换成int会出现错误,int不支持list转换。

解决方法:

1、分开来强制类型转换,一个一个来;

2、直接将键盘收到的两个字符串进行split()之后的list直接给map(),函数会返回一个迭代器,通过解包赋值每一个转换,最后输出。

完整代码:

python 复制代码
# 需求:每一行要输入两个数字,要是输出的结果又连续两次的0,那么直接跳出循环。

# 统计计算结果为0的次数
count = 0

while count < 2:
    print(f"每一行输入两个数字")
    # 输入两个数字,两个数之间存在空格,在计算的时候直接删去
    # input_number1,input_number2 = input().split()
    # 在网上能搜到还有一种方法可以直接解决list转换成int的问题
    input_number1, input_number2 = map(int, input().split())
    if int(input_number1) + int(input_number2) != 0:
       print(int(input_number1) + int(input_number2))
    else:
        print(int(input_number1) + int(input_number2))
        count += 1
相关推荐
步行cgn3 小时前
JSON.stringify 详解:把 JavaScript 对象变成 JSON 字符串
开发语言·javascript·json
速易达网络3 小时前
如何“训练”一个 .task 模型
python
PiaoKe___3 小时前
云手机技术解析:从 ARM 虚拟化到 Python 自动化实战
python·智能手机·自动化
学习论之费曼学习法3 小时前
无人值守AI内容矩阵实战(10):Windows 上让 Python 进程 7×24 活着
人工智能·windows·python·矩阵·powershell·进程守护
Soneswy3 小时前
C++基础篇——类与对象(中)
开发语言·c++
CoderYanger4 小时前
Java EE 进阶:5. Spring IoC&DI(第二弹):Spring IoC
java·开发语言·spring boot·spring·面试·职场和发展·java-ee
Norna4 小时前
锁文件里明明有哈希,PDM 为什么说没有?
python
大侠在此在在此4 小时前
我花了不到1块钱,用python把自己一年的1500次会话做成了个人知识库
python·llm
Jino4 小时前
900×2100 的门洞,展开面积却是 3.4 ㎡ —— 不锈钢核价引擎的七条口径
python
贝壳6904 小时前
Python 布尔数组选型:list、numpy.bool_、bitarray、RoaringBitmap、bool-hybrid-array 各管一段
python