栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python第五课:输入循环

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python第五课:输入循环

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

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


文章目录
  • 1.输入循环
    • 1.1input()函数
    • 1.2while循环
    • 1.3循环列表和字典


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循环列表和字典
un/confirm/ied_users = ['alice','brian','candace']
/confirm/ied_users = []
while un/confirm/ied_users:
    current_user = un/confirm/ied_users.pop()
    print("Verifying user: "+current_user.title())
    /confirm/ied_users.aqqend(current_user)
print("nThe following users have been /confirm/ied:")
for /confirm/ied_user in /confirm/ied_users:
    print(/confirm/ied_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+".")

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/347895.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号