In this notebook, we will look at how to take Integer input from command line in Python 3 and Python 2. For taking string input from command line in Python, check out How To Take String Input From Command Line In Python
Python raw_input() allows taking input from command line, but by default all the inputs are treated as strings.
userinput = raw_input("Enter Integer Number!\n")
print("You entered %d"%userinput)
The reason we got above error is "userinput" variable contains a string but not a number.
To fix this, we will have to convert the input to integer before assigning to a variable.
userinput = int(raw_input("Enter Integer Number!\n"))
print("You entered %d"%userinput)
Similarly we can tweak our previous code to take a Floating point number as input.
userinput = float(raw_input("Enter Floating Point Number!\n"))
print("You entered %f"%userinput)
Similarly we can use above code snippets in Python 3 by replacing the Python input function raw_input() with input().
userinput = int(input("Enter Integer Number!\n"))
print("You entered %d"%userinput)
userinput = float(raw_input("Enter Floating Point Number!\n"))
print("You entered %f"%userinput)
Related Notebooks
Related Notebooks
- How To Take String Input From Command Line In Python
- Python Pandas String To Integer And Integer To String DataFrame
- Python Is Integer
- How To Add Regression Line On Ggplot
- Write Single And Multi Line Comments In Python
- Five Ways To Remove Characters From A String In Python
- How To Run Code From Git Repo In Collab GPU Notebook
- Return Multiple Values From a Function in Python
- How to Plot a Histogram in Python