python读取屏幕数字,python - 如何将输入读取为数字?

TLDR

Python 3 doesn't evaluate the data received with input function, but Python 2's input function does (read the next section to understand the implication).

Python 2's equivalent of Python 3's input is the raw_input function.

Python 2.x

There were two functions to get user input, called input and raw_input. The difference between them is, raw_input doesn't evaluate the data and returns as it is, in string form. But, input will evaluate whatever you entered and the result of evaluation will be returned. For example,

>>> import sys

>>> sys.version

'2.7.6 (default, Mar 22 2014, 22:59:56) \n[GCC 4.8.2]'

>>> data = input("Enter a number: ")

Enter a number: 5 + 17

>>> data, type(data)

(22, )

The data 5 + 17 is evaluated and the result is 22. When it evaluates the expression 5 + 17, it detects that you are adding two numbers and so the result will also be of the same int type. So, the type conversion is done for free and 22 is returned as the result of input and stored in data variable. You can think of input as the raw_input composed with an eval call.

>>> data = eval(raw_input("Enter a number: "))

Enter a number: 5 + 17

>>> data, type(data)

(22, )

Note: you should be careful when you are using input in Python 2.x. I explained why one should be careful when using it, in this answer.

But, raw_input doesn't evaluate the input and returns as it is, as a string.

>>> import sys

>>> sys.version

'2.7.6 (default, Mar 22 2014, 22:59:56) \n[GCC 4.8.2]'

>>> data = raw_input("Enter a number: ")

Enter a number: 5 + 17

>>> data, type(data)

('5 + 17', )

Python 3.x

Python 3.x's input and Python 2.x's raw_input are similar and raw_input is not available in Python 3.x.

>>> import sys

>>> sys.version

'3.4.0 (default, Apr 11 2014, 13:05:11) \n[GCC 4.8.2]'

>>> data = input("Enter a number: ")

Enter a number: 5 + 17

>>> data, type(data)

('5 + 17', )

Solution

To answer your question, since Python 3.x doesn't evaluate and convert the data type, you have to explicitly convert to ints, with int, like this

x = int(input("Enter a number: "))

y = int(input("Enter a number: "))

You can accept numbers of any base and convert them directly to base-10 with the int function, like this

>>> data = int(input("Enter a number: "), 8)

Enter a number: 777

>>> data

511

>>> data = int(input("Enter a number: "), 16)

Enter a number: FFFF

>>> data

65535

>>> data = int(input("Enter a number: "), 2)

Enter a number: 10101010101

>>> data

1365

The second parameter tells what is the base of the numbers entered and then internally it understands and converts it. If the entered data is wrong it will throw a ValueError.

>>> data = int(input("Enter a number: "), 2)

Enter a number: 1234

Traceback (most recent call last):

File "", line 1, in

ValueError: invalid literal for int() with base 2: '1234'

For values that can have a fractional component, the type would be float rather than int:

x = float(input("Enter a number:"))

Apart from that, your program can be changed a little bit, like this

while True:

...

...

if input("Play again? ") == "no":

break

You can get rid of the play variable by using break and while True.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值