python 读取整数_在Python中将输入读取为整数

python 读取整数

To take input in Python, we use input() function, it asks for an input from the user and returns a string value, no matter what value you have entered, all values will be considered as strings values.

要使用Python进行输入,我们使用input()函数 ,它要求用户输入并返回字符串值,无论您输入了什么值,所有值都将被视为字符串值。

Consider the following example,

考虑以下示例,

# python code to demonstrate example
# of input() function

val1 = input("Enter any value: ")
print("value of val1: ", val1)
print("type of val1: ", type(val1))

val2 = input("Enter another value: ")
print("value of val2: ", val2)
print("type of val2: ", type(val2))

val3 = input("Enter another value: ")
print("value of val3: ", val3)
print("type of val3: ", type(val3))

Output

输出量

Enter any value: 10
value of val1:  10
type of val1:  <class 'str'>
Enter another value: 10.23
value of val2:  10.23
type of val2:  <class 'str'>
Enter another value: Hello
value of val3:  Hello
type of val3:  <class 'str'>

See the program and output – Here, we provided three value "10" for val1 which is an integer value but considered as a string, "10.23" for val2 which is a float value but considered as a string, "Hello" for val3 which is a string value.

参见程序和输出–在这里,我们为val1提供了三个值“ 10” ,它是一个整数值,但被视为字符串,为val2提供了 “ 10.23” ,它是一个浮点值,但被视为一个字符串,为val3提供了 “ Hello” ,其中是一个字符串值。

如何将输入作为整数? (How to take input as integer?)

There is no such method, that can be used to take input as an integer directly – but input string can be converted into an integer by using int() function which accepts a string and returns an integer value.

没有这样的方法,可以直接将输入作为整数使用-但是可以使用int()函数将输入字符串转换为整数,该函数接受一个字符串并返回一个整数值。

Thus, we use input() function to read input and convert it into an integer using int() function.

因此,我们使用input()函数读取输入,并使用int()函数将其转换为整数。

Consider the below example,

考虑下面的示例,

# python code to take integer input

# reading a value, printing input and it's type
val1 = input("Enter any number: ")
print("value of val1: ", val1)
print("type of val1: ", type(val1))

# reading a value, converting to int
# printing value and it's type
val2 = int(input("Enter any number: "))
print("value of val2: ", val2)
print("type of val2: ", type(val2))

Output

输出量

Enter any number: 123
value of val1:  123
type of val1:  <class 'str'>
Enter any number: 456
value of val2:  456
type of val2:  <class 'int'>

Example to input two integer numbers and find their sum and average

输入两个整数并求和和求平均值的示例

# python code to read two integer numbers
# and find their addition, average

num1 = int(input("Enter first number : "))
num2 = int(input("Enter second number: "))

# addition
add = num1 + num2

# average
avg = float(num1 + num2)/2

print("addition: ", add)
print("average : ", avg)

Output

输出量

Enter first number : 3
Enter second number: 4
addition:  7
average :  3.5

Recommended posts

推荐的帖子

翻译自: https://www.includehelp.com/python/read-input-as-an-integer.aspx

python 读取整数

  • 0
    点赞
  • 0
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值