本站消息

  本站每日ip已达10000,出租广告位,位置价格可谈,需要合作请联系站长


+关注
已关注

分类  

pandas(0)

flask(0)

标签  

pandas(0)

flask(0)

日期归档  

2019-01(1)

2019-04(1)

2019-06(1)

2019-07(3)

2019-08(97)

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

发布于2019-08-07 12:41     阅读(166)     评论(0)     点赞(1)     收藏(0)


#第七章用户输入和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 + "!")
  • 1
  • 2

#输出结果:

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

#输出结果:

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

 Hello! Lily
  • 1
  • 2
  • 3
  • 4

#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.")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

#输出结果:

How tall are you: 110

You'll be able to ride when you are a little older.
  • 1
  • 2
  • 3

################
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.")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

#输出结果:

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

The number 11is odd.
  • 1
  • 2
  • 3

################
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

#输出结果:

1
2
3
4
5
  • 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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

#输出结果:

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

################
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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

#输出结果:

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

################
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() +"!")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

#输出结果:

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

################
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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

#输出结果:

1
3
5
7
9
  • 1
  • 2
  • 3
  • 4
  • 5

################
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())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

#输出结果:

Verifying user Candce
Verifying user Brian
Verifying user Alice

The following users have benn confirmed: 
Candce
Brian
Alice
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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

pets = ['cat','dog','rabbit','cat','goldenfish','lion']
print(pets)
while 'cat' in pets:
    pets.remove('cat')
print (pets)
  • 1
  • 2
  • 3
  • 4
  • 5

#输出结果:

['cat', 'dog', 'rabbit', 'cat', 'goldenfish', 'lion']
['dog', 'rabbit', 'goldenfish', 'lion']
  • 1
  • 2

################
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 +".")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

#输出结果:

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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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



所属网站分类: 技术文章 > 博客

作者:爱丽丝

链接: https://www.pythonheidong.com/blog/article/10924/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

1 0
收藏该文
已收藏

评论内容:(最多支持255个字符)