Python编程从入门到实践(第七章用户输入和while循环学习总结)

#第七章用户输入和while循环#
1、input()函数让程序暂停运行,等待用户输入一些文本
2、int()函数来获取数值
3、求模运算符%返回两个数相除的余数
4、Python2.7中获取输入使用raw_input函数而不是input()函数
5、while循环
6、用户选择退出时
7、使用标志
8、break退出循环,不再运行循环中余下的代码
9、循环中使用continue
10、使用while循环在列表之间移动元素
11、删除包含特定值的所有列表元素
12、使用用户来输入来填充字典

################
1、input()函数让程序暂停运行,等待用户输入一些文本

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

#输出结果:

Please enter your name: Tom
Hello Tom!


prompt = "If you tell us who you are,we can personalize the messages you see."
prompt += "\n What's your first name: "   #运算符+=在存储在prompt中的字符串末尾附加一个字符串
name = input(prompt)
print("\n Hello! " + name)

#输出结果:

If you tell us who you are,we can personalize the messages you see.
 What's your first name: Lily

 Hello! Lily

#Sublime Text 不能运行提示用户输入的程序。可以使用Sublime Text来编写提示用户输入的程序,但必须从终端运行它们

################
2、int()函数来获取数值
#将数字的字符串标识转换为数值表示

height = input("How tall are you: ")
height = int(height)  #转换成数值表示
if height >= 120:
    print ("\nYou are tall enought to ride!")
else:
    print ("\nYou'll be able to ride when you are a little older.")

#输出结果:

How tall are you: 110

You'll be able to ride when you are a little older.

################
3、求模运算符%返回两个数相除的余数

number = input("Enter a number,and I will tell you if it's even or odd: ")
number = int(number)  #将值转换为数值类型
if number % 2 == 0:   #求模运算结果,除以2后的余数是否等于0
    print("\nThe number " + str(number) + "is even.")
else:
    print("\nThe number " + str(number) + "is odd.")

#输出结果:

Enter a number,and I will tell you if it's even or odd: 11

The number 11is odd.

################
4、Python2.7中获取输入使用raw_input函数而不是input()函数

################
5、while循环

current_number = 1
while current_number <= 5: #符合条件时执行循环
    print (current_number)
    current_number += 1

#输出结果:

1
2
3
4
5

################
6、用户选择退出时

prompt = "\nTell me something, and I will repeat it back to you。"
prompt += "\n Enter 'quit' to end the program: "
message = ""
while message != 'quit':
    message = input(prompt)
    if message != 'quit':  #检查消息内容不是quit时才打印
        print (message)

#输出结果:

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

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

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

Process finished with exit code 0

################
7、使用标志
#要求很多条件都满足才继续运行的程序中,可以定义一个变量,用于判断整个程序是否处于活动状态,这个变量成为标志,充当程序的交通信号灯

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':  #如果用户输入的是quit,将active设置为False
        active = False
    else:
        print (message)

#输出结果:

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

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

################
8、break退出循环,不再运行循环中余下的代码
#在任何Python循环中都可以使用break语句,例如遍历列表或者字典的for循环

prompt = "\nPlease enter the name of a city you have visited: "
prompt += "\nEnter 'quit' when you are finished: "
while True:
    city = input(prompt)
    if city == 'quit':
        break #用户输入quit后立即退出while循环
    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: WuHan
I'd love to go to Wuhan!

Please enter the name of a city you have visited: 
Enter 'quit' when you are finished: quit

################
9、循环中使用continue
#返回循环开头,并根据条件测试结果决定是否继续执行循环,

current_number = 0
while current_number < 10:
    current_number += 1
    if current_number % 2 ==0: #与2进行求模运算,余数为0执行continue返回循环开头
        continue
    else:
        print(current_number)

#输出结果:

1
3
5
7
9

################
10、使用while循环在列表之间移动元素

unconfirmed_users = ['alice','brian','candce']  #创建一个待验证用户列表
confirmed_users = [] #创建一个用于存储已验证用户的空列表
while unconfirmed_users:
    current_user = unconfirmed_users.pop()
    print ("Verifying user " + current_user.title())
    confirmed_users.append(current_user)  #将验证的用户添加到已验证用户列表中
print ("\nThe following users have benn confirmed: ")
for confirmed_user in confirmed_users:
    print(confirmed_user.title())

#输出结果:

Verifying user Candce
Verifying user Brian
Verifying user Alice

The following users have benn confirmed: 
Candce
Brian
Alice

################
11、删除包含特定值的所有列表元素

pets = ['cat','dog','rabbit','cat','goldenfish','lion']
print(pets)
while 'cat' in pets:
    pets.remove('cat')
print (pets)

#输出结果:

['cat', 'dog', 'rabbit', 'cat', 'goldenfish', 'lion']
['dog', 'rabbit', 'goldenfish', 'lion']

################
12、使用用户来输入来填充字典

responses = {}
polling_active = True #设置一个标志,指出调查是否继续
while polling_active:
    name = input("\nWhat is your name? ")   #提示输入被调查者的名字和回答
    response = input("Which mountain would you like to climb someday? ")
    responses[name]=response  #将答卷写到字典中
    repeat = input("Would you like to let another person respond?(yes / no)")  #是否还有人参与调查
    if repeat == 'no':
        polling_active = False
print("\n----Poll Results----")  #调查结果显示
for name,response in responses.items():
    print(name + " would you like to climb " + response +".")

#输出结果:

What is your name? alice
Which mountain would you like to climb someday? one mon
Would you like to let another person respond?(yes / no)yes

What is your name? tom
Which mountain would you like to climb someday? two mon
Would you like to let another person respond?(yes / no)no

----Poll Results----
alice would you like to climb one mon.
tom would you like to climb two mon.

Python编程从入门到实践基础知识:https://blog.csdn.net/louzhu_lz/article/details/90721685
Python编程从入门到实践(第三、四章的列表和元祖):https://blog.csdn.net/louzhu_lz/article/details/91354506
Python编程从入门到实践(第五章if语句学习总结):https://blog.csdn.net/louzhu_lz/article/details/91409903
Python编程从入门到实践(第六章字典学习总结):https://blog.csdn.net/louzhu_lz/article/details/91910554
Python编程从入门到实践(第七章用户输入和while循环学习总结):https://blog.csdn.net/louzhu_lz/article/details/92384649
Python编程从入门到实践(第八章函数)学习总结:https://blog.csdn.net/louzhu_lz/article/details/93377817

你可能感兴趣的:(Python入门)