python - How can I read inputs as integers? -


why code not input integers? on web says use raw_input(), read on stack overflow (on thread did not deal integer input) raw_input() renamed input() in python 3.x.

play = true  while play:      x = input("enter number: ")     y = input("enter number: ")      print(x + y)     print(x - y)     print(x * y)     print(x / y)     print(x % y)      if input("play again? ") == "no":         play = false 

python 2.x

there 2 functions user input, called input , raw_input. difference between them is, raw_input doesn't evaluate data , returns is, in string form. but, input evaluate whatever entered , result of evaluation returned. example,

>>> import sys >>> sys.version '2.7.6 (default, mar 22 2014, 22:59:56) \n[gcc 4.8.2]' >>> data = input("enter number: ") enter number: 5 + 17 >>> data, type(data) (22, <type 'int'>) 

the data 5 + 17 evaluated , result 22. when evaluates expression 5 + 17, detects adding 2 numbers , result of same int type. so, type conversion done free , 22 returned result of input , stored in data variable. can think of input raw_input composed eval call.

>>> data = eval(raw_input("enter number: ")) enter number: 5 + 17 >>> data, type(data) (22, <type 'int'>) 

note: should careful when using input in python 2.x. explained why 1 should careful when using it, in this answer.

but, raw_input doesn't evaluate input , returns is, string.

>>> import sys >>> sys.version '2.7.6 (default, mar 22 2014, 22:59:56) \n[gcc 4.8.2]' >>> data = raw_input("enter number: ") enter number: 5 + 17 >>> data, type(data) ('5 + 17', <type 'str'>) 

python 3.x

python 3.x's input , python 2.x's raw_input similar , raw_input 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 number: ") enter number: 5 + 17 >>> data, type(data) ('5 + 17', <class 'str'>) 

solution

to answer question, since python 3.x doesn't evaluate , convert data type, have explicitly convert ints, int, this

x = int(input("enter number: ")) y = int(input("enter number: ")) 

you can accept numbers of base , convert them directly base-10 int function, this

>>> data = int(input("enter number: "), 8) enter number: 777 >>> data 511 >>> data = int(input("enter number: "), 16) enter number: ffff >>> data 65535 >>> data = int(input("enter number: "), 2) enter number: 10101010101 >>> data 1365 

the second parameter tells base of numbers entered , internally understands , converts it. if entered data wrong throw valueerror.

>>> data = int(input("enter number: "), 2) enter number: 1234 traceback (most recent call last):   file "<input>", line 1, in <module> valueerror: invalid literal int() base 2: '1234' 

apart that, program can changed little bit, this

while true:     ...     ...     if input("play again? ") == "no":         break 

you can rid of play variable using break , while true.

ps: python doesn't expect ; @ end of line :)


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -