Python第五课:输入循环

原码Gitee:https://gitee.com/xu-wen-jie/python.git
原码Github:https://github.com/miracleboys/Python.git

参考:《Python编程从入门到实践》



1.输入循环

1.1input()函数

message = input("Tell me somethiong, and I will repeat it back to you: ")#提示信息和用户输入
print(message)#打印用户输入
#打印内容
prompt = "If you tell us who you are, we can personalize the message you see."
prompt += "\nWhat is your first name?"
name = input(prompt)
print("\nHello, "+ name + "!")
height = input("How tall are you, in inches?")
height = int(height)#类型转换
if height >= 30:
    print('\nYou are tall enough to ride!')
else:
    print('\nYou will able to ride when you are will a little older.')
number = input("Enter a number, and I will tell you if it is even or odd:")
number = int(number)
if number%2 == 0:#求模运算
    print("\nThe number "+str(number)+" is even.")
else:
    print("\nThe nuumber "+str(number)+" is odd.")

1.2while循环

promot = "\nTell me somethong, and I will repeat it back to you:"
promot = "\nEnter 'quit' to end the program."
active = True
while active:
    message = input(prompt)
    if message == 'quit':
        active = False
    else:
        print(message)
prompt = "\nPlease enter the name of a city you have visited:"
promot += "\n(Enter 'quit' when you are finished.)"
while True:
    city = input(prompt)
    if city == 'quit':
        break   #退出循环
    else:
        print("I would love to go to "+city.title()+"!")
current_number = 0
while current_number <10:
    current_number +=1
    if current_number %2==0:
        continue #退出本次循环
    print(current_number)

1.3循环列表和字典

unconfirmed_users = ['alice','brian','candace']
confirmed_users = []
while unconfirmed_users:
    current_user = unconfirmed_users.pop()
    print("Verifying user: "+current_user.title())
    confirmed_users.aqqend(current_user)
print("\nThe following users have been confirmed:")
for confirmed_user in confirmed_users:
    print(confirmed_user.title())
pets = ['dog','cat','dog','goldfish','cat','rabbit','cat']
print(pets)
while 'cat' in pets:#如果'cat'在列表中循环继续,直到没有
    pet.reomve('cat')
print(pets)
#用户输入填充字典
rseponses = {}
polling_active = True
while polling_active:
    name = input("\nWhat is your name?")
    response = input("Which mountain would you like to climb someday?")
    reponses[name] = response
    repeat = input("Would you like to let another perosn respond?(Yes/No)")
    if repeat == "No":
        polling_active = False
print("\n---Poll Results ---")
for name, response in rseponses.items():
    print(name + " would like to climb "+response+".")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乱入梦魇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值