1、函数 input()
提示2行输出 重点 +=
prom=“If you tell me who you are ,i can return it back to you .”
prom+="\nPlease input you name:"
name=input(prom)
#输入提示框 输入pick
print("\n Hello "+ name )
2、input()输入获取的时字符串,如果想数字字符串转换成数字需要用int () 函数。
age =input(“please input you age:”)
please input you age:20
age=int(age)
if age>18:
print("you old enough! ")
eles:
print(“you too young!”)
3、%求余数运算。
4、while循环,让用户选择退出
prom=“Tell me something ,and I will repeat it back to you :”
prom+="\nEnter ‘quit’ to end the program."
message=""
while message!=“quit”:
message=input(prom)
print(message)
下面不打印quit
prom=“Tell me something ,and I will repeat it back to you :”
prom+="\nEnter ‘quit’ to end the program."
message=""
while message!=“quit”:
message=input(prom)
if message!=“quit”:
print(message)
使用标志
prom=“Tell me something ,and I will repeat it back to you :”
prom+="\nEnter ‘quit’ to end the program."
flag=True
while flag:
message=input(prom)
if message==“quit”:
flag=False
else:
print(message)
使用break退出循环 。
prom=“Tell me something ,and I will repeat it back to you :”
prom+="\nEnter ‘quit’ to end the program."
while True:
message=input(prom)
if message==“quit”:
break
else:
print(message)
使用continue 退出当次循环。
打印1–10中的奇数
python 中没有i++ ,i–,用i=i+1 或者 i+=1
cur_num=0
while cur_num<10:
cur_num+=1
if cur_num%2==0:
continue
print(cur_num)
5、使用while处理列表和字典
在列表中移动元素
un_user=["tom","rose","bike"]
config_user=[]
while un_user:
user=un_user.pop()
config_user.append(user.title())
for co in config_user:
print(co.title())
删掉包含特定值的所有列表元素
citys=["beijjing","shanghai","najing","shanghai","hefei","shanghai"]
# ci="shanghai"
while "shanghai" in citys:
citys.remove("shanghai")
print(citys)
while 填充字典 姓名和爱好对应起来,利用键-值 字典
responses={}
flag=True
while flag:
name=input("\nwhat you name?")
answer=input("what you like?")
responses[name]=answer
ag=input("do you have another response?(yes/no)")
if ag=="no":
flag=False
print("\n -----the result----- ")
for name,response in responses.items():
print(name+" will like "+response+".")