- input()
- int()
- while 循环
-
- break
-
- continue
-
- active_status =True
-
- 删除列表重复元素
-
- 结合字典和列表
message = input("hehehe:")#100
num = int(message)+100
print(num)#200
7.2 even_or_odd %
if int(input("num:"))%2==0:
print("even")
else:
print("odd")
7.3 while loop
count_num = 0
sum = 0
while count_num<11:
sum += count_num
count_num += 1
print(sum)#55
7.4 用户输入某一数据时停止
# 用户输入quit时停止循环
message = ""
while message!='quit':
message = input()
print(message)
7.5 使用break退出循环
while True:
message = input()
if message == 'quit':
break
else:
print(message)
7.5 使用continue进入下一次循环
while True:
message = input()
if message == 'exit':
break
elif message == 'pause':
continue
else:
print(message)
7.6 列表中元素移动
animal_1 = ['dog','cat','lion','panda','bear','tiger','cow','sheep','pig']
animal_2 = []
while animal_1:
animal_2.append(animal_1.pop())
print(animal_1)
# []
print(animal_2)
# ['pig', 'sheep', 'cow', 'tiger', 'bear', 'panda', 'lion', 'cat', 'dog']
7.7 删除列表中多项重复元素
animal_3 = ['dog','pig','cat','lion','pig','bear','tiger','cow','sheep','pig']
while 'pig' in animal_3:
animal_3.remove('pig')
print(animal_3)
#['dog', 'cat', 'lion', 'bear', 'tiger', 'cow', 'sheep']
7.8 使用用户输入来填充字典
responses = {}
active_status = True
while active_status:
name = input("\nplease input your name:")
response = input("which mountain would you like?")
responses[name] = response
repeat = input("yes or no :")
if repeat == 'no':
active_status = False
for name,response in responses.items():
print(name,response)
# please input your name:libing
# which mountain would you like?taishan
# yes or no :yes
# please input your name:zhoujielun
# which mountain would you like?huashan
# yes or no :yes
# please input your name:lixiaolong
# which mountain would you like?wudangshan
# yes or no :no
# libinglin taishan
# zhoujielun huashan
# lixiaolong wudangshan
7.9 熟食店
sandwich_orders = ['beef','cheese','fish']
finished_sandwiches = []
while sandwich_orders:
sandwich = sandwich_orders.pop()
print('i made your '+sandwich+' sandwich')
finished_sandwiches.append(sandwich)
for a in finished_sandwiches:
print(a)
# i made your fish sandwich
# i made your cheese sandwich
# i made your beef sandwich
# fish
# cheese
# beef
7.10 五香烟熏牛肉三明治卖完了
sandwich_orders = ['pastrami','beef','pastrami','cheese','fish','pastrami']
print("五香牛肉三明治卖完了")
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
print(sandwich_orders)
# 五香牛肉三明治卖完了
# ['beef', 'cheese', 'fish']
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)