How to Make a Program Start Again if It Receives a Bad Input Python
Interacting with a user is one of the great features of programming. If y'all can't take the inputs from a user, then you cannot take actions based on what the user needs. That'south where Python Input Function comes in picture. Moreover, Python has an inbuilt role called input() to cater to user inputs. Permit'south run into the list of the tutorial.
- What is the Python Input office?
- Python Input function statement
- Python Input office render blazon
- Blazon Casting in Python
What is the Python Input office?
Nosotros have already seen the print part in Python, which sends information to the console. Python Input part does the reverse of it. In other words, it takes user data from the console so that the program can have activity based on that input.
The input function indicates the user to enter the data on the console. Moreover, the program executes farther simply when the user has entered the data. This information can then be stored in a variable and can use it in the program wherever we need it.
Note - Input function always takes the input information as Cord. So if y'all are request the user to enter age, and he enters age as 25, this will exist considered a cord. Additionally, you need to convert it to int before you take whatever activeness on it.
Write the post-obit in IDE and run it.
# taking input from the user proper name = input() print(name)
If you run the above program, you will meet a blinking cursor in the output panel, every bit shown in the below snap. It is waiting for the data. Moreover, the program won't execute the farther steps until we give the input.
Now, enter anything in the console.
After inbound whatever text striking Enter . Consequently, you will get what y'all entered as output.
The storage of the data that nosotros have given is in the variable name . And we accept printed the variable in the terminal line of the code. That'due south the output we have seen in the above image and your IDE.
How to laissez passer an statement to the Python Input office?
How does a user know he has to enter a number or name or whatsoever other data? To tell what blazon of data user has to enter, we can pass a bulletin to the input() office that displays to the user.
For case, your programme needs the name of the user to print a welcome message. You can write the input statement as input("Enter your name:- "). Information technology displays the message to the user. Later, try the following code in IDE.
# getting proper noun of the user user_name = input("Enter your name:- ") impress("Howdy,", user_name, "\b!")
Nosotros accept given a message to the user that Enter your name:-. User will read the message and enter the information appropriately.
You lot can come across that we have stored the user input in a variable user_name, and while printing it, nosotros have added "Hi" to information technology.
What happens when you lot pass multiple arguments to the input() function? Will information technology evidence everything like print() function?
Let's see what happens with the following code.
# input() -> multiple arguments num = input("Enter a number", "greater than 0 and less than 100") print(num)
We got an fault. Why is it? The input() office takes at nearly ane statement, but we passed two arguments to the input office. Equally a result, we got an error. Remember python user input() role takes only ane argument. In other words, if we don't pass anything, and so the input function won't show any bulletin to the user, as seen in the showtime department of the tutorial.
What is the Return Blazon of Python Input office?
We have mentioned that the python user input takes the information equally a cord blazon. Let's cheque by an experiment.
# getting input from the user number = input("Enter a number:- ") result = number + two impress(result)
What practise you await the output will be? Run the lawmaking and enter a number. See what you get.
Nosotros got the error. Why? The data that nosotros got from the console is a string. And we attempt to add a string to a number. So, we got the mistake.
What if we want to take a number as input? Is it possible? Allow's jump into the side by side section to find the answers.
Type Casting
Converting one blazon of data to other kinds of information is known equally typecasting. Nosotros are already familiar with such kind of conversions.
What if nosotros want to accept an integer , or float as input. Is it possible in Python? The answer is NO . Merely, nosotros tin can apply the typecasting (blazon conversion) using congenital-in information type functions.
Allow'south try to convert the python string to python integer. You still remember the int function, don't you lot?
# getting input from the user # and converting it to an integer # int() function number = int(input("Enter a number:- ")) upshot = number + 2 print(event)
We have converted the input data to an integer at the fourth dimension of taking the input itself. Another pick was offset to consider the number as a string, store information technology in a variable, and then convert information technology to int. While this will work perfectly fine, information technology will increase the lines of code and memory usage. It's always best to write efficient code!!
We will become an fault if we endeavour to convert a string that contains alphabets or other special characters. Run the above code and give the 2020ToolsQA equally input.
We are trying to catechumen a cord that is not a number. And then, Python throws an mistake.
Similar to the conversion of strings to integers, we can convert them to floats besides. You tin apply int or bladder functions for the conversion based on your needs.
That's it for this tutorial.
Cardinal Takeaways
- input() part takes the data from the console in the form of cord.
- Moreover, we can laissez passer a message to the input() function that volition appear to the user before giving the data input. E.g. input("Enter a number:- ")
- If nosotros pass multiple arguments to theinput() function, and then we will get the error.
- input() function returns the information in the cord (str) format. Moreover, we tin can use the typecasting for blazon conversion. E.k. number = int(input("Enter a number:- "))
- Additionally, converting 1 type of data into other types of data is known equally typecasting.
- Nosotros can utilise the functionsint, str, andfloat for the typecasting.
Practise Yourself
#Problem 1
# input:- ToolsQA impress (input()) #Trouble 2
# input:- ToolsQA print ("Welcome to " + input() + "!") #Problem three
# Follow the instructions to write a program # 1. Take a number as input from the user # ii. Multiply information technology with 10 and carve up the effect by 10 # 3. Print the above the result Problem 4
# Follow the instructions to write a plan # 1. Take the user proper name as input and store it in a variable # 2. Accept the age of the user as a number and store it in a variable # 3. Print the a line of string as follows # Output: Your name is <user-proper noun> and historic period <user-age> #Trouble 5
# Follow the instructions to write a program # one. Accept two numbers from the user as input # ii. Add those 2 numbers # iii. Print the result
What Side by side?
To conclude, we have completed learning the Input/Output(I/O) functions in Python. Additionally, try to accept unlike inputs using the input function and employ the typecasting and practice. Remember - the more yous practice, the more you will feel comfortable with Python coding.
In addition to the above, nosotros have arithmetic operators + and * for integers. They have unlike meanings when you use it with string operands. Allow's understand Python Cord Operator in item in the next section.
Happy Coding :)_column_text][/vc_column][/vc_row]
Source: https://www.toolsqa.com/python/python-input-function/
0 Response to "How to Make a Program Start Again if It Receives a Bad Input Python"
Post a Comment