一天快速入门Python语法基础之用户输入和if while语句

#1、函数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整除

#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) #3、使用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("不缺货")
相关文章
相关标签/搜索