Python user input and while loop

User input and while loop

  1. How the function input () works The
    function input () pauses the program and waits for the user to enter some text. After getting the input, python stores it in a variable and uses it.
    Examples:
message = input("tell me somthing, and i will repeat it back to you: ")
print(message)
tell me somthing, and i will repeat it back to you: aa
aa

Whenever the input () function is used, it should clearly prompt the user what to do next.

name =  input("PLease enteryour name: ")
print("\nHello, " + name + "!")
PLease enteryour name: aa

Hello, aa!
prompt = "if you tell us who you are, wo can personalize the message you see."
prompt += "\nwhat is your first name?"
name  = input(prompt)

print("\nHello, " + name + "!")
if you tell us who you are, wo can personalize the message you see.
what is your first name?qq

Hello, qq!

Use int () to get numeric input:
When using the function input (), Python interprets user input as a string, such as:

>>> age = input("how old are you:")
how old are you:21
>>> age
'21'

The input is the number 21, but when the value of the age variable is printed, the return is indeed '21'. The input number is represented by a string. How did you just judge that the number is displayed in the form of a string, that is, the number 21 is surrounded by single quotes .
So if you want to directly use the number that the user belongs to in Python, it is not possible to make certain judgments or checks. You need to convert the string number to a number before using it, such as:

age = input("how old are you? ")
age = int(age)
if age>= 30:
	print(age)
else:
	print("age too small!")
how old are you? 55
55
how old are you? 21
age too small!
height = input("how tall are you, in inches?")
height = int(height)

if height >= 36:
	print("\nyou're tall enough to ride!")
else:
	print("\nyou'll be able to ride when you're a little older.--command")
how tall are you, in inches?34

you'll be able to ride when you're a little older.--command

how tall are you, in inches?66

you're tall enough to ride!

Modulus operation When
dealing with numerical information, the modulo operation (%) is a very useful tool. It divides two numbers and returns the remainder:

>>> 4%3
1
>>> 5%3
2
>>> 6%3
0
>>> 7%2
1
>>> 7%3
1

number = input("enter a number, and I'll tell you if it's even or odd: ")
number = int(number)

if number % 2 == 0:
	print("\n the number " + str(number) + " is even.")
else:
	print("\n the number " + str(number) + " is odd.")
enter a number, and I'll tell you if it's even or odd: 5

 the number 5 is odd.
enter a number, and I'll tell you if it's even or odd: 6

 the number 6 is even.

Note: If you are using python 2.7 version, you should use the function raw_input () to prompt the user for input. This function, like input () in python3, also interprets the input as a string.
Python 2.7 also includes the function input (), but it interprets user input as python code and tries to run them. If you run them, the best execution result is an error, but it may result in successful execution, but you will not get your own expected results. So in python2.7, please use raw_input () instead of input () to get input.

car = input("please tall me, your select car: ")
print("let me see if I can find you a " + car)
please tall me, your select car: bwa
let me see if I can find you a bwa

peopel = input("please tel me, your total many peopel eatting: ")
peopel = int(peopel)

if peopel > 8:
	print("sorry, current table is full!")
else:
	print("ok, have empty table.please...")
please tel me, your total many peopel eatting: 9
sorry, current table is full!

please tel me, your total many peopel eatting: 5
ok, have empty table.please...

num = input("please input int number: ")

num = int(num)

if num % 10 != 0:
	print("the num: " + str(num) + " , not 10 Whole multiple")
else:
	print("yes: " + str(num) + " is 10 Whole multiple")
please input int number: 5
the num: 5 , not 10 Whole multiple

please input int number: 10
yes: 10 is 10 Whole multiple

  1. The while loop
    for loop is used for a code block for each element in the collection, while the while loop continues to run until the specified condition is not met.
    Many programs we use may contain while loops. For example, the game uses a while loop to ensure that it continues to run when the player wants to play, and stops when the player wants to quit. If the program stops when the user does not stop it, or it continues to run when the user wants to quit, it is too boring.
    Use a while loop to keep the program running when the user wishes, such as:
prompt = "\ntell me somthing, and i will repeat it back to you: "
prompt += "\nenter 'quit' to end the program."
message = ""
while message != 'quit':	
	message = input(prompt)
	print(message)
tell me somthing, and i will repeat it back to you: 
enter 'quit' to end the program.go
go

tell me somthing, and i will repeat it back to you: 
enter 'quit' to end the program.quit
quit


------------------
(program exited with code: 0)
Press return to continue

Of course, we can also use a flag to decide whether to continue running:
In a program that requires many conditions to be met before it can continue to run, a variable can be defined to determine whether the entire program is active. This variable is called The sign serves as a traffic signal for the program, such as:

prompt = "\ntell me somthing, and i will repeat it back to you: "
prompt += "\nenter 'quit' to end the program."
message = ""

active = True
while active:
	message = input(prompt)
	if message == 'quit':
		active = False
	else:
		print(message)

tell me somthing, and i will repeat it back to you: 
enter 'quit' to end the program.go
go

tell me somthing, and i will repeat it back to you: 
enter 'quit' to end the program.do
do

tell me somthing, and i will repeat it back to you: 
enter 'quit' to end the program.quit


------------------
(program exited with code: 0)
Press return to continue

Use break to exit the loop, for example:

prompt = "\nplease enter the name of a city you have visited: "
prompt += "\n(enter 'quit' when you are finished)"

while True:
	city = input(prompt)
	
	if city == 'quit':
		break
	else:
		print("i'd love to go to " + city.title() + "!")


please enter the name of a city you have visited: 
(enter 'quit' when you are finished)go
i'd love to go to Go!

please enter the name of a city you have visited: 
(enter 'quit' when you are finished)go
i'd love to go to Go!

please enter the name of a city you have visited: 
(enter 'quit' when you are finished)quit


------------------
(program exited with code: 0)
Press return to continue

Use continue in the loop, the continue statement does not exit the program directly like the break statement, but ignores the current loop, but jumps directly to the beginning for the next loop.

current_number = 0

while current_number < 10:
	current_number += 1
	if current_number % 2 == 0:
		continue
		
	print(current_number)
1
3
5
7
9


------------------
(program exited with code: 0)
Press return to continue

If the remainder is not 0, print it out; if the remainder is 0, execute the next cycle.
Note: When writing a loop program, you must avoid dead loops, so that the program will be executed indefinitely. Unless you need such a scene, infinite loops are used. Otherwise, you must exit the loop in the loop. conditions of.

Published 53 original articles · praised 16 · visits 2213

Guess you like

Origin blog.csdn.net/m0_37757533/article/details/105468802