python的while以及input()教学

#函数input()的工作原理
message=input("Tell me something ,and I will repeat it ")
print(message)

#编写清晰的程序
name=input("please enter ypur name : ")
print("hello, "+name+"!")
prompt="If you tell us "
prompt+="\nwhat's your name ?"
name=input(prompt)# 输入的值赋值给name
print("\nhello, "+name+"!")

#使用int()来获取数值输入
height=input("how tall are you ?")
heigt=int(height)
if height>=36:
    print("you are allowed")
else:
    print("you are not allowed")

#求模运算符
number=input("Enter a number ,and I'will tell you if it's 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")

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

#让用户选择何时退出
prompt="\nTell me something "
prompt+="\nenter quit to end the program."
active =True
while active:
    message=input(prompt)
    if message =='quit':
        active=False
    else:
        print(message)

#使用break退出循环
prompt="\nTell me something "
prompt+="\nenter quit to end the program."
while True:
    city=input(prompt)
    if city=='quit':
        break
    else:
        print("I'd love to go to "+city.title())

#在循环中使用continue
current_number=0
while current_number<10:
    current_number+=1
    if current_number%2==0:
        continue
    print(current_number)

#在列表之间移动元素
unconfirmed_users=['alice','brain','candace']
confirmed_users=[]
while unconfirmed_users:
    current_user=unconfirmed_users.pop()
    confirmed_users.append(current_number)
print("The following users have been confirmed :")
for confirmed_user in confirmed_users:
    print(confirmed_user.title())

#删除包含特定值的所有列表元素——remove()
pets=['dog','cat','dog','goldfish','cat','rabbit','cat']
print(pets)
while 'cat'in pets:
    pets.remove('cat')
print(pets)

#使用用户输入来填充字典
response={}
polling_active=True
while polling_active:
    name=input("\nwhat's your name? ")
    answer=input("which mountain would you like to climb")
    response[name]=answer
    repeat=input("would you like to let another person respond (yes/no)")
    if repeat=='no':
        polling_active=False
print("\n ---poll results-----")
for a,b in response.items():
    print(a+" would you like to climb "+b+".")



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值