英文:
Why is the first method wrong and the second correct?
问题
#第一种方法(不正确)
two_digit_number = input
number0 = two_digit_number[0]
number1 = two_digit_number[1]
int(number0)
int(number1)
result = number0 + number1
print(result)
#第二种方法(正确)
two_digit_number = input
number0 = two_digit_number[0]
number1 = two_digit_number[1]
result = int(number0) + int(number1)
print(result)
数据类型转换是如何工作的?
英文:
#FIRST METHOD(INCORRECT)
two_digit_number = input
number0 = two_digit_number[0]
number1 = two_digit_number[1]
int(number0)
int(number1)
result = number0 + number1
print(result)
#SECOND METHOD(CORRECT)
two_digit_number = input
number0 = two_digit_number[0]
number1 = two_digit_number[1]
result = int(number0) + int(number1)
print(result)
How does converting data types work?
答案1
得分: 1
在第一种方法中,number0
和 number1
没有正确转换为整数。
是的,您对它们调用了 int()
函数,但变量的类型仍然保持不变。
我认为您可以这样做:
two_digit_number = input
number0 = two_digit_number[0]
number1 = two_digit_number[1]
number0 = int(number0)
number1 = int(number1)
result = number0 + number1
print(result)
英文:
In the first method, number0
and number1
were not transformed into int properly.
yes, you called int()
function on them but the variables remained the same type.
I think you could have done it this way:
two_digit_number = input
number0 = two_digit_number[0]
number1 = two_digit_number[1]
number0 = int(number0)
number1 = int(number1)
result = number0 + number1
print(result)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论