How to Make Program Run Again After User Input Python

Python User Input

Introduction to Python User Input

While programming very often, we are required to take input from the user then as to manipulate the information and process information technology thereafter. This ensures the programme'southward dynamism and robustness as information technology is not hardcoded and can successfully execute at whatever random value given past the user. In this topic, nosotros are going to acquire about Python User Input.

In today'due south article, we would learn about the concept of accepting input from the user at the run time of the plan for Python programming language and process that data. This ensures proper communication between the program and the exterior world.

Methods of Inputting Data

The following are the ways of inputting data from the user: –

  • input()
  • raw_input()

Both basically practice the aforementioned chore, the simply difference being the version of Python, which supports the 2 functions.

raw_input was used in older versions of Python, and information technology got replaced by input() in recent Python versions.

In this article, we would discuss input() as it is used in recent versions of Python.

Working of Python Input()

When we use the input() office in our program, the menstruation of execution is halted till the user inputs the data and clicks the Enter push button. Afterwards that, the user's value input is stored in some variable in the form of a cord. No thing what information type the user intended their data to be, it gets stored as a string but. It needs to explicitly typecast to the desired format before use. Later we will wait at such an example where if we exercise not typecast the information, it will result in an error.

Now permit united states of america accept a look at the syntax of input()

Syntax

              input([<prompt from user>])            

Now let u.s.a. run into some examples and hash out how to apply input() to accept data from users for different scenarios.

Examples of Python User Input

Here are the following examples mention beneath:

Instance #1

Inputting normal text from the keyboard

Code:

              string1 = input() print(string1)            

Output

Python User Input output 1

Caption

In the first line, we used the input() function to take input from the user and store it in the variable named string1. After we press the  Enter button, we are instructed to provide our input by the show of a  blank input box with a cursor blinking. When we provide the input and printing the Enter button, then that value gets stored in the variable named string1. In the side by side line, when we are printing the variable, we actually meet that the input provided past us is really printed in the console.

Example #2

Inputting number from the panel and finding its square

Code:

              print("Enter number to find the square:") number = input() print("The Square of number {}".format(number,number**2))            

Output

Python User Input output 2

Explanation

Here we encounter a TypeError, and if we read the errorMessage, it suggests to united states of america that the first argument, i.e. number variable cord datatype. And because of that, it is not able to perform the power operation on that. It is important to notation here that the input data type is always taken as a string, then information technology must exist typecast to the desired datatype before it tin can be worked on.

Let us correct the code and have a look at the output.

Code

              print("Enter number to find the square:") number = input() number = int(number) print("The square of the number {} is {}".format(number,number**2))            

Output

output 3

Example #3

Inputting multiple numbers and finding out the maximum from them

Code:

              import numpy equally np print("How many numbers nosotros want to detect the maximum of:") n=int(input()) numbers = []  for i in range(0, n):     ele = int(input("Enter elements: ")) numbers.append(ele) impress("The Maximum Number is:", max(numbers))            

Output

output 4

Example #4

Inputting sentence and counting the number of characters in that

Code:

              print("Enter the sentence :") judgement = input() length = len(sentence) print("The number of characters in the sentence is {}".format(length))            

Output

output 5

Decision

Finally, it is time to draw closure to this commodity. In today's article, we learned about the input() function of Python. We looked at the syntax and discussed different examples and utilize cases. Then next time you lot write a Python plan, do think to apply the concepts of input() and exam your program on many random user input information.

Recommended Articles

This is a guide to Python User Input. Here we discuss the methods, working, and the examples of Python User Input along with the appropriate syntax. You lot may also accept a look at the following manufactures to acquire more –

  1. Python yield
  2. Python Switch Case
  3. Python Piece Cord
  4. Python Constants

toresapatons.blogspot.com

Source: https://www.educba.com/python-user-input/

0 Response to "How to Make Program Run Again After User Input Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel