#一、函数input()的工作原理
#1.函数input()让程序暂停运行,等待用户输入一些文本,获取用户输入后Python将其存储在一个变量中。
name=input("告诉我你的名字:")
print("Hello "+name)
#2.使用int()来获取数值输入
age=input("How old are you")
age>=18
#会报错,因为input()返回的是字符串储存在age中,而18是整数值,所以不能比较
#解决,使用函数int()将数字的字符串转换为数值表示
age=int(age)
if age>=18:
print("你已成年")
#3.求模运算符
#求模运算符%,它将两个数相除并返回余数
print(4%3)
#可用来判断基数还是偶数,偶数能被2整除
#二、While循环
#1.使用while循环
number=1
while number<=5:
print(number)
number+=1
#2.让用户选择何时退出
# prompt="\n输入一个字符串"
# prompt+="\n否则quit退出"
# message=" " #将变量message设置成空字符串,让Python首次执行while代码是有可供检查的东西
# while message!='quit':
# message=input(prompt)
# print(message)
#3.使用标志
#导致程序结束的事件有很多时,如果在一条while语句中检查所有这些条件,将很复杂
#在要求很多条件都满足才继续运行的程序中,可定义一个变量,用于判断整个程序是否处于活动状态,这个变量被称为标志
#标志为true时继续执行
# prompt="\n输入一个字符串"
# prompt+="\n否则quit退出"
# active=True
# while active: #只要active为True 循环就将继续执行
# message=input(prompt)
#
# if message=='quit':
# active=False
# else:
# print(message)
#4.使用break退出循环
#要立即退出while循环,不再运行循环中余下代码,可使用break语句
prompt="\n输入一个字符串"
prompt+="\n否则quit退出"
while True:
city=input(prompt)
if city=='quit':
break
else:
print("我想去的城市"+city)
#5.在循环中使用continue
number=0
while number<10:
number+=1
if number%2==0:
continue
print(number)
#三、使用while循环来处理列表和字典
#for循环是遍历列表的有效方式,但在for循环中不应修改列表,要在遍历列表的同时对其进行修改,可使用while循环
#1.在列表之间移动元素
#假设有一个列表,其中包含新注册但还未验证的用户,验证这些用户后将其移到另一个列表中。
#使用while循环在验证用户的同时将其从未验证的用户列表中提取出来,再将其加入到另有一个列表中。
unconfirmed_users=['alice','tom','jack'] #先建两个列表 一个是待验证用户列表
confirmed_users=[ ] #另一个是已验证的用户列表
#验证每个用户,直到没有未验证的用户为止
#将每个经过验证的列表都移到已验证用户列表中
while unconfirmed_users:
current_user=unconfirmed_users.pop()
print(current_user)
confirmed_users.append(current_user)
#显示所有已验证的用户
print("已验证的用户")
for confirmed_users in confirmed_users:
print(confirmed_users)
#2.删除包含特定值的所有列表元素
#之前使用remove()来删除列表中的特定值,之所以可行是因为要删除的值在列表中只出现一次,
#如果特定值出现多次,可使用while循环
#例:有个宠物列表,其中包含多个cat元素,要删除所有这些元素,可不断运行while循环,直到列表中不再包含cat值
pets=['dog','cat','rabbit','cat','cat','fish','cat']
print(pets)
while 'cat' in pets:
pets.remove('cat')
print(pets)
#3.使用用户输入来填充字典
responses={}
polling_active=True
while polling_active:
name=input("\n你叫什么名字")
response=input("你喜欢爬哪座山")
#将答卷存储在字典中
responses[name]=response
#看看是否还有人要参与调查
repeat=input("是否还想继续yes/no")
if repeat=="no":
polling_active=False
#调查结束 显示结果
print("\n----调查结果-----")
for name,response in responses.items():
print(name+"喜欢去爬"+response)
#四、if语句
#1.简单的if语句
age=19
if age>=18:
print("你已成年")
age_1=20
age_2=21
if age_1>=18and age_2<=30:
print("你们正值青年")
age=20
if age<=30or age>=18:
print("你好年轻")
#2.if-else语句
age=17
if age>=18:
print("你已成年")
else:
print("未成年")
#3.if-elif-else结构
#门票4岁以下免费,4-18岁收半价,18岁(含18)以上收全价10元
age=12
if age <4:
print("免费")
elif age>=4 and age<18:
print("半价5元")
else:
print("10元")
#使用多个elif代码块
age=12
if age <4:
price=0
elif age>=4 and age<18:
price=5
elif age>=18 and age<=60:
price=10
else:
price=5
print("花费"+str(price))
#else 可以省
#4.使用if语句处理列表
#水果店的商品有apple,pear,orange,grape 销售,如果断货了要提醒缺货
shops=['apple','pear','orange','grape']
for shop in shops:
print("水果种类有"+shop)
if shop=='apple':
print("苹果缺货")
else:
print("不缺货")