How can I read inputs as numbers? Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!TypeError: '<=' not supported between instances of 'str' and 'int'How do I do simple user input in python?unsupported operand type(s) for /: 'str' and 'int'Using a for loop to add values in a listPython not summing (add) numbers, just sticking them togetherPython 3.4 User InputRaw Input If Statements PythonHow do I compare a string and an integer in Python?Checking if input is in a list of numbers in pythonHow do I multiply a variable by itself?How can I represent an 'Enum' in Python?How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory in Python?How can I make a time delay in Python?How do I get the number of elements in a list in Python?How do I list all files of a directory?How to read a file line-by-line into a list?How do I convert a String to an int in Java?

How would this chord from "Rocket Man" be analyzed?

Is there any hidden 'W' sound after 'comment' in : Comment est-elle?

Why is an operator the quantum mechanical analogue of an observable?

Can you stand up from being prone using Skirmisher outside of your turn?

Suing a Police Officer Instead of the Police Department

What ability score does a Hexblade's Pact Weapon use for attack and damage when wielded by another character?

Why did C use the -> operator instead of reusing the . operator?

Multiple fireplaces in an apartment building?

Book with legacy programming code on a space ship that the main character hacks to escape

Has a Nobel Peace laureate ever been accused of war crimes?

Co-worker works way more than he should

finding a tangent line to a parabola

Are these square matrices always diagonalisable?

What is it called when you ride around on your front wheel?

Is Electric Central Heating worth it if using Solar Panels?

What is the ongoing value of the Kanban board to the developers as opposed to management

What is the least dense liquid under normal conditions?

Why does the Cisco show run command not show the full version, while the show version command does?

France's Public Holidays' Puzzle

Justification for leaving new position after a short time

Did the Roman Empire have penal colonies?

Does Mathematica have an implementation of the Poisson Binomial Distribution?

Is accepting an invalid credit card number a security issue?

Implementing 3DES algorithm in Java: is my code secure?



How can I read inputs as numbers?



Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!TypeError: '<=' not supported between instances of 'str' and 'int'How do I do simple user input in python?unsupported operand type(s) for /: 'str' and 'int'Using a for loop to add values in a listPython not summing (add) numbers, just sticking them togetherPython 3.4 User InputRaw Input If Statements PythonHow do I compare a string and an integer in Python?Checking if input is in a list of numbers in pythonHow do I multiply a variable by itself?How can I represent an 'Enum' in Python?How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory in Python?How can I make a time delay in Python?How do I get the number of elements in a list in Python?How do I list all files of a directory?How to read a file line-by-line into a list?How do I convert a String to an int in Java?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








259















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 '18 at 20:50

















259















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 '18 at 20:50













259












259








259


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 '18 at 7:24









user2357112

159k13177272




159k13177272










asked Dec 8 '13 at 3:08









Hosch250Hosch250

2,46232144




2,46232144












  • asking-the-user-for-input-until-they-give-a-valid-response

    – Patrick Artner
    Sep 13 '18 at 20:50

















  • asking-the-user-for-input-until-they-give-a-valid-response

    – Patrick Artner
    Sep 13 '18 at 20:50
















asking-the-user-for-input-until-they-give-a-valid-response

– Patrick Artner
Sep 13 '18 at 20:50





asking-the-user-for-input-until-they-give-a-valid-response

– Patrick Artner
Sep 13 '18 at 20:50












19 Answers
19






active

oldest

votes


















268














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






  • 1





    Could you please add a tl;dr version to the beginning of the answer? It's a long answer, and I believe many will not bother to read it, and will just use input() and eval(input())

    – Elazar
    Nov 13 '17 at 1:19






  • 1





    @thefourtheye at least use ast.literal_eval for that. It does not have the security concerns of eval.

    – spectras
    Apr 6 '18 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 '18 at 14:36






  • 1





    @Chris_Rands Sorry, it took a while. I updated with a TLDR now, PTAL.

    – thefourtheye
    Oct 17 '18 at 6:01


















39














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 '18 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 '18 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














          Python 3.x

          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 '18 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 '18 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












                      • eval() just evaluates the input, the OP wants several things done with the two inputs.

                        – user11093202
                        Mar 27 at 0:46


















                      0














                      Lots of people like to do int(input("etc")), but if you put in a string, it will raise an error.



                      What I like to do is this:



                      s = input("integer: ")
                      integer = int(s)


                      Now use integer for whatever purpose!!



                      Plus, if s is an string, this doesn't raise an error!!!



                      At least, not immediately.






                      share|improve this answer























                      • Both forms throw the same error, "ValueError: invalid literal for int() with base 10"

                        – cdlane
                        Mar 31 at 4:39











                      • I know, but not immediately when the user types something different@cdlane

                        – user11093202
                        Apr 1 at 19:50



















                      0














                      parse the input into an integer with int



                      x = int(input("What is x?"))





                      share|improve this answer

























                      • it is highly encouraged for new users to attempt to give a relevant answers to questions and to share their knowledge, but please make sure to read the Stackoverflow: how to write a good answer? guide before attempting to do so. Additionally, please check previous answers and make sure that your answer brings a new perspective or some sort of additional information to the answers that have already been given (and approved) and please include an explanation when sharing a block of code.

                        – andreihondrari
                        Apr 2 at 15:16


















                      0














                      play = True

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

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

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


                      This makes it know that the variable x and y are integers and therefore uses them like integers.

                      If you want it to be able to use decimals change int to float.



                      The output for the first function:



                      print(int(x) + int(y))


                      x = 3 and y = 7 returned 10.






                      share|improve this answer

























                      • Your code won't work. The indentation breaks the while loop

                        – Zoe
                        Apr 2 at 14:58


















                      -1














                      Try this,



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


                      int() is a built-in python function that converts any datatype to a integer.






                      share|improve this answer


















                      • 2





                        Welcome to Stack Overflow! Does this answer add anything new to the existing ones?

                        – iBug
                        Dec 26 '18 at 12:39









                      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?














                      19 Answers
                      19






                      active

                      oldest

                      votes








                      19 Answers
                      19






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes









                      268














                      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






                      • 1





                        Could you please add a tl;dr version to the beginning of the answer? It's a long answer, and I believe many will not bother to read it, and will just use input() and eval(input())

                        – Elazar
                        Nov 13 '17 at 1:19






                      • 1





                        @thefourtheye at least use ast.literal_eval for that. It does not have the security concerns of eval.

                        – spectras
                        Apr 6 '18 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 '18 at 14:36






                      • 1





                        @Chris_Rands Sorry, it took a while. I updated with a TLDR now, PTAL.

                        – thefourtheye
                        Oct 17 '18 at 6:01















                      268














                      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






                      • 1





                        Could you please add a tl;dr version to the beginning of the answer? It's a long answer, and I believe many will not bother to read it, and will just use input() and eval(input())

                        – Elazar
                        Nov 13 '17 at 1:19






                      • 1





                        @thefourtheye at least use ast.literal_eval for that. It does not have the security concerns of eval.

                        – spectras
                        Apr 6 '18 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 '18 at 14:36






                      • 1





                        @Chris_Rands Sorry, it took a while. I updated with a TLDR now, PTAL.

                        – thefourtheye
                        Oct 17 '18 at 6:01













                      268












                      268








                      268







                      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 '18 at 7:26









                      user2357112

                      159k13177272




                      159k13177272










                      answered Dec 8 '13 at 3:08









                      thefourtheyethefourtheye

                      168k28304380




                      168k28304380












                      • 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






                      • 1





                        Could you please add a tl;dr version to the beginning of the answer? It's a long answer, and I believe many will not bother to read it, and will just use input() and eval(input())

                        – Elazar
                        Nov 13 '17 at 1:19






                      • 1





                        @thefourtheye at least use ast.literal_eval for that. It does not have the security concerns of eval.

                        – spectras
                        Apr 6 '18 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 '18 at 14:36






                      • 1





                        @Chris_Rands Sorry, it took a while. I updated with a TLDR now, PTAL.

                        – thefourtheye
                        Oct 17 '18 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






                      • 1





                        Could you please add a tl;dr version to the beginning of the answer? It's a long answer, and I believe many will not bother to read it, and will just use input() and eval(input())

                        – Elazar
                        Nov 13 '17 at 1:19






                      • 1





                        @thefourtheye at least use ast.literal_eval for that. It does not have the security concerns of eval.

                        – spectras
                        Apr 6 '18 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 '18 at 14:36






                      • 1





                        @Chris_Rands Sorry, it took a while. I updated with a TLDR now, PTAL.

                        – thefourtheye
                        Oct 17 '18 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




                      1




                      1





                      Could you please add a tl;dr version to the beginning of the answer? It's a long answer, and I believe many will not bother to read it, and will just use input() and eval(input())

                      – Elazar
                      Nov 13 '17 at 1:19





                      Could you please add a tl;dr version to the beginning of the answer? It's a long answer, and I believe many will not bother to read it, and will just use input() and eval(input())

                      – Elazar
                      Nov 13 '17 at 1:19




                      1




                      1





                      @thefourtheye at least use ast.literal_eval for that. It does not have the security concerns of eval.

                      – spectras
                      Apr 6 '18 at 12:48





                      @thefourtheye at least use ast.literal_eval for that. It does not have the security concerns of eval.

                      – spectras
                      Apr 6 '18 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 '18 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 '18 at 14:36




                      1




                      1





                      @Chris_Rands Sorry, it took a while. I updated with a TLDR now, PTAL.

                      – thefourtheye
                      Oct 17 '18 at 6:01





                      @Chris_Rands Sorry, it took a while. I updated with a TLDR now, PTAL.

                      – thefourtheye
                      Oct 17 '18 at 6:01













                      39














                      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 '18 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 '18 at 15:14















                      39














                      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 '18 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 '18 at 15:14













                      39












                      39








                      39







                      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









                      iCodeziCodez

                      111k22219224




                      111k22219224












                      • 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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









                          user1341043user1341043

                          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 PietersMartijn Pieters

                                  729k14625612360




                                  729k14625612360





















                                      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,2641466108




                                          3,2641466108










                                          answered Jul 8 '14 at 0:13









                                          gumboygumboy

                                          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

                                                  3,20493440




                                                  3,20493440










                                                  answered Apr 17 '16 at 16:20









                                                  Hemanth SavasereHemanth Savasere

                                                  9319




                                                  9319





















                                                      6














                                                      Python 3.x

                                                      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














                                                        Python 3.x

                                                        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







                                                          Python 3.x

                                                          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















                                                          Python 3.x

                                                          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








                                                          edited Apr 4 at 6:14









                                                          Jagoda Sokół

                                                          162317




                                                          162317










                                                          answered Apr 16 '17 at 17:35









                                                          Rohit-PandeyRohit-Pandey

                                                          1,122917




                                                          1,122917





















                                                              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 ERGULHarun ERGUL

                                                                  3,28633846




                                                                  3,28633846





















                                                                      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 Apr 4 at 6:09









                                                                          Jagoda Sokół

                                                                          162317




                                                                          162317










                                                                          answered Jun 17 '17 at 13:13









                                                                          Madhusudan chowdaryMadhusudan chowdary

                                                                          329513




                                                                          329513





















                                                                              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 '18 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 '18 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









                                                                              AravindAravind

                                                                              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 '18 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 '18 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 '18 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 '18 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 Apr 4 at 8:47

























                                                                                  answered Jun 30 '15 at 9:16









                                                                                  SanyalSanyal

                                                                                  645718




                                                                                  645718





















                                                                                      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 KienzlerTobias Kienzler

                                                                                          11.1k1685178




                                                                                          11.1k1685178





















                                                                                              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 '18 at 16:30









                                                                                                  ravi tanwarravi tanwar

                                                                                                  11911




                                                                                                  11911





















                                                                                                      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 '18 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 '18 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 '18 at 13:11









                                                                                                      Artjom B.

                                                                                                      53.4k1783155




                                                                                                      53.4k1783155










                                                                                                      answered Oct 14 '18 at 12:51









                                                                                                      uday moreuday more

                                                                                                      562




                                                                                                      562












                                                                                                      • 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 '18 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 '18 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 '18 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 '18 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












                                                                                                      • eval() just evaluates the input, the OP wants several things done with the two inputs.

                                                                                                        – user11093202
                                                                                                        Mar 27 at 0:46















                                                                                                      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












                                                                                                      • eval() just evaluates the input, the OP wants several things done with the two inputs.

                                                                                                        – user11093202
                                                                                                        Mar 27 at 0:46













                                                                                                      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 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












                                                                                                      • eval() just evaluates the input, the OP wants several things done with the two inputs.

                                                                                                        – user11093202
                                                                                                        Mar 27 at 0:46

















                                                                                                      • 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












                                                                                                      • eval() just evaluates the input, the OP wants several things done with the two inputs.

                                                                                                        – user11093202
                                                                                                        Mar 27 at 0:46
















                                                                                                      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














                                                                                                      eval() just evaluates the input, the OP wants several things done with the two inputs.

                                                                                                      – user11093202
                                                                                                      Mar 27 at 0:46





                                                                                                      eval() just evaluates the input, the OP wants several things done with the two inputs.

                                                                                                      – user11093202
                                                                                                      Mar 27 at 0:46











                                                                                                      0














                                                                                                      Lots of people like to do int(input("etc")), but if you put in a string, it will raise an error.



                                                                                                      What I like to do is this:



                                                                                                      s = input("integer: ")
                                                                                                      integer = int(s)


                                                                                                      Now use integer for whatever purpose!!



                                                                                                      Plus, if s is an string, this doesn't raise an error!!!



                                                                                                      At least, not immediately.






                                                                                                      share|improve this answer























                                                                                                      • Both forms throw the same error, "ValueError: invalid literal for int() with base 10"

                                                                                                        – cdlane
                                                                                                        Mar 31 at 4:39











                                                                                                      • I know, but not immediately when the user types something different@cdlane

                                                                                                        – user11093202
                                                                                                        Apr 1 at 19:50
















                                                                                                      0














                                                                                                      Lots of people like to do int(input("etc")), but if you put in a string, it will raise an error.



                                                                                                      What I like to do is this:



                                                                                                      s = input("integer: ")
                                                                                                      integer = int(s)


                                                                                                      Now use integer for whatever purpose!!



                                                                                                      Plus, if s is an string, this doesn't raise an error!!!



                                                                                                      At least, not immediately.






                                                                                                      share|improve this answer























                                                                                                      • Both forms throw the same error, "ValueError: invalid literal for int() with base 10"

                                                                                                        – cdlane
                                                                                                        Mar 31 at 4:39











                                                                                                      • I know, but not immediately when the user types something different@cdlane

                                                                                                        – user11093202
                                                                                                        Apr 1 at 19:50














                                                                                                      0












                                                                                                      0








                                                                                                      0







                                                                                                      Lots of people like to do int(input("etc")), but if you put in a string, it will raise an error.



                                                                                                      What I like to do is this:



                                                                                                      s = input("integer: ")
                                                                                                      integer = int(s)


                                                                                                      Now use integer for whatever purpose!!



                                                                                                      Plus, if s is an string, this doesn't raise an error!!!



                                                                                                      At least, not immediately.






                                                                                                      share|improve this answer













                                                                                                      Lots of people like to do int(input("etc")), but if you put in a string, it will raise an error.



                                                                                                      What I like to do is this:



                                                                                                      s = input("integer: ")
                                                                                                      integer = int(s)


                                                                                                      Now use integer for whatever purpose!!



                                                                                                      Plus, if s is an string, this doesn't raise an error!!!



                                                                                                      At least, not immediately.







                                                                                                      share|improve this answer












                                                                                                      share|improve this answer



                                                                                                      share|improve this answer










                                                                                                      answered Mar 27 at 0:45







                                                                                                      user11093202



















                                                                                                      • Both forms throw the same error, "ValueError: invalid literal for int() with base 10"

                                                                                                        – cdlane
                                                                                                        Mar 31 at 4:39











                                                                                                      • I know, but not immediately when the user types something different@cdlane

                                                                                                        – user11093202
                                                                                                        Apr 1 at 19:50


















                                                                                                      • Both forms throw the same error, "ValueError: invalid literal for int() with base 10"

                                                                                                        – cdlane
                                                                                                        Mar 31 at 4:39











                                                                                                      • I know, but not immediately when the user types something different@cdlane

                                                                                                        – user11093202
                                                                                                        Apr 1 at 19:50

















                                                                                                      Both forms throw the same error, "ValueError: invalid literal for int() with base 10"

                                                                                                      – cdlane
                                                                                                      Mar 31 at 4:39





                                                                                                      Both forms throw the same error, "ValueError: invalid literal for int() with base 10"

                                                                                                      – cdlane
                                                                                                      Mar 31 at 4:39













                                                                                                      I know, but not immediately when the user types something different@cdlane

                                                                                                      – user11093202
                                                                                                      Apr 1 at 19:50






                                                                                                      I know, but not immediately when the user types something different@cdlane

                                                                                                      – user11093202
                                                                                                      Apr 1 at 19:50












                                                                                                      0














                                                                                                      parse the input into an integer with int



                                                                                                      x = int(input("What is x?"))





                                                                                                      share|improve this answer

























                                                                                                      • it is highly encouraged for new users to attempt to give a relevant answers to questions and to share their knowledge, but please make sure to read the Stackoverflow: how to write a good answer? guide before attempting to do so. Additionally, please check previous answers and make sure that your answer brings a new perspective or some sort of additional information to the answers that have already been given (and approved) and please include an explanation when sharing a block of code.

                                                                                                        – andreihondrari
                                                                                                        Apr 2 at 15:16















                                                                                                      0














                                                                                                      parse the input into an integer with int



                                                                                                      x = int(input("What is x?"))





                                                                                                      share|improve this answer

























                                                                                                      • it is highly encouraged for new users to attempt to give a relevant answers to questions and to share their knowledge, but please make sure to read the Stackoverflow: how to write a good answer? guide before attempting to do so. Additionally, please check previous answers and make sure that your answer brings a new perspective or some sort of additional information to the answers that have already been given (and approved) and please include an explanation when sharing a block of code.

                                                                                                        – andreihondrari
                                                                                                        Apr 2 at 15:16













                                                                                                      0












                                                                                                      0








                                                                                                      0







                                                                                                      parse the input into an integer with int



                                                                                                      x = int(input("What is x?"))





                                                                                                      share|improve this answer















                                                                                                      parse the input into an integer with int



                                                                                                      x = int(input("What is x?"))






                                                                                                      share|improve this answer














                                                                                                      share|improve this answer



                                                                                                      share|improve this answer








                                                                                                      edited Apr 2 at 14:57









                                                                                                      Zoe

                                                                                                      13.9k85586




                                                                                                      13.9k85586










                                                                                                      answered Apr 2 at 14:53









                                                                                                      EwanziakEwanziak

                                                                                                      333




                                                                                                      333












                                                                                                      • it is highly encouraged for new users to attempt to give a relevant answers to questions and to share their knowledge, but please make sure to read the Stackoverflow: how to write a good answer? guide before attempting to do so. Additionally, please check previous answers and make sure that your answer brings a new perspective or some sort of additional information to the answers that have already been given (and approved) and please include an explanation when sharing a block of code.

                                                                                                        – andreihondrari
                                                                                                        Apr 2 at 15:16

















                                                                                                      • it is highly encouraged for new users to attempt to give a relevant answers to questions and to share their knowledge, but please make sure to read the Stackoverflow: how to write a good answer? guide before attempting to do so. Additionally, please check previous answers and make sure that your answer brings a new perspective or some sort of additional information to the answers that have already been given (and approved) and please include an explanation when sharing a block of code.

                                                                                                        – andreihondrari
                                                                                                        Apr 2 at 15:16
















                                                                                                      it is highly encouraged for new users to attempt to give a relevant answers to questions and to share their knowledge, but please make sure to read the Stackoverflow: how to write a good answer? guide before attempting to do so. Additionally, please check previous answers and make sure that your answer brings a new perspective or some sort of additional information to the answers that have already been given (and approved) and please include an explanation when sharing a block of code.

                                                                                                      – andreihondrari
                                                                                                      Apr 2 at 15:16





                                                                                                      it is highly encouraged for new users to attempt to give a relevant answers to questions and to share their knowledge, but please make sure to read the Stackoverflow: how to write a good answer? guide before attempting to do so. Additionally, please check previous answers and make sure that your answer brings a new perspective or some sort of additional information to the answers that have already been given (and approved) and please include an explanation when sharing a block of code.

                                                                                                      – andreihondrari
                                                                                                      Apr 2 at 15:16











                                                                                                      0














                                                                                                      play = True

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

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

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


                                                                                                      This makes it know that the variable x and y are integers and therefore uses them like integers.

                                                                                                      If you want it to be able to use decimals change int to float.



                                                                                                      The output for the first function:



                                                                                                      print(int(x) + int(y))


                                                                                                      x = 3 and y = 7 returned 10.






                                                                                                      share|improve this answer

























                                                                                                      • Your code won't work. The indentation breaks the while loop

                                                                                                        – Zoe
                                                                                                        Apr 2 at 14:58















                                                                                                      0














                                                                                                      play = True

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

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

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


                                                                                                      This makes it know that the variable x and y are integers and therefore uses them like integers.

                                                                                                      If you want it to be able to use decimals change int to float.



                                                                                                      The output for the first function:



                                                                                                      print(int(x) + int(y))


                                                                                                      x = 3 and y = 7 returned 10.






                                                                                                      share|improve this answer

























                                                                                                      • Your code won't work. The indentation breaks the while loop

                                                                                                        – Zoe
                                                                                                        Apr 2 at 14:58













                                                                                                      0












                                                                                                      0








                                                                                                      0







                                                                                                      play = True

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

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

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


                                                                                                      This makes it know that the variable x and y are integers and therefore uses them like integers.

                                                                                                      If you want it to be able to use decimals change int to float.



                                                                                                      The output for the first function:



                                                                                                      print(int(x) + int(y))


                                                                                                      x = 3 and y = 7 returned 10.






                                                                                                      share|improve this answer















                                                                                                      play = True

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

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

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


                                                                                                      This makes it know that the variable x and y are integers and therefore uses them like integers.

                                                                                                      If you want it to be able to use decimals change int to float.



                                                                                                      The output for the first function:



                                                                                                      print(int(x) + int(y))


                                                                                                      x = 3 and y = 7 returned 10.







                                                                                                      share|improve this answer














                                                                                                      share|improve this answer



                                                                                                      share|improve this answer








                                                                                                      edited Apr 4 at 7:11









                                                                                                      Jagoda Sokół

                                                                                                      162317




                                                                                                      162317










                                                                                                      answered Jan 13 at 5:55









                                                                                                      Gregg MorganGregg Morgan

                                                                                                      105




                                                                                                      105












                                                                                                      • Your code won't work. The indentation breaks the while loop

                                                                                                        – Zoe
                                                                                                        Apr 2 at 14:58

















                                                                                                      • Your code won't work. The indentation breaks the while loop

                                                                                                        – Zoe
                                                                                                        Apr 2 at 14:58
















                                                                                                      Your code won't work. The indentation breaks the while loop

                                                                                                      – Zoe
                                                                                                      Apr 2 at 14:58





                                                                                                      Your code won't work. The indentation breaks the while loop

                                                                                                      – Zoe
                                                                                                      Apr 2 at 14:58











                                                                                                      -1














                                                                                                      Try this,



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


                                                                                                      int() is a built-in python function that converts any datatype to a integer.






                                                                                                      share|improve this answer


















                                                                                                      • 2





                                                                                                        Welcome to Stack Overflow! Does this answer add anything new to the existing ones?

                                                                                                        – iBug
                                                                                                        Dec 26 '18 at 12:39















                                                                                                      -1














                                                                                                      Try this,



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


                                                                                                      int() is a built-in python function that converts any datatype to a integer.






                                                                                                      share|improve this answer


















                                                                                                      • 2





                                                                                                        Welcome to Stack Overflow! Does this answer add anything new to the existing ones?

                                                                                                        – iBug
                                                                                                        Dec 26 '18 at 12:39













                                                                                                      -1












                                                                                                      -1








                                                                                                      -1







                                                                                                      Try this,



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


                                                                                                      int() is a built-in python function that converts any datatype to a integer.






                                                                                                      share|improve this answer













                                                                                                      Try this,



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


                                                                                                      int() is a built-in python function that converts any datatype to a integer.







                                                                                                      share|improve this answer












                                                                                                      share|improve this answer



                                                                                                      share|improve this answer










                                                                                                      answered Dec 26 '18 at 12:30









                                                                                                      Sakith KarunasenaSakith Karunasena

                                                                                                      265




                                                                                                      265







                                                                                                      • 2





                                                                                                        Welcome to Stack Overflow! Does this answer add anything new to the existing ones?

                                                                                                        – iBug
                                                                                                        Dec 26 '18 at 12:39












                                                                                                      • 2





                                                                                                        Welcome to Stack Overflow! Does this answer add anything new to the existing ones?

                                                                                                        – iBug
                                                                                                        Dec 26 '18 at 12:39







                                                                                                      2




                                                                                                      2





                                                                                                      Welcome to Stack Overflow! Does this answer add anything new to the existing ones?

                                                                                                      – iBug
                                                                                                      Dec 26 '18 at 12:39





                                                                                                      Welcome to Stack Overflow! Does this answer add anything new to the existing ones?

                                                                                                      – iBug
                                                                                                      Dec 26 '18 at 12:39





                                                                                                      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

                                                                                                      1928 у кіно

                                                                                                      Захаров Федір Захарович

                                                                                                      Ель Греко