How can I read inputs as numbers?












227














Why are x and y strings instead of ints in the below code? Everything on the web says to use raw_input(), but I read on Stack Overflow (on a thread that did not deal with integer input) that raw_input() was renamed to input() in Python 3.x.



play = True

while play:

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

print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)

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









share|improve this question
























  • asking-the-user-for-input-until-they-give-a-valid-response
    – Patrick Artner
    Sep 13 at 20:50
















227














Why are x and y strings instead of ints in the below code? Everything on the web says to use raw_input(), but I read on Stack Overflow (on a thread that did not deal with integer input) that raw_input() was renamed to input() in Python 3.x.



play = True

while play:

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

print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)

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









share|improve this question
























  • asking-the-user-for-input-until-they-give-a-valid-response
    – Patrick Artner
    Sep 13 at 20:50














227












227








227


63





Why are x and y strings instead of ints in the below code? Everything on the web says to use raw_input(), but I read on Stack Overflow (on a thread that did not deal with integer input) that raw_input() was renamed to input() in Python 3.x.



play = True

while play:

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

print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)

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









share|improve this question















Why are x and y strings instead of ints in the below code? Everything on the web says to use raw_input(), but I read on Stack Overflow (on a thread that did not deal with integer input) that raw_input() was renamed to input() in Python 3.x.



play = True

while play:

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

print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)

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






python python-2.7 python-3.x int






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 7:24









user2357112

150k12157247




150k12157247










asked Dec 8 '13 at 3:08









Hosch250

2,29432042




2,29432042












  • asking-the-user-for-input-until-they-give-a-valid-response
    – Patrick Artner
    Sep 13 at 20:50


















  • asking-the-user-for-input-until-they-give-a-valid-response
    – Patrick Artner
    Sep 13 at 20:50
















asking-the-user-for-input-until-they-give-a-valid-response
– Patrick Artner
Sep 13 at 20:50




asking-the-user-for-input-until-they-give-a-valid-response
– Patrick Artner
Sep 13 at 20:50












15 Answers
15






active

oldest

votes


















240














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, <type 'int'>)


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, <type 'int'>)


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', <type 'str'>)


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', <class 'str'>)




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 "<input>", line 1, in <module>
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.



PS: Python doesn't expect ; at the end of the line :)






share|improve this answer























  • Is there any other way, like a function or something so that we dont need to convert to int in 3.x other than doing explicit conversion to int??
    – Shreyan Mehta
    Apr 9 '16 at 6:19










  • @ShreyanMehta eval would work, but don't go for that unless you have pressing reasons.
    – thefourtheye
    Apr 9 '16 at 7:01






  • 1




    @thefourtheye at least use ast.literal_eval for that. It does not have the security concerns of eval.
    – spectras
    Apr 6 at 12:48






  • 1




    I use this Q&A as a dupe target, but maybe you can add a TDLR with the python 3 solution, i.e. int(input()... at the top? Python 2 is nearing the end of it's life and the python 3 info is too buried IMO
    – Chris_Rands
    Jul 24 at 14:36






  • 1




    @Chris_Rands Sorry, it took a while. I updated with a TLDR now, PTAL.
    – thefourtheye
    Oct 17 at 6:01



















35














In Python 3.x, raw_input was renamed to input and the Python 2.x input was removed.



This means that, just like raw_input, input in Python 3.x always returns a string object.



To fix the problem, you need to explicitly make those inputs into integers by putting them in int:



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


Also, Python does not need/use semicolons to end lines. So, having them doesn't do anything positive.






share|improve this answer























  • Nice short answer. There seems to be lots of confusion over what's in Py3x and what's not! Here are the docs for input() [link]docs.python.org/3/library/functions.html#input
    – MJM
    Jul 24 at 11:03










  • this works well, up to a point... if you enter an string (like 'foo') it'll raise ValueError:invalid literal for int() with base 10.... so you need to check before if it's actually an integer (or catch the exception). My question is, what is a pythonic way to do this?
    – Rodrigo Laguna
    Nov 12 at 15:14



















22














For multiple integer in a single line, map might be better.



arr = map(int, raw_input().split())


If the number is already known, (like 2 integers), you can use



num1, num2 = map(int, raw_input().split())





share|improve this answer































    13














    input() (Python 3) and raw_input() (Python 2) always return strings. Convert the result to integer explicitly with int().



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


    Pro tip: semi-colons are not needed in Python.






    share|improve this answer





























      9














      Multiple questions require input for several integers on single line. The best way is to input the whole string of numbers one one line and then split them to integers.



       p=raw_input()
      p=p.split()
      for i in p:
      a.append(int(i))





      share|improve this answer































        6














        Convert to integers:



        my_number = int(input("enter the number"))


        Similarly for floating point numbers:



        my_decimalnumber = float(input("enter the number"))





        share|improve this answer































          6














          Taking int as input in python:
          we take a simple string input using:



          input()


          now we want int as input.so we typecast this string to int. simply using:



          int(input())





          share|improve this answer





























            5














            Python 3.x has input() function which returns always string.So you must convert to int



            python 3.x



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


            python 2.x



            In python 2.x raw_input() and input() functions always return string so you must convert them to int too.



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





            share|improve this answer





























              5














              In Python 3.x. By default the input function takes input in string format . To convert it into integer you need to include int(input())



              x=int(input("Enter the number"))





              share|improve this answer































                3














                I encountered a problem of taking integer input while solving a problem on CodeChef, where two integers - separated by space - should be read from one line.



                While int(input()) is sufficient for a single integer, I did not find a direct way to input two integers. I tried this:



                num = input()
                num1 = 0
                num2 = 0

                for i in range(len(num)):
                if num[i] == ' ':
                break

                num1 = int(num[:i])
                num2 = int(num[i+1:])


                Now I use num1 and num2 as integers. Hope this helps.






                share|improve this answer





















                • This looks very interesting. However, isn't i destroyed when the for loop is exited?
                  – Hosch250
                  May 23 '14 at 16:33












                • @hosch250 When a loop is exited, the value of the index variable (here, i) remains. I tried this piece out, and it works correctly.
                  – Aravind
                  May 24 '14 at 15:18










                • For this kind of input manipulation, you can either num1, num2 = map(int, input().split()) if you know how much integers you will encounter or nums = list(map(int, input().split())) if you don't.
                  – Mathias Ettinger
                  Jul 12 at 12:58



















                3














                def dbz():
                try:
                r = raw_input("Enter number:")
                if r.isdigit():
                i = int(raw_input("Enter divident:"))
                d = int(r)/i
                print "O/p is -:",d
                else:
                print "Not a number"
                except Exception ,e:
                print "Program halted incorrect data entered",type(e)
                dbz()

                Or

                num = input("Enter Number:")#"input" will accept only numbers





                share|improve this answer































                  2














                  While in your example, int(input(...)) does the trick in any case, python-future's builtins.input is worth consideration since that makes sure your code works for both Python 2 and 3 and disables Python2's default behaviour of input trying to be "clever" about the input data type (builtins.input basically just behaves like raw_input).






                  share|improve this answer





























                    2














                    n=int(input())
                    for i in range(n):
                    n=input()
                    n=int(n)
                    arr1=list(map(int,input().split()))


                    the for loop shall run 'n' number of times . the second 'n' is the length of the array.
                    the last statement maps the integers to a list and takes input in space separated form .
                    you can also return the array at the end of for loop.






                    share|improve this answer





























                      2














                      play = True

                      while play:

                      #you can simply contain the input function inside an int function i.e int(input(""))
                      #This will only accept int inputs
                      # and can also convert any variable to 'int' form

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

                      print(x + y)
                      print(x - y)
                      print(x * y)
                      print(x / y)
                      print(x % y)

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





                      share|improve this answer























                      • While this code block may answer the question, it would be best if you could provide a little explanation for why it does so. Please edit your answer to include such a description.
                        – Artjom B.
                        Oct 14 at 13:11



















                      1














                      Yes, in python 3.x, raw_input is replaced with input. In order to revert to old behavior of input use:



                      eval(input("Enter a number: "))



                      This will let python know that entered input is integer






                      share|improve this answer





















                      • Is this correct?
                        – tjt263
                        Mar 8 '16 at 13:46










                      • Yes, you may try please
                        – Waseem Akhtar
                        Jul 3 '16 at 11:26






                      • 2




                        This will let python know that entered input is integer, it could be much worse things than an integer.
                        – Padraic Cunningham
                        Oct 18 '16 at 17:52












                      protected by thefourtheye May 31 '15 at 2:42



                      Thank you for your interest in this question.
                      Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                      Would you like to answer one of these unanswered questions instead?














                      15 Answers
                      15






                      active

                      oldest

                      votes








                      15 Answers
                      15






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes









                      240














                      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, <type 'int'>)


                      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, <type 'int'>)


                      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', <type 'str'>)


                      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', <class 'str'>)




                      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 "<input>", line 1, in <module>
                      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.



                      PS: Python doesn't expect ; at the end of the line :)






                      share|improve this answer























                      • Is there any other way, like a function or something so that we dont need to convert to int in 3.x other than doing explicit conversion to int??
                        – Shreyan Mehta
                        Apr 9 '16 at 6:19










                      • @ShreyanMehta eval would work, but don't go for that unless you have pressing reasons.
                        – thefourtheye
                        Apr 9 '16 at 7:01






                      • 1




                        @thefourtheye at least use ast.literal_eval for that. It does not have the security concerns of eval.
                        – spectras
                        Apr 6 at 12:48






                      • 1




                        I use this Q&A as a dupe target, but maybe you can add a TDLR with the python 3 solution, i.e. int(input()... at the top? Python 2 is nearing the end of it's life and the python 3 info is too buried IMO
                        – Chris_Rands
                        Jul 24 at 14:36






                      • 1




                        @Chris_Rands Sorry, it took a while. I updated with a TLDR now, PTAL.
                        – thefourtheye
                        Oct 17 at 6:01
















                      240














                      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, <type 'int'>)


                      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, <type 'int'>)


                      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', <type 'str'>)


                      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', <class 'str'>)




                      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 "<input>", line 1, in <module>
                      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.



                      PS: Python doesn't expect ; at the end of the line :)






                      share|improve this answer























                      • Is there any other way, like a function or something so that we dont need to convert to int in 3.x other than doing explicit conversion to int??
                        – Shreyan Mehta
                        Apr 9 '16 at 6:19










                      • @ShreyanMehta eval would work, but don't go for that unless you have pressing reasons.
                        – thefourtheye
                        Apr 9 '16 at 7:01






                      • 1




                        @thefourtheye at least use ast.literal_eval for that. It does not have the security concerns of eval.
                        – spectras
                        Apr 6 at 12:48






                      • 1




                        I use this Q&A as a dupe target, but maybe you can add a TDLR with the python 3 solution, i.e. int(input()... at the top? Python 2 is nearing the end of it's life and the python 3 info is too buried IMO
                        – Chris_Rands
                        Jul 24 at 14:36






                      • 1




                        @Chris_Rands Sorry, it took a while. I updated with a TLDR now, PTAL.
                        – thefourtheye
                        Oct 17 at 6:01














                      240












                      240








                      240






                      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, <type 'int'>)


                      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, <type 'int'>)


                      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', <type 'str'>)


                      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', <class 'str'>)




                      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 "<input>", line 1, in <module>
                      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.



                      PS: Python doesn't expect ; at the end of the line :)






                      share|improve this answer














                      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, <type 'int'>)


                      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, <type 'int'>)


                      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', <type 'str'>)


                      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', <class 'str'>)




                      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 "<input>", line 1, in <module>
                      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.



                      PS: Python doesn't expect ; at the end of the line :)







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 23 at 7:26









                      user2357112

                      150k12157247




                      150k12157247










                      answered Dec 8 '13 at 3:08









                      thefourtheye

                      162k26284368




                      162k26284368












                      • Is there any other way, like a function or something so that we dont need to convert to int in 3.x other than doing explicit conversion to int??
                        – Shreyan Mehta
                        Apr 9 '16 at 6:19










                      • @ShreyanMehta eval would work, but don't go for that unless you have pressing reasons.
                        – thefourtheye
                        Apr 9 '16 at 7:01






                      • 1




                        @thefourtheye at least use ast.literal_eval for that. It does not have the security concerns of eval.
                        – spectras
                        Apr 6 at 12:48






                      • 1




                        I use this Q&A as a dupe target, but maybe you can add a TDLR with the python 3 solution, i.e. int(input()... at the top? Python 2 is nearing the end of it's life and the python 3 info is too buried IMO
                        – Chris_Rands
                        Jul 24 at 14:36






                      • 1




                        @Chris_Rands Sorry, it took a while. I updated with a TLDR now, PTAL.
                        – thefourtheye
                        Oct 17 at 6:01


















                      • Is there any other way, like a function or something so that we dont need to convert to int in 3.x other than doing explicit conversion to int??
                        – Shreyan Mehta
                        Apr 9 '16 at 6:19










                      • @ShreyanMehta eval would work, but don't go for that unless you have pressing reasons.
                        – thefourtheye
                        Apr 9 '16 at 7:01






                      • 1




                        @thefourtheye at least use ast.literal_eval for that. It does not have the security concerns of eval.
                        – spectras
                        Apr 6 at 12:48






                      • 1




                        I use this Q&A as a dupe target, but maybe you can add a TDLR with the python 3 solution, i.e. int(input()... at the top? Python 2 is nearing the end of it's life and the python 3 info is too buried IMO
                        – Chris_Rands
                        Jul 24 at 14:36






                      • 1




                        @Chris_Rands Sorry, it took a while. I updated with a TLDR now, PTAL.
                        – thefourtheye
                        Oct 17 at 6:01
















                      Is there any other way, like a function or something so that we dont need to convert to int in 3.x other than doing explicit conversion to int??
                      – Shreyan Mehta
                      Apr 9 '16 at 6:19




                      Is there any other way, like a function or something so that we dont need to convert to int in 3.x other than doing explicit conversion to int??
                      – Shreyan Mehta
                      Apr 9 '16 at 6:19












                      @ShreyanMehta eval would work, but don't go for that unless you have pressing reasons.
                      – thefourtheye
                      Apr 9 '16 at 7:01




                      @ShreyanMehta eval would work, but don't go for that unless you have pressing reasons.
                      – thefourtheye
                      Apr 9 '16 at 7:01




                      1




                      1




                      @thefourtheye at least use ast.literal_eval for that. It does not have the security concerns of eval.
                      – spectras
                      Apr 6 at 12:48




                      @thefourtheye at least use ast.literal_eval for that. It does not have the security concerns of eval.
                      – spectras
                      Apr 6 at 12:48




                      1




                      1




                      I use this Q&A as a dupe target, but maybe you can add a TDLR with the python 3 solution, i.e. int(input()... at the top? Python 2 is nearing the end of it's life and the python 3 info is too buried IMO
                      – Chris_Rands
                      Jul 24 at 14:36




                      I use this Q&A as a dupe target, but maybe you can add a TDLR with the python 3 solution, i.e. int(input()... at the top? Python 2 is nearing the end of it's life and the python 3 info is too buried IMO
                      – Chris_Rands
                      Jul 24 at 14:36




                      1




                      1




                      @Chris_Rands Sorry, it took a while. I updated with a TLDR now, PTAL.
                      – thefourtheye
                      Oct 17 at 6:01




                      @Chris_Rands Sorry, it took a while. I updated with a TLDR now, PTAL.
                      – thefourtheye
                      Oct 17 at 6:01













                      35














                      In Python 3.x, raw_input was renamed to input and the Python 2.x input was removed.



                      This means that, just like raw_input, input in Python 3.x always returns a string object.



                      To fix the problem, you need to explicitly make those inputs into integers by putting them in int:



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


                      Also, Python does not need/use semicolons to end lines. So, having them doesn't do anything positive.






                      share|improve this answer























                      • Nice short answer. There seems to be lots of confusion over what's in Py3x and what's not! Here are the docs for input() [link]docs.python.org/3/library/functions.html#input
                        – MJM
                        Jul 24 at 11:03










                      • this works well, up to a point... if you enter an string (like 'foo') it'll raise ValueError:invalid literal for int() with base 10.... so you need to check before if it's actually an integer (or catch the exception). My question is, what is a pythonic way to do this?
                        – Rodrigo Laguna
                        Nov 12 at 15:14
















                      35














                      In Python 3.x, raw_input was renamed to input and the Python 2.x input was removed.



                      This means that, just like raw_input, input in Python 3.x always returns a string object.



                      To fix the problem, you need to explicitly make those inputs into integers by putting them in int:



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


                      Also, Python does not need/use semicolons to end lines. So, having them doesn't do anything positive.






                      share|improve this answer























                      • Nice short answer. There seems to be lots of confusion over what's in Py3x and what's not! Here are the docs for input() [link]docs.python.org/3/library/functions.html#input
                        – MJM
                        Jul 24 at 11:03










                      • this works well, up to a point... if you enter an string (like 'foo') it'll raise ValueError:invalid literal for int() with base 10.... so you need to check before if it's actually an integer (or catch the exception). My question is, what is a pythonic way to do this?
                        – Rodrigo Laguna
                        Nov 12 at 15:14














                      35












                      35








                      35






                      In Python 3.x, raw_input was renamed to input and the Python 2.x input was removed.



                      This means that, just like raw_input, input in Python 3.x always returns a string object.



                      To fix the problem, you need to explicitly make those inputs into integers by putting them in int:



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


                      Also, Python does not need/use semicolons to end lines. So, having them doesn't do anything positive.






                      share|improve this answer














                      In Python 3.x, raw_input was renamed to input and the Python 2.x input was removed.



                      This means that, just like raw_input, input in Python 3.x always returns a string object.



                      To fix the problem, you need to explicitly make those inputs into integers by putting them in int:



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


                      Also, Python does not need/use semicolons to end lines. So, having them doesn't do anything positive.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 8 '13 at 3:19

























                      answered Dec 8 '13 at 3:09









                      iCodez

                      106k22203215




                      106k22203215












                      • Nice short answer. There seems to be lots of confusion over what's in Py3x and what's not! Here are the docs for input() [link]docs.python.org/3/library/functions.html#input
                        – MJM
                        Jul 24 at 11:03










                      • this works well, up to a point... if you enter an string (like 'foo') it'll raise ValueError:invalid literal for int() with base 10.... so you need to check before if it's actually an integer (or catch the exception). My question is, what is a pythonic way to do this?
                        – Rodrigo Laguna
                        Nov 12 at 15:14


















                      • Nice short answer. There seems to be lots of confusion over what's in Py3x and what's not! Here are the docs for input() [link]docs.python.org/3/library/functions.html#input
                        – MJM
                        Jul 24 at 11:03










                      • this works well, up to a point... if you enter an string (like 'foo') it'll raise ValueError:invalid literal for int() with base 10.... so you need to check before if it's actually an integer (or catch the exception). My question is, what is a pythonic way to do this?
                        – Rodrigo Laguna
                        Nov 12 at 15:14
















                      Nice short answer. There seems to be lots of confusion over what's in Py3x and what's not! Here are the docs for input() [link]docs.python.org/3/library/functions.html#input
                      – MJM
                      Jul 24 at 11:03




                      Nice short answer. There seems to be lots of confusion over what's in Py3x and what's not! Here are the docs for input() [link]docs.python.org/3/library/functions.html#input
                      – MJM
                      Jul 24 at 11:03












                      this works well, up to a point... if you enter an string (like 'foo') it'll raise ValueError:invalid literal for int() with base 10.... so you need to check before if it's actually an integer (or catch the exception). My question is, what is a pythonic way to do this?
                      – Rodrigo Laguna
                      Nov 12 at 15:14




                      this works well, up to a point... if you enter an string (like 'foo') it'll raise ValueError:invalid literal for int() with base 10.... so you need to check before if it's actually an integer (or catch the exception). My question is, what is a pythonic way to do this?
                      – Rodrigo Laguna
                      Nov 12 at 15:14











                      22














                      For multiple integer in a single line, map might be better.



                      arr = map(int, raw_input().split())


                      If the number is already known, (like 2 integers), you can use



                      num1, num2 = map(int, raw_input().split())





                      share|improve this answer




























                        22














                        For multiple integer in a single line, map might be better.



                        arr = map(int, raw_input().split())


                        If the number is already known, (like 2 integers), you can use



                        num1, num2 = map(int, raw_input().split())





                        share|improve this answer


























                          22












                          22








                          22






                          For multiple integer in a single line, map might be better.



                          arr = map(int, raw_input().split())


                          If the number is already known, (like 2 integers), you can use



                          num1, num2 = map(int, raw_input().split())





                          share|improve this answer














                          For multiple integer in a single line, map might be better.



                          arr = map(int, raw_input().split())


                          If the number is already known, (like 2 integers), you can use



                          num1, num2 = map(int, raw_input().split())






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 11 '14 at 0:40

























                          answered Nov 11 '14 at 0:32









                          user1341043

                          22123




                          22123























                              13














                              input() (Python 3) and raw_input() (Python 2) always return strings. Convert the result to integer explicitly with int().



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


                              Pro tip: semi-colons are not needed in Python.






                              share|improve this answer


























                                13














                                input() (Python 3) and raw_input() (Python 2) always return strings. Convert the result to integer explicitly with int().



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


                                Pro tip: semi-colons are not needed in Python.






                                share|improve this answer
























                                  13












                                  13








                                  13






                                  input() (Python 3) and raw_input() (Python 2) always return strings. Convert the result to integer explicitly with int().



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


                                  Pro tip: semi-colons are not needed in Python.






                                  share|improve this answer












                                  input() (Python 3) and raw_input() (Python 2) always return strings. Convert the result to integer explicitly with int().



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


                                  Pro tip: semi-colons are not needed in Python.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Dec 8 '13 at 3:09









                                  Martijn Pieters

                                  698k13124152255




                                  698k13124152255























                                      9














                                      Multiple questions require input for several integers on single line. The best way is to input the whole string of numbers one one line and then split them to integers.



                                       p=raw_input()
                                      p=p.split()
                                      for i in p:
                                      a.append(int(i))





                                      share|improve this answer




























                                        9














                                        Multiple questions require input for several integers on single line. The best way is to input the whole string of numbers one one line and then split them to integers.



                                         p=raw_input()
                                        p=p.split()
                                        for i in p:
                                        a.append(int(i))





                                        share|improve this answer


























                                          9












                                          9








                                          9






                                          Multiple questions require input for several integers on single line. The best way is to input the whole string of numbers one one line and then split them to integers.



                                           p=raw_input()
                                          p=p.split()
                                          for i in p:
                                          a.append(int(i))





                                          share|improve this answer














                                          Multiple questions require input for several integers on single line. The best way is to input the whole string of numbers one one line and then split them to integers.



                                           p=raw_input()
                                          p=p.split()
                                          for i in p:
                                          a.append(int(i))






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Jul 8 '14 at 0:33









                                          Dan Neely

                                          3,2141462105




                                          3,2141462105










                                          answered Jul 8 '14 at 0:13









                                          gumboy

                                          9111




                                          9111























                                              6














                                              Convert to integers:



                                              my_number = int(input("enter the number"))


                                              Similarly for floating point numbers:



                                              my_decimalnumber = float(input("enter the number"))





                                              share|improve this answer




























                                                6














                                                Convert to integers:



                                                my_number = int(input("enter the number"))


                                                Similarly for floating point numbers:



                                                my_decimalnumber = float(input("enter the number"))





                                                share|improve this answer


























                                                  6












                                                  6








                                                  6






                                                  Convert to integers:



                                                  my_number = int(input("enter the number"))


                                                  Similarly for floating point numbers:



                                                  my_decimalnumber = float(input("enter the number"))





                                                  share|improve this answer














                                                  Convert to integers:



                                                  my_number = int(input("enter the number"))


                                                  Similarly for floating point numbers:



                                                  my_decimalnumber = float(input("enter the number"))






                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Jan 26 '17 at 4:28









                                                  xlm

                                                  2,95992837




                                                  2,95992837










                                                  answered Apr 17 '16 at 16:20









                                                  Hemanth Savasere

                                                  7818




                                                  7818























                                                      6














                                                      Taking int as input in python:
                                                      we take a simple string input using:



                                                      input()


                                                      now we want int as input.so we typecast this string to int. simply using:



                                                      int(input())





                                                      share|improve this answer


























                                                        6














                                                        Taking int as input in python:
                                                        we take a simple string input using:



                                                        input()


                                                        now we want int as input.so we typecast this string to int. simply using:



                                                        int(input())





                                                        share|improve this answer
























                                                          6












                                                          6








                                                          6






                                                          Taking int as input in python:
                                                          we take a simple string input using:



                                                          input()


                                                          now we want int as input.so we typecast this string to int. simply using:



                                                          int(input())





                                                          share|improve this answer












                                                          Taking int as input in python:
                                                          we take a simple string input using:



                                                          input()


                                                          now we want int as input.so we typecast this string to int. simply using:



                                                          int(input())






                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Apr 16 '17 at 17:35









                                                          Rohit-Pandey

                                                          911615




                                                          911615























                                                              5














                                                              Python 3.x has input() function which returns always string.So you must convert to int



                                                              python 3.x



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


                                                              python 2.x



                                                              In python 2.x raw_input() and input() functions always return string so you must convert them to int too.



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





                                                              share|improve this answer


























                                                                5














                                                                Python 3.x has input() function which returns always string.So you must convert to int



                                                                python 3.x



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


                                                                python 2.x



                                                                In python 2.x raw_input() and input() functions always return string so you must convert them to int too.



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





                                                                share|improve this answer
























                                                                  5












                                                                  5








                                                                  5






                                                                  Python 3.x has input() function which returns always string.So you must convert to int



                                                                  python 3.x



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


                                                                  python 2.x



                                                                  In python 2.x raw_input() and input() functions always return string so you must convert them to int too.



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





                                                                  share|improve this answer












                                                                  Python 3.x has input() function which returns always string.So you must convert to int



                                                                  python 3.x



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


                                                                  python 2.x



                                                                  In python 2.x raw_input() and input() functions always return string so you must convert them to int too.



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






                                                                  share|improve this answer












                                                                  share|improve this answer



                                                                  share|improve this answer










                                                                  answered Mar 23 '16 at 20:57









                                                                  Harun ERGUL

                                                                  3,01133441




                                                                  3,01133441























                                                                      5














                                                                      In Python 3.x. By default the input function takes input in string format . To convert it into integer you need to include int(input())



                                                                      x=int(input("Enter the number"))





                                                                      share|improve this answer




























                                                                        5














                                                                        In Python 3.x. By default the input function takes input in string format . To convert it into integer you need to include int(input())



                                                                        x=int(input("Enter the number"))





                                                                        share|improve this answer


























                                                                          5












                                                                          5








                                                                          5






                                                                          In Python 3.x. By default the input function takes input in string format . To convert it into integer you need to include int(input())



                                                                          x=int(input("Enter the number"))





                                                                          share|improve this answer














                                                                          In Python 3.x. By default the input function takes input in string format . To convert it into integer you need to include int(input())



                                                                          x=int(input("Enter the number"))






                                                                          share|improve this answer














                                                                          share|improve this answer



                                                                          share|improve this answer








                                                                          edited Jul 13 at 12:55

























                                                                          answered Jun 17 '17 at 13:13









                                                                          Madhusudan chowdary

                                                                          299212




                                                                          299212























                                                                              3














                                                                              I encountered a problem of taking integer input while solving a problem on CodeChef, where two integers - separated by space - should be read from one line.



                                                                              While int(input()) is sufficient for a single integer, I did not find a direct way to input two integers. I tried this:



                                                                              num = input()
                                                                              num1 = 0
                                                                              num2 = 0

                                                                              for i in range(len(num)):
                                                                              if num[i] == ' ':
                                                                              break

                                                                              num1 = int(num[:i])
                                                                              num2 = int(num[i+1:])


                                                                              Now I use num1 and num2 as integers. Hope this helps.






                                                                              share|improve this answer





















                                                                              • This looks very interesting. However, isn't i destroyed when the for loop is exited?
                                                                                – Hosch250
                                                                                May 23 '14 at 16:33












                                                                              • @hosch250 When a loop is exited, the value of the index variable (here, i) remains. I tried this piece out, and it works correctly.
                                                                                – Aravind
                                                                                May 24 '14 at 15:18










                                                                              • For this kind of input manipulation, you can either num1, num2 = map(int, input().split()) if you know how much integers you will encounter or nums = list(map(int, input().split())) if you don't.
                                                                                – Mathias Ettinger
                                                                                Jul 12 at 12:58
















                                                                              3














                                                                              I encountered a problem of taking integer input while solving a problem on CodeChef, where two integers - separated by space - should be read from one line.



                                                                              While int(input()) is sufficient for a single integer, I did not find a direct way to input two integers. I tried this:



                                                                              num = input()
                                                                              num1 = 0
                                                                              num2 = 0

                                                                              for i in range(len(num)):
                                                                              if num[i] == ' ':
                                                                              break

                                                                              num1 = int(num[:i])
                                                                              num2 = int(num[i+1:])


                                                                              Now I use num1 and num2 as integers. Hope this helps.






                                                                              share|improve this answer





















                                                                              • This looks very interesting. However, isn't i destroyed when the for loop is exited?
                                                                                – Hosch250
                                                                                May 23 '14 at 16:33












                                                                              • @hosch250 When a loop is exited, the value of the index variable (here, i) remains. I tried this piece out, and it works correctly.
                                                                                – Aravind
                                                                                May 24 '14 at 15:18










                                                                              • For this kind of input manipulation, you can either num1, num2 = map(int, input().split()) if you know how much integers you will encounter or nums = list(map(int, input().split())) if you don't.
                                                                                – Mathias Ettinger
                                                                                Jul 12 at 12:58














                                                                              3












                                                                              3








                                                                              3






                                                                              I encountered a problem of taking integer input while solving a problem on CodeChef, where two integers - separated by space - should be read from one line.



                                                                              While int(input()) is sufficient for a single integer, I did not find a direct way to input two integers. I tried this:



                                                                              num = input()
                                                                              num1 = 0
                                                                              num2 = 0

                                                                              for i in range(len(num)):
                                                                              if num[i] == ' ':
                                                                              break

                                                                              num1 = int(num[:i])
                                                                              num2 = int(num[i+1:])


                                                                              Now I use num1 and num2 as integers. Hope this helps.






                                                                              share|improve this answer












                                                                              I encountered a problem of taking integer input while solving a problem on CodeChef, where two integers - separated by space - should be read from one line.



                                                                              While int(input()) is sufficient for a single integer, I did not find a direct way to input two integers. I tried this:



                                                                              num = input()
                                                                              num1 = 0
                                                                              num2 = 0

                                                                              for i in range(len(num)):
                                                                              if num[i] == ' ':
                                                                              break

                                                                              num1 = int(num[:i])
                                                                              num2 = int(num[i+1:])


                                                                              Now I use num1 and num2 as integers. Hope this helps.







                                                                              share|improve this answer












                                                                              share|improve this answer



                                                                              share|improve this answer










                                                                              answered May 23 '14 at 11:32









                                                                              Aravind

                                                                              392




                                                                              392












                                                                              • This looks very interesting. However, isn't i destroyed when the for loop is exited?
                                                                                – Hosch250
                                                                                May 23 '14 at 16:33












                                                                              • @hosch250 When a loop is exited, the value of the index variable (here, i) remains. I tried this piece out, and it works correctly.
                                                                                – Aravind
                                                                                May 24 '14 at 15:18










                                                                              • For this kind of input manipulation, you can either num1, num2 = map(int, input().split()) if you know how much integers you will encounter or nums = list(map(int, input().split())) if you don't.
                                                                                – Mathias Ettinger
                                                                                Jul 12 at 12:58


















                                                                              • This looks very interesting. However, isn't i destroyed when the for loop is exited?
                                                                                – Hosch250
                                                                                May 23 '14 at 16:33












                                                                              • @hosch250 When a loop is exited, the value of the index variable (here, i) remains. I tried this piece out, and it works correctly.
                                                                                – Aravind
                                                                                May 24 '14 at 15:18










                                                                              • For this kind of input manipulation, you can either num1, num2 = map(int, input().split()) if you know how much integers you will encounter or nums = list(map(int, input().split())) if you don't.
                                                                                – Mathias Ettinger
                                                                                Jul 12 at 12:58
















                                                                              This looks very interesting. However, isn't i destroyed when the for loop is exited?
                                                                              – Hosch250
                                                                              May 23 '14 at 16:33






                                                                              This looks very interesting. However, isn't i destroyed when the for loop is exited?
                                                                              – Hosch250
                                                                              May 23 '14 at 16:33














                                                                              @hosch250 When a loop is exited, the value of the index variable (here, i) remains. I tried this piece out, and it works correctly.
                                                                              – Aravind
                                                                              May 24 '14 at 15:18




                                                                              @hosch250 When a loop is exited, the value of the index variable (here, i) remains. I tried this piece out, and it works correctly.
                                                                              – Aravind
                                                                              May 24 '14 at 15:18












                                                                              For this kind of input manipulation, you can either num1, num2 = map(int, input().split()) if you know how much integers you will encounter or nums = list(map(int, input().split())) if you don't.
                                                                              – Mathias Ettinger
                                                                              Jul 12 at 12:58




                                                                              For this kind of input manipulation, you can either num1, num2 = map(int, input().split()) if you know how much integers you will encounter or nums = list(map(int, input().split())) if you don't.
                                                                              – Mathias Ettinger
                                                                              Jul 12 at 12:58











                                                                              3














                                                                              def dbz():
                                                                              try:
                                                                              r = raw_input("Enter number:")
                                                                              if r.isdigit():
                                                                              i = int(raw_input("Enter divident:"))
                                                                              d = int(r)/i
                                                                              print "O/p is -:",d
                                                                              else:
                                                                              print "Not a number"
                                                                              except Exception ,e:
                                                                              print "Program halted incorrect data entered",type(e)
                                                                              dbz()

                                                                              Or

                                                                              num = input("Enter Number:")#"input" will accept only numbers





                                                                              share|improve this answer




























                                                                                3














                                                                                def dbz():
                                                                                try:
                                                                                r = raw_input("Enter number:")
                                                                                if r.isdigit():
                                                                                i = int(raw_input("Enter divident:"))
                                                                                d = int(r)/i
                                                                                print "O/p is -:",d
                                                                                else:
                                                                                print "Not a number"
                                                                                except Exception ,e:
                                                                                print "Program halted incorrect data entered",type(e)
                                                                                dbz()

                                                                                Or

                                                                                num = input("Enter Number:")#"input" will accept only numbers





                                                                                share|improve this answer


























                                                                                  3












                                                                                  3








                                                                                  3






                                                                                  def dbz():
                                                                                  try:
                                                                                  r = raw_input("Enter number:")
                                                                                  if r.isdigit():
                                                                                  i = int(raw_input("Enter divident:"))
                                                                                  d = int(r)/i
                                                                                  print "O/p is -:",d
                                                                                  else:
                                                                                  print "Not a number"
                                                                                  except Exception ,e:
                                                                                  print "Program halted incorrect data entered",type(e)
                                                                                  dbz()

                                                                                  Or

                                                                                  num = input("Enter Number:")#"input" will accept only numbers





                                                                                  share|improve this answer














                                                                                  def dbz():
                                                                                  try:
                                                                                  r = raw_input("Enter number:")
                                                                                  if r.isdigit():
                                                                                  i = int(raw_input("Enter divident:"))
                                                                                  d = int(r)/i
                                                                                  print "O/p is -:",d
                                                                                  else:
                                                                                  print "Not a number"
                                                                                  except Exception ,e:
                                                                                  print "Program halted incorrect data entered",type(e)
                                                                                  dbz()

                                                                                  Or

                                                                                  num = input("Enter Number:")#"input" will accept only numbers






                                                                                  share|improve this answer














                                                                                  share|improve this answer



                                                                                  share|improve this answer








                                                                                  edited Jul 9 '15 at 11:47

























                                                                                  answered Jun 30 '15 at 9:16









                                                                                  Sanyal

                                                                                  600618




                                                                                  600618























                                                                                      2














                                                                                      While in your example, int(input(...)) does the trick in any case, python-future's builtins.input is worth consideration since that makes sure your code works for both Python 2 and 3 and disables Python2's default behaviour of input trying to be "clever" about the input data type (builtins.input basically just behaves like raw_input).






                                                                                      share|improve this answer


























                                                                                        2














                                                                                        While in your example, int(input(...)) does the trick in any case, python-future's builtins.input is worth consideration since that makes sure your code works for both Python 2 and 3 and disables Python2's default behaviour of input trying to be "clever" about the input data type (builtins.input basically just behaves like raw_input).






                                                                                        share|improve this answer
























                                                                                          2












                                                                                          2








                                                                                          2






                                                                                          While in your example, int(input(...)) does the trick in any case, python-future's builtins.input is worth consideration since that makes sure your code works for both Python 2 and 3 and disables Python2's default behaviour of input trying to be "clever" about the input data type (builtins.input basically just behaves like raw_input).






                                                                                          share|improve this answer












                                                                                          While in your example, int(input(...)) does the trick in any case, python-future's builtins.input is worth consideration since that makes sure your code works for both Python 2 and 3 and disables Python2's default behaviour of input trying to be "clever" about the input data type (builtins.input basically just behaves like raw_input).







                                                                                          share|improve this answer












                                                                                          share|improve this answer



                                                                                          share|improve this answer










                                                                                          answered Nov 23 '16 at 12:19









                                                                                          Tobias Kienzler

                                                                                          10.5k1679173




                                                                                          10.5k1679173























                                                                                              2














                                                                                              n=int(input())
                                                                                              for i in range(n):
                                                                                              n=input()
                                                                                              n=int(n)
                                                                                              arr1=list(map(int,input().split()))


                                                                                              the for loop shall run 'n' number of times . the second 'n' is the length of the array.
                                                                                              the last statement maps the integers to a list and takes input in space separated form .
                                                                                              you can also return the array at the end of for loop.






                                                                                              share|improve this answer


























                                                                                                2














                                                                                                n=int(input())
                                                                                                for i in range(n):
                                                                                                n=input()
                                                                                                n=int(n)
                                                                                                arr1=list(map(int,input().split()))


                                                                                                the for loop shall run 'n' number of times . the second 'n' is the length of the array.
                                                                                                the last statement maps the integers to a list and takes input in space separated form .
                                                                                                you can also return the array at the end of for loop.






                                                                                                share|improve this answer
























                                                                                                  2












                                                                                                  2








                                                                                                  2






                                                                                                  n=int(input())
                                                                                                  for i in range(n):
                                                                                                  n=input()
                                                                                                  n=int(n)
                                                                                                  arr1=list(map(int,input().split()))


                                                                                                  the for loop shall run 'n' number of times . the second 'n' is the length of the array.
                                                                                                  the last statement maps the integers to a list and takes input in space separated form .
                                                                                                  you can also return the array at the end of for loop.






                                                                                                  share|improve this answer












                                                                                                  n=int(input())
                                                                                                  for i in range(n):
                                                                                                  n=input()
                                                                                                  n=int(n)
                                                                                                  arr1=list(map(int,input().split()))


                                                                                                  the for loop shall run 'n' number of times . the second 'n' is the length of the array.
                                                                                                  the last statement maps the integers to a list and takes input in space separated form .
                                                                                                  you can also return the array at the end of for loop.







                                                                                                  share|improve this answer












                                                                                                  share|improve this answer



                                                                                                  share|improve this answer










                                                                                                  answered Aug 3 at 16:30









                                                                                                  ravi tanwar

                                                                                                  10110




                                                                                                  10110























                                                                                                      2














                                                                                                      play = True

                                                                                                      while play:

                                                                                                      #you can simply contain the input function inside an int function i.e int(input(""))
                                                                                                      #This will only accept int inputs
                                                                                                      # and can also convert any variable to 'int' form

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

                                                                                                      print(x + y)
                                                                                                      print(x - y)
                                                                                                      print(x * y)
                                                                                                      print(x / y)
                                                                                                      print(x % y)

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





                                                                                                      share|improve this answer























                                                                                                      • While this code block may answer the question, it would be best if you could provide a little explanation for why it does so. Please edit your answer to include such a description.
                                                                                                        – Artjom B.
                                                                                                        Oct 14 at 13:11
















                                                                                                      2














                                                                                                      play = True

                                                                                                      while play:

                                                                                                      #you can simply contain the input function inside an int function i.e int(input(""))
                                                                                                      #This will only accept int inputs
                                                                                                      # and can also convert any variable to 'int' form

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

                                                                                                      print(x + y)
                                                                                                      print(x - y)
                                                                                                      print(x * y)
                                                                                                      print(x / y)
                                                                                                      print(x % y)

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





                                                                                                      share|improve this answer























                                                                                                      • While this code block may answer the question, it would be best if you could provide a little explanation for why it does so. Please edit your answer to include such a description.
                                                                                                        – Artjom B.
                                                                                                        Oct 14 at 13:11














                                                                                                      2












                                                                                                      2








                                                                                                      2






                                                                                                      play = True

                                                                                                      while play:

                                                                                                      #you can simply contain the input function inside an int function i.e int(input(""))
                                                                                                      #This will only accept int inputs
                                                                                                      # and can also convert any variable to 'int' form

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

                                                                                                      print(x + y)
                                                                                                      print(x - y)
                                                                                                      print(x * y)
                                                                                                      print(x / y)
                                                                                                      print(x % y)

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





                                                                                                      share|improve this answer














                                                                                                      play = True

                                                                                                      while play:

                                                                                                      #you can simply contain the input function inside an int function i.e int(input(""))
                                                                                                      #This will only accept int inputs
                                                                                                      # and can also convert any variable to 'int' form

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

                                                                                                      print(x + y)
                                                                                                      print(x - y)
                                                                                                      print(x * y)
                                                                                                      print(x / y)
                                                                                                      print(x % y)

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






                                                                                                      share|improve this answer














                                                                                                      share|improve this answer



                                                                                                      share|improve this answer








                                                                                                      edited Oct 14 at 13:11









                                                                                                      Artjom B.

                                                                                                      52.7k1779144




                                                                                                      52.7k1779144










                                                                                                      answered Oct 14 at 12:51









                                                                                                      uday more

                                                                                                      362




                                                                                                      362












                                                                                                      • While this code block may answer the question, it would be best if you could provide a little explanation for why it does so. Please edit your answer to include such a description.
                                                                                                        – Artjom B.
                                                                                                        Oct 14 at 13:11


















                                                                                                      • While this code block may answer the question, it would be best if you could provide a little explanation for why it does so. Please edit your answer to include such a description.
                                                                                                        – Artjom B.
                                                                                                        Oct 14 at 13:11
















                                                                                                      While this code block may answer the question, it would be best if you could provide a little explanation for why it does so. Please edit your answer to include such a description.
                                                                                                      – Artjom B.
                                                                                                      Oct 14 at 13:11




                                                                                                      While this code block may answer the question, it would be best if you could provide a little explanation for why it does so. Please edit your answer to include such a description.
                                                                                                      – Artjom B.
                                                                                                      Oct 14 at 13:11











                                                                                                      1














                                                                                                      Yes, in python 3.x, raw_input is replaced with input. In order to revert to old behavior of input use:



                                                                                                      eval(input("Enter a number: "))



                                                                                                      This will let python know that entered input is integer






                                                                                                      share|improve this answer





















                                                                                                      • Is this correct?
                                                                                                        – tjt263
                                                                                                        Mar 8 '16 at 13:46










                                                                                                      • Yes, you may try please
                                                                                                        – Waseem Akhtar
                                                                                                        Jul 3 '16 at 11:26






                                                                                                      • 2




                                                                                                        This will let python know that entered input is integer, it could be much worse things than an integer.
                                                                                                        – Padraic Cunningham
                                                                                                        Oct 18 '16 at 17:52


















                                                                                                      1














                                                                                                      Yes, in python 3.x, raw_input is replaced with input. In order to revert to old behavior of input use:



                                                                                                      eval(input("Enter a number: "))



                                                                                                      This will let python know that entered input is integer






                                                                                                      share|improve this answer





















                                                                                                      • Is this correct?
                                                                                                        – tjt263
                                                                                                        Mar 8 '16 at 13:46










                                                                                                      • Yes, you may try please
                                                                                                        – Waseem Akhtar
                                                                                                        Jul 3 '16 at 11:26






                                                                                                      • 2




                                                                                                        This will let python know that entered input is integer, it could be much worse things than an integer.
                                                                                                        – Padraic Cunningham
                                                                                                        Oct 18 '16 at 17:52
















                                                                                                      1












                                                                                                      1








                                                                                                      1






                                                                                                      Yes, in python 3.x, raw_input is replaced with input. In order to revert to old behavior of input use:



                                                                                                      eval(input("Enter a number: "))



                                                                                                      This will let python know that entered input is integer






                                                                                                      share|improve this answer












                                                                                                      Yes, in python 3.x, raw_input is replaced with input. In order to revert to old behavior of input use:



                                                                                                      eval(input("Enter a number: "))



                                                                                                      This will let python know that entered input is integer







                                                                                                      share|improve this answer












                                                                                                      share|improve this answer



                                                                                                      share|improve this answer










                                                                                                      answered Feb 21 '15 at 11:52









                                                                                                      Waseem Akhtar

                                                                                                      352




                                                                                                      352












                                                                                                      • Is this correct?
                                                                                                        – tjt263
                                                                                                        Mar 8 '16 at 13:46










                                                                                                      • Yes, you may try please
                                                                                                        – Waseem Akhtar
                                                                                                        Jul 3 '16 at 11:26






                                                                                                      • 2




                                                                                                        This will let python know that entered input is integer, it could be much worse things than an integer.
                                                                                                        – Padraic Cunningham
                                                                                                        Oct 18 '16 at 17:52




















                                                                                                      • Is this correct?
                                                                                                        – tjt263
                                                                                                        Mar 8 '16 at 13:46










                                                                                                      • Yes, you may try please
                                                                                                        – Waseem Akhtar
                                                                                                        Jul 3 '16 at 11:26






                                                                                                      • 2




                                                                                                        This will let python know that entered input is integer, it could be much worse things than an integer.
                                                                                                        – Padraic Cunningham
                                                                                                        Oct 18 '16 at 17:52


















                                                                                                      Is this correct?
                                                                                                      – tjt263
                                                                                                      Mar 8 '16 at 13:46




                                                                                                      Is this correct?
                                                                                                      – tjt263
                                                                                                      Mar 8 '16 at 13:46












                                                                                                      Yes, you may try please
                                                                                                      – Waseem Akhtar
                                                                                                      Jul 3 '16 at 11:26




                                                                                                      Yes, you may try please
                                                                                                      – Waseem Akhtar
                                                                                                      Jul 3 '16 at 11:26




                                                                                                      2




                                                                                                      2




                                                                                                      This will let python know that entered input is integer, it could be much worse things than an integer.
                                                                                                      – Padraic Cunningham
                                                                                                      Oct 18 '16 at 17:52






                                                                                                      This will let python know that entered input is integer, it could be much worse things than an integer.
                                                                                                      – Padraic Cunningham
                                                                                                      Oct 18 '16 at 17:52







                                                                                                      protected by thefourtheye May 31 '15 at 2:42



                                                                                                      Thank you for your interest in this question.
                                                                                                      Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                                                                      Would you like to answer one of these unanswered questions instead?



                                                                                                      Popular posts from this blog

                                                                                                      If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                                                                                                      Alcedinidae

                                                                                                      Can an atomic nucleus contain both particles and antiparticles? [duplicate]