首页 > 编程语言 >五、python——用户输入和while循环

五、python——用户输入和while循环

时间:2023-02-06 11:45:24浏览次数:35  
标签:name python age while print input 输入

# 函数input() 让程序暂停运行,等待用户输入一些文本。
# 获取用户输入后,Python将其存储在一个变量中,以方便你使用
message = input("Tell me something, and I will repeat it back to you: ")
print(message)
name = input("Please enter your name: ")
print("Hello, " + name + "!")
# 输入数字,输出是字符串
age = input("how old are you?")
print(age)
age = int(age)
print(age > 18)

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

prompt = "\nTell me something, and I will repeat it back to you: "
prompt += "\nEnter 'quit' to end the program."
message = ""
while message != 'quit':
    message = input(prompt)
    if message != 'quit':
        print(message)

# 以while True 打头的循环(见❶)将不断运行,直到遇到break 语句
prompt = "\nTell me something, and I will repeat it back to you: "
prompt += "\nEnter 'quit' to end the program."
while True:
    message = input(prompt)
    if message != 'quit':
        print(message)
    else:
        break
# continue跳出本次循环
current_number = 0
while current_number < 10:
    current_number += 1
    if current_number % 2 == 0:
        continue
    else:
        print(current_number)

prompt = "\nAge: "
while True:
    message = input(prompt)
    if message == 'quit':
        break
    elif int(message) < 3:
        print('free')
    elif int(message) <= 12:
        print(10)
    else:
        print(15)

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

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

# 使用用户输入来填充字典
responses = {}
polling_active = True
while polling_active:
    name = input("what is your name? ")
    response = input("which mountain would you like to climb someday? ")
    responses[name] = response
    repeat = input("any one else? ")
    if repeat == 'no':
        polling_active = False
for name, response in responses.items():
    print(name + ' would like to climb ' + response + '.')

 

标签:name,python,age,while,print,input,输入
From: https://www.cnblogs.com/sxww-zyt/p/17094880.html

相关文章

  • 六、python——函数
    pizza.pydefmake_pizzas(size,*toppings):"""概述要制作的比萨"""print('size:'+size)print(toppings)deftest_one():print('one')deft......
  • 七、python——类
    类名应采用驼峰命名法,即将类名中的每个单词的首字母都大写,而不使用下划线。实例名和模块名都采用小写格式,并在单词之间加上下划线。对于每个类,都应紧跟在类定义后面包含一......
  • python 的爬虫技巧是什么?
    以前写毕设是关于网络爬虫这一块的。大学期间做的项目都是关于Java应用程序开发这一块的,这次不想再写那些烂大街的管理系统了,不如试试别的。正逢导师给出参考题目,于是选了......
  • python中的lambda函数用法
    python中的lambda函数用法 例1:传入多个参数的lambda函数defsum(x,y):returnx+y用lambda来实现: p=lambdax,y:x+yprint(p(4,6))例2:传入一个参......
  • 浅谈Python中的包
    浅谈Python中的包Package的定义(你以为的)你在很多的地方都能看到关于package的定义:在Python中在当前目录下有__init__.py文件的目录即为一个package。嗯,包括pytho......
  • 【微信小程序-原生开发】实用教程10 - 动态的新增、修改、删除(含微信云数据库的新增、
    开始前,请先完成首页的动态列表和动态详情的开发,详见【微信小程序-原生开发】实用教程09-可滚动选项,动态列表-步骤条(含事件传参),动态详情(含微信云查询单条数据doc)技术要点......
  • Python并发执行的简易实现:多进程、多线程、协程
    多进程importloggingimporttimefrommultiprocessingimportPoollogging.basicConfig(format='%(asctime)s%(message)s',level=logging.INFO)deff(x):time.sleep(......
  • 几行Python代码自动清理电脑重复文件
    随着互联网不断的发展,众多有技术的企业都已经转入人工智能的领域,我们知道人工智能首先就是需要会python编程,因此,如果能学好Python不仅仅只限于人工智能行业,能从事的行业还很......
  • 几行Python代码自动清理电脑重复文件
    随着互联网不断的发展,众多有技术的企业都已经转入人工智能的领域,我们知道人工智能首先就是需要会python编程,因此,如果能学好Python不仅仅只限于人工智能行业,能从事的行业还......
  • Python3排序sorted(key=lambda)
    Python3排序sorted(key=lambda)简述:假如d是一个由元组构成的列表,我们需要用到参数key,也就是关键词,看下面这句命令,lambda是一个隐函数,是固定写法,不要写成别的单词;x表示列......