python学习笔记(四)---用户输入与while循环

用户输入

函数input

demo1:

message = input("all you input is chars:")
print(message)

demo2:
由input接受的所有内容都将被以字符串数字储存,需要用int()强制转换

number = input("please input a number")
num = int(numbers)

while循环处理列表和字典

删除包含特定值的所有列表元素
pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
print(pets)
while 'cat' in pets:
pets.remove('cat')
print(pets)
使用用户输入来填充字典

demo:

names = {}
number= 0
condition = 'y'
while condition == 'y':
    name = input("
what's your name? : ")
    names[str(number)] = name
    number += 1
    condition = input("would you like to collect more names? (y/n) : ")
print(names)
原文地址:https://www.cnblogs.com/l0nmar/p/12553866.html