文档目录:
一、if语句
二、检索条件
三、用户输入input
四、while+inoput(),让用户选择何时退出
五、break与continue
六、while循环处理字典和列表
---------------------------------------分割线:正文--------------------------------------------------------
1、if-else语句
cars=['audi','bmw','toyota']
for car in cars:
if car=='bmw':
print(car.upper())
else:
print(car.title())
查看结果:
Audi
BMW
Toyota
2、if-elif-else
age=12
if(age<4):
print("Your admission cost is $0.")
elif(age<18):
print("Your admission cost is $25.")
else:
print("Your admission cost is $40.")
查看结果:
Your admission cost is $25.
1、忽略大小写
car='Audi'
print(car=='audi')
print(car.lower()=='audi')
print(car.upper()=='AUDI')
查看运行结果:
2、检查不相等
car='Audi'
print(car !='AUDI')
查看运行结果:
True
3、检查多个条件
age_0=22
age_1=18
print(age_0>=21 and age_1>=21)
print((age_0>=21) and (age_1<21))
print(age_0>=21 or age_1>=21)
查看运行结果:
False
True
True
4、检查特定值是否包含在列表中
testList=['A','B','C']
print('A' in testList)
print('D' not in testList)
查看运行结果:
True
True
5、检查列表是否为空
testList2=[1,2,3]
testList3=[]
if testList2:
for test in testList2:
print(test)
else:
print("testList2为空")
if testList3:
for test in testList2:
print(test)
else:
print("testList3为空")
查看运行结果:
1
2
3
testList2为空
1、用户输入并返回
message=input("Tell me something,and I will repeat it back to you:")
print(message)
查看结果:
Tell me something,and I will repeat it back to you:hello world
hello world
2、f表达式返回
name=input("Please enter yout name:")
print(f"hello,{name}!")
查看结果:
Please enter yout name:jack
hello,jack!
3、更长的句子
prompt="Tell me something,and I will repeat it back to you,"
prompt+="
What's your name?
"
name=input(prompt)
print(f"hello {name.title()}")
查看结果:
Tell me something,and I will repeat it back to you,
What's your name?
mary
hello Mary
4、int()获取数值输入
age=input("How old are you?:")
age=int(age)
print(f"Your age is {age}!")
查看结果:
How old are you?:27
Your age is 27!
1、普通用法
prompt="Tell me something,and I will repeat it back to you:"
prompt+="
Enter 'quit' to be end the program."
message=""
while message!='quit':
message=input(prompt)
if message!='quit':
print(message)
查看结果
Tell me something,and I will repeat it back to you:
Enter 'quit' to be end the program.hello world
hello world
Tell me something,and I will repeat it back to you:
Enter 'quit' to be end the program.quit
2、进阶用法
prompt="Tell me something,and I will repeat it back to you:"
prompt+="
Enter 'quit' to be end the program."
#设置标志
active=True
while active:
message=input(prompt)
if message=='quit':
active=False
else:
print(message)
查看结果:
Tell me something,and I will repeat it back to you:
Enter 'quit' to be end the program.ok
ok
Tell me something,and I will repeat it back to you:
Enter 'quit' to be end the program.quit
1、break:退出循环
prompt="Tell me a city you want got to."
prompt+="
Enter 'quit' to be end the program:"
while True:
city=input(prompt)
if city=='quit':
break
else:
print(f"You love to go to {city.title()}!")
查看结果:
Tell me a city you want got to.
Enter 'quit' to be end the program:nanjing
You love to go to Nanjing!
Tell me a city you want got to.
Enter 'quit' to be end the program:newyork
You love to go to Newyork!
Tell me a city you want got to.
Enter 'quit' to be end the program:quit
2、continue:跳出本次循环,继续执行
current_number=0
while current_number<10:
current_number+=1
if current_number%2==0:
continue
else:
print(current_number)
查看结果:
1
3
5
7
9
1、while+列表:用户认证
#处理用户认证的列表
unconfirmed_users=['alice','brian','candace']
confirmed_users=[]
while unconfirmed_users:
current_user=unconfirmed_users.pop()
print(f"Verfying users:{current_user.title()}")
confirmed_users.append(current_user)
#显示所有验证的用户
print("
The following users have been confirmed!")
for confirm_user in confirmed_users:
print(confirm_user.title())
查看结果
Verfying users:Candace
Verfying users:Brian
Verfying users:Alice
The following users have been confirmed!
Candace
Brian
Alice
2、删除特定值的所有列表元素
pet=['dog','cat','pig','cat','triger','rabbit']
print(f"删除前:{pet}")
while 'cat' in pet:
pet.remove('cat')
print(f"删除后:{pet}")
查看结果:
删除前:['dog', 'cat', 'pig', 'cat', 'triger', 'rabbit']
删除后:['dog', 'pig', 'triger', 'rabbit']
3、使用字典记录问卷调差
mydict={}
active=True
while active:
name=input("Please enter your name:")
city=input("Please enter what city you want go to:")
mydict[name]=city
next=input("Do you want to send this questionnaire to another people(yes/no):")
if next=='no':
break
print("The questionnaire is over,now the result is:")
for name,city in mydict.items():
print(f"{name.title()} want go to {city.title()}!")
查看结果:
Please enter your name:lily
Please enter what city you want go to:nanjing
Do you want to send this questionnaire to another people(yes/no):yes
Please enter your name:mary
Please enter what city you want go to:shanghai
Do you want to send this questionnaire to another people(yes/no):yes
Please enter your name:tom
Please enter what city you want go to:london
Do you want to send this questionnaire to another people(yes/no):no
The questionnaire is over,now the result is:
Lily want go to Nanjing!
Mary want go to Shanghai!
Tom want go to London!