出现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