python学习笔记之用户输入 和while循环

一用户输入
1、用户输入: input(函数),函数input()让程序暂停运行,等待用户输入一些文本,获取用户输入之后,python将其存在一个变量中,以方便使用。

message=input("plese input a number,and i will repeat it to you !")
print(message)

plese input a number,and i will repeat it to you !2
2
#int 来获取数值输入 age=input("how old are you ") age=int(age)#用int将字符串age改为数值age 才可以进行比较 age>18
how old are you 16
Out[2]:
False
2、取模运算%,算出的是余数

#判断一个数是奇数还是偶数
number=input("please input a number ,and i will tell you it is even or odd:")
number=int(number)
if number % 2==0:
    print("\nthe number "+str(number)+" is even.")
else:
    print("\nthe number "+str(number)+" is odd.")

please input a number ,and i will tell you it is even or odd:33

the number 33 is odd.

二 while循环
1.for 循环是针对每个元素的一个代码块,而while循环不断运行,直到之低昂的条件不满足为止

#使用while循环来计数
current_number=0
while current_number<=5:
    print(current_number)
    current_number+=1

0
1
2
3
4
5

2.退出while循环

#设定退出值
prompt='\n tell me something and i will repeat it for you:'
prompt+='\n enter "quit" to end the program.'
message=""
while message!="quit":
    message=input(prompt)
    print(message)

tell me something and i will repeat it for you:
enter “quit” to end the program.你好
你好

tell me something and i will repeat it for you:
enter “quit” to end the program.你好
你好

tell me something and i will repeat it for you:
enter “quit” to end the program.quit
quit

#使用标志
prompt='\n tell me something and i will repeat it for you:'
prompt+='\n enter "quit" to end the program.'
active=True #将active设置为标志
while active:
    message=input(prompt)
    if message =='quit':
        active=False
    else:
        print(message)

tell me something and i will repeat it for you:
enter “quit” to end the program.hello
hello

tell me something and i will repeat it for you:
enter “quit” to end the program.hello
hello

tell me something and i will repeat it for you:
enter “quit” to end the program.quit

#使用break退出,遇到break直接跳出循环,不再执行未执行的部分
prompt="\n please enter the name of a city you have visited!"
prompt+="\n(enter 'quit' when you finished)"
while True:
    city = input(prompt)
    if city == 'quit':
        break
    else:
        print(city)

please enter the name of a city you have visited!
(enter ‘quit’ when you finished)beijiang
beijiang

please enter the name of a city you have visited!
(enter ‘quit’ when you finished)nanjing
nanjing

please enter the name of a city you have visited!
(enter ‘quit’ when you finished)quit

#使用continue 回到循环开头,再次判断循环条件看是执行循环
#打印出奇数
number=0
while number <=10:
    number+=1
    if number%2==0:
        continue
    else:
        print(number)

1
3
5
7
9
11
避免无限循环,一定要有循环退出条件,
若是不小心写出了无限循环,使用ctrl+c强制退出

3.使用while循环来处理列表和字典

#在列表之间移动元素,将未验证的用户移到已经验证的列表中
#创建一个待验证的用户列表和一个用于接受已验证用户的空列表
unconfirmed_users=['mary','jack','alice']
confirmed_users=[]
#开始验证

while unconfirmed_users:
    current_user=unconfirmed_users.pop()
    print('werifying user:'+current_user.title())
    confirmed_users.append(current_user)
    
print('the following users have been confirmed: ')
for confirmed_user in confirmed_users:
    print(confirmed_user)
    

werifying user:Alice
werifying user:Jack
werifying user:Mary
the following users have been confirmed:
alice
jack
mar

#删除特定值的所有列表元素
pets=["cats","dogs","cats","pigs","rabbit","cats"]
print(pets)
while "cats" in pets: #没有此while的话,程序只会删除第一个cats
    pets.remove("cats")
print(pets)

[‘cats’, ‘dogs’, ‘cats’, ‘pigs’, ‘rabbit’, ‘cats’]
[‘dogs’, ‘pigs’, ‘rabbit’]

 #使用用户输入来填充字典
#创建一个用来收集调查信息的字典,用户名和喜欢的城市
responses={ }

#设置一个标志看调查是否继续
polling_active =True #标志条件为真
while polling_active:
    name=input("what you name?")
    city=input('which city do you like best?')
    responses[name]=city
    #看看是否要继续
    repeat=input("do you want other people answer?(yes or no)")
    if repeat=="no":#注意是字符no
        polling_active=False#循环退出
print("---poll results---")
for name,city in responses.items():
    print(name+" best love city is :"+city+".")

what you name?mary
which city do you like best?beijiang
do you want other people answer?(yes or no)yes
what you name?jack
which city do you like best?shanghai
do you want other people answer?(yes or no)no
—poll results—
marybest love city is :beijiang.
jackbest love city is :shanghai.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值