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
相关推荐
黄贵根4 小时前
JavaScript实现教培行业意向登记系统
开发语言·javascript·ecmascript
小趴蔡ha7 小时前
02 Anaconda、Python 与机器学习开发环境入门
python·机器学习·anaconda
2401_868534787 小时前
RTOS之RT-Thread & Linux:线程/进程管理与调度核心差异分析
c++·python
zzzll11117 小时前
Claude Code离线安装方案揭秘
开发语言·php
数字化转型分享点滴8 小时前
富士康、蓝思科技为何选择四化信息?MES制造执行系统的实践
python·科技
wling03018 小时前
vasp虚频计算-python脚本
开发语言·windows·python·vasp计算
魔镜前的帅比8 小时前
(开源项目)x-claw(总)
python·ai·rust·开源
郝学胜-神的一滴8 小时前
Qt 高级编程 040:按钮悬浮弹出滑块弹窗的完整攻略
开发语言·c++·qt·软件工程·用户界面
xrandzj9 小时前
Python面向对象编程入门:类、实例、初始化与封装实践
开发语言·python
DLYSB_10 小时前
API 网关流量洪峰与突发 CC 攻击:我用 Go 写了个“现场物理防御哨兵”,把故障响应压缩到秒级
开发语言·后端·golang·报警灯