input和while循环——Python编程从入门到实践

input( )

input()函数:让程序运行暂停,等待用户输入。

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

运行结果:

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

 1. 编写清晰的程序

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

Please enter your name: hery
Hello, hery!

提示信息超过一行时:

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

2. Get input values

age = input("How old are you? ")
print(type(age))

How old are you? 12
<class 'str'>

By input () function of the input information is stored in the form of a string, if you need to enter a numerical value to use how to do it?

You may be used int () function converts it to the value represented by:

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.")

3. modulus operator

Modulo operator (%): remainder obtained by dividing the two returns.

Be used to determine a number is odd or even:

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

运算符两端的元素类型要一致,故print语句中又需要将数值型通过str()函数转换为字符型。

while循环

for循环是针对集合中的每个元素的一个代码块,而while循环是不断的运行,直到指定条件不满足。

1. 使用while循环

current_number = 1
while current_number <= 5:
    print(current_number)
    current_number += 1

operation result:

1
2
3
4
5

2. let the user choose when to quit

prompt = "\nTell me something , and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
message = ''
while message != 'quit':
    message = input(prompt)
    print(message)

operation result:

Tell me something , and I will repeat it back to you: 
Enter 'quit' to end the program. Hello Python
Hello Python

Tell me something , and I will repeat it back to you: 
Enter 'quit' to end the program. Hello 0629
Hello 0629

Tell me something , and I will repeat it back to you: 
Enter 'quit' to end the program. quit
quit

 Enter quit to end the cycle time.

If do not want to quit will be printed out as a message, then:

prompt = "\nTell me something , and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
message = ''
while message != 'quit':
    message = input(prompt)
    if message != 'quit':
        print(message)

3. Use logo

The program continues to run only in the case of many required conditions are met, you can define a variable for judging the entire program is active, this variable is called flag.

prompt = "\nTell me something , and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "

active = True
while active:
    message = input(prompt)

    if message == 'quit':
        active = False
    else:
        print(message)

When the knock code the Active = False knock become the Active = 'False', then enter quit also keep looping, ha ha ha

4. Use the break to exit the loop

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() + "!")

 Note: Python loop (while loops, for loops) in the break statement can be used to launch cycle.

5. continue in a loop

Cycle use continue, it will return to the beginning of a cycle, and decide whether to continue the cycle based on a conditional test:

current_number = 0
while current_number < 10:
    current_number += 1
    if current_number % 2 == 0:
        continue
    else:
        print(current_number)

operation result:

1
3
5
7
9

6. Avoid endless loop

x = 1
while x < 5:
    print(x)
    x += 1

The above-described code block, if left out the line of code x + = 1, the program will run endlessly. Press Ctrl + C, can turn off the display terminal program output window, or close the editor, the end of an infinite loop.

 

 

Guess you like

Origin www.cnblogs.com/shirley-yang/p/11073782.html