python入门6:input用户输入,while循环,以及while循环处理字典和列表

用户输入

input()输入原理:让程序暂停,等待用户输入一些文本;
input()输入原理:input(内容),将输入内容解读为字符串; 使用int()获取数值输入:将程序默认为字符串的数字转成整型;
求模运算符:%,两数相除返回余数;

car = input("What kind of cars do you want?")
print("Let me see if i can help you find a Subaru.")

number = input("How many people will go there for the dinner?")
if int(number) > 8:
    print("Sorry, we do not have the free table.")
else:
    print("You can book a table now.")

number = input("Please enter a number:")
if int(number) % 10 == 0:
    print("This number is an integer multiple of 10.")

While 循环

for循环:用于针对集合中的每个元素都有一个代码块;
while循环:while循环不断运行,直到指定条件不满足为止;
while循环让用户选择何时退出:定义一个退出值即可;
标志我不太懂?,为了检查程序哪里出现了问题?????
break退出循环:退出遍历列表循环,或者字典中的for循环;
continue:循环的是代码,若满足条件,continue后的代码将不被执行;
避免无限循环

#for 
for num in range(1,8):
    if num > 4:
        break
    else:
        print("stop")

#while
num = 1
while num < 4:
    print(num)
    num += 1

#出现quit时退出,break
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":
        break
    else:
        print(message)

#continue
number = 1
while number < 11:
    number += 1
    if number % 2 == 0:
        continue#if成立,就继续返回执行代码,if不成立,才执行print
    print(number)

while循环处理列表和字典

while循环:可在循环进行时对列表进行修改,for循环则不可;
while循环:while循环+列表+字典,可收集、存储并组织大量输入;
在列表之间移动元素:while循环进行遍历,在一个列表中删除,再添加;
删除包含特定值的所有列表元素:while循环遍历,用.remove();
用户输入填充字典

#列表之间移动元素
unconfirmed_users = ["abc","bcd","cdf","dfe"]
confirmed_users = []
while unconfirmed_users:#为什么这个没有用到in?
    current_users = unconfirmed_users.pop()
    print(current_users.title() + ", you are verified.")
    confirmed_users.append(current_users)
    
#删除一个列表中所有的特定值
numbers = [1,2,3,4,2,1,2,4,5,6,7,2]
print(numbers)
while 2 in numbers:#怎么这个就用到了in?
    numbers.remove(2)
    print(numbers)
    
#用户输入填充字典
responses = {}
polling_active = True #设置一个标志,指出调查是否继续,只要是true就继续
while polling_active:
    name = input("Please enter your name:")
    response = input("Do you like dogs?")
    responses[name] = response #将信息存储到字典中
    repeat = input("If there anyone else?")
    if repeat == "no":
        polling_active = False #调查结束,标志是false就结束循环
print("results:")
for name, response in responses.items(): #显示调查结果
    print(name.title() + "'s response is " + response + ".")
Dfe, you are verified.
Cdf, you are verified.
Bcd, you are verified.
Abc, you are verified.
[1, 2, 3, 4, 2, 1, 2, 4, 5, 6, 7, 2]
[1, 3, 4, 2, 1, 2, 4, 5, 6, 7, 2]
[1, 3, 4, 1, 2, 4, 5, 6, 7, 2]
[1, 3, 4, 1, 4, 5, 6, 7, 2]
[1, 3, 4, 1, 4, 5, 6, 7]
Please enter your name:ymj
Do you like dogs?yes
If there anyone else?yes
Please enter your name:mjy
Do you like dogs?yes
If there anyone else?no
results:
Ymj's response is yes.
Mjy's response is yes.
#try one try
sandwich_orders = ["ab","bc","cd","df","fe"]
finished_sandwiches = []
while sandwich_orders:
    finished_sandwich = sandwich_orders.pop()
    finished_sandwiches.append(finished_sandwich)
for finished_sandwich in finished_sandwiches:
    print(finished_sandwich)
print("has been finished.")

sandwich_orders = ["ab","bc","cd","bc","df","fe","bc"]
print("The bc has been sold out.")
while "bc" in sandwich_orders:
    sandwich_orders.remove("bc")
print(sandwich_orders)

polling_results = {}
polling_active = True
while polling_active:
    name = input("Please enter your name:")
    responses = input("Where would you go if you could visit one place?")
    polling_results[name] = responses
    repeat = input("If anyone else?")
    if repeat == "no":
        polling_active = False
print(polling_results)
for name, response in polling_results.items():
    print(name.title() + "'s repsonse is " + response.title() + ".")
#try one try
sandwich_orders = ["ab","bc","cd","df","fe"]
finished_sandwiches = []
while sandwich_orders:
    finished_sandwich = sandwich_orders.pop()
    finished_sandwiches.append(finished_sandwich)
for finished_sandwich in finished_sandwiches:
    print(finished_sandwich)
print("has been finished.")

sandwich_orders = ["ab","bc","cd","bc","df","fe","bc"]
print("The bc has been sold out.")
while "bc" in sandwich_orders:
    sandwich_orders.remove("bc")
print(sandwich_orders)

polling_results = {}
polling_active = True
while polling_active:
    name = input("Please enter your name:")
    responses = input("Where would you go if you could visit one place?")
    polling_results[name] = responses
    repeat = input("If anyone else?")
    if repeat == "no":
        polling_active = False
print(polling_results)
for name, response in polling_results.items():
    print(name.title() + "'s repsonse is " + response.title() + ".")
fe
df
cd
bc
ab
has been finished.
The bc has been sold out.
['ab', 'cd', 'df', 'fe']
Please enter your name:ymj
Where would you go if you could visit one place?norway
If anyone else?no
{'ymj': 'norway'}
Ymj's repsonse is Norway.
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值