User input and while loop

How the function input() works

The function input() pauses the program and waits for the user to enter some text. After obtaining user input, Python stores it in a variable for easy use.
For example:

message = input("Tell me something, and I will repeat it back to you: ")

The function input() accepts a parameter: the prompt or description to be displayed to the user. The program waits for the user to input and continues to run after the user presses the Enter key. The input is stored in the variable message, and the following print(message) presents the input to the user:

Tell me something, and I will repeat it back to you:Hello everyone!
Hello everyone!

Note: Sublime Text cannot run programs that prompt users for input.

1. Write clear procedures

When you use the function input(), you should specify a clear and easy to understand prompt to accurately indicate what information you want the user to provide - a prompt indicating that the user should enter any information is OK, as shown below:

name = input("Please enter your name: ")
print("Hello, "+name+"!")

By including a space at the end of the prompt (after the colon here), the prompt can be separated from the user's input, so that the user can clearly know where his input begins, as shown below:

Please enter your name:Eric
Hello, Eric!

You need to indicate the reason for obtaining specific input. In this case, you can store the prompt in a variable and pass the variable to the function input(). In this way, even if the prompt is more than one line, the input() statement is very clear.

prompt = "If you tell us who you are, we can personalize the messages you see."
prompt+= "\nWhat is your first name? "
name = input(prompt)
print("\nHello, "+name+"!")

Demonstrates a way to create multiline strings.
The first line stores the first half of the message in the variable prompt; In line 2, the operator + = appends a string to the end of the string stored in prompt.
The final prompt spans two lines and contains a space after the question mark,
The print result is:

If you tell us who you are, we can personalize the messages you see.
What is your first name? Eric
Hello, Eric!

2. Use int() to get numeric input

When using the function input(), Python interprets user input as a string.
For example:

>>>age = input("How old are you? ")
How old are you? 21

The user enters the number 21, but when we ask Python to provide the value of the variable age, it returns' 21 '- a string representation of the value entered by the user.

age = input("How old are you? ")
How old are you? 21
age >= 18 

Traceback (most recent call last):
  File "", line 1, in 
TypeError: unorderable types: str() >= int() 

When you try to use input for numerical comparison, Python will raise an error because it cannot compare strings with integers: the string '21' stored in age cannot be compared with the value 18.
To solve this problem, use the function int (), which lets Python treat the input as a number. The function int() converts the string representation of a number to a numeric representation, as follows:

>>>age = input("How old are you? ")
How old are you? 21
>>>age = int(age) 
>>>age >= 18

How to use the function int() in a real program?
The following procedure determines whether a person meets the height requirements for riding a roller coaster:

height = input("How tall are you, in inches? ")
height = int(height)
if height >= 36:
    print("\nYou're tall enough to ride!")
    print("\nYou'll be able to ride when you're a little older.")

The print result is:

How tall are you, in inches? 66
You're tall enough to ride!

How tall are you, in inches? 12
You'll be able to ride when you're a little older.

Before entering values for calculation and comparison, be sure to convert them to numerical representation.

3. Modulo operator

The modulus operator (%) is a useful tool for processing numeric information. It divides two numbers and returns the remainder:

>>>4 % 3
>>>5 % 3
>>>6 % 3
>>>7 % 3

The modulo operator does not indicate how many times one number is another, but only the remainder.
If one number is divisible by another, the remainder is 0, so the modulus operator returns 0.
This can be used to determine whether a number is odd or even:

number = input("Enter a number, and I'll tell you if it's even or odd: ")
number = int(number)
if number % 2 == 0:
    print("\nThe number "+str(number)+" is even.")
    print("\nThe number "+str(number)+" is odd.")

Even numbers can be divided by 2, so the result of modulo operation on a number and 2 is zero, that is, number% 2 = = 0, then this number is even; Otherwise, it is odd.

Enter a number, and I'll tell you if it's even or odd:42
The number 42 is even.

Tags: Python

Posted on Tue, 23 Nov 2021 09:10:55 -0500 by tomasd