-4

如何在 python 2.7 中将 Unicode 字符串解析为 int?我试过了:

a= int(input) 

但无法成功。

4

2 回答 2

2

input() 是python中的内置函数。它默认返回一个字符串。您可以使用外部类型转换将字符串转换为任何其他数据类型。

d = input('Enter a number')
# d will have a string value

e = int(d)
# e is now an integer value

print e * 10

a = input('Enter your name')
# a will have a string value

c = a + "is an awesome person"
# string concatination
于 2013-09-26T16:33:50.050 回答
1

您可以使用 try-except 循环将输入转换为 int。如果输入了不正确的值,则会抛出异常并重新提示用户输入。

while True:
     try:
          number = int(input("Enter and integer."))
     except:
          print("An integer is a WHOLE NUMBER, genius.")
     else:
          break

print(number + 10)
于 2013-09-26T16:58:56.107 回答