第一个方法为什么错误,而第二个正确?

huangapple go评论66阅读模式
英文:

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

在第一种方法中,number0number1 没有正确转换为整数。

是的,您对它们调用了 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)

huangapple
  • 本文由 发表于 2023年3月7日 19:17:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定