《Python编程:从入门到实践》学习打卡7-输入+while循环

输入

input函数

作用:暂停程序。等待用户输入内容并将其存放进变量之中,等待进行下一步的操作

注意:每次使用input函数都应该在括号内给予明显的提示,即用户需要输入什么

name = 'if you tell me your name,we will send messages to you'
name = name + '\nso what is your name:'
message = input(name) #光标移至冒号后进行输入
print(message)

要点:对于多行显示的内容可以将需要打印的内容放进一个变量中,一行不能放下或多行打印的可以使用“+”进行字符串的拼接,多行打印时第二行前加一个**换行符转义字符(\n)**保证代码的美观简洁,再对变量使用input函数

int()获取数值输入

age = input('what is your age:') # 输入15
print(age)
if age == 15:
#if int(age) == 15: 使用int后字符串改编为数值型,条件成立,返回true
    print('true')
else:
    print('false') # 输出False

求模运算符(%)

定义:两个数相除返回余数,一般用于判断奇偶数

num = input('input the number:')
number = int(num) # 转换为数值型
if number % 2 == 0: # 余数为0则为偶数,否则为奇数
    print('even')
else:
    print('odd')

课后习题

7-1汽车租赁

car = input('input ideal car:')
print('let me search if I can find a ' + car.title())

7-2餐厅订位

total = input('how many persons for dinner? input the number:')
total = int(total) # 转换为数值型
if total >= 8:
    print('no vacant')
else:
    print('more table availble')

7-3十的整数倍

num = input('input a number:')
num = int(num)
if num % 10 == 0:
    print('this is times of ten')
else:
    print('this is not times of ten')

while循环

基本概念

while后面跟上循环进行的条件

num = 1
while num <=5: # 只要num小于等于5就一直执行循环
    print(num)
    num = num + 1 # 每执行一次循环,数字加一

终止循环

输入指定词

将指定词语的输入当作循环进行的条件,当触发指定词语时终止循环

temp = 'tell me something(press quit to end the program):'
message = " " # 赋初始值,使循环能够进行
while message != 'quit': #当message不是quit时,执行input函数输入内容
    message = input(temp)
    if message != 'quit': # 只有输入其他内容才会打印,输入quit不会打印
        print(message)

使用表征系统状态的标志

系统内含有多个条件,需要一个标志表明系统的状态,当标志处于活跃状态系统正常的运转,当标志处于关闭状态则系统不会运行,在循环中一样,没有标志循环就会终止

temp = 'tell me something(press quit to end the program):'
message = " "
active = True # 程序处于真状态
while active: # 当系统一直是真时
    message = input(temp)
    if message == 'quit':
        active = False # 系统变为假,结束循环
    else:
        print(message)

break退出循环

退出整个循环,不执行循环语句,该关键词适用于python中任何的循环

temp = 'please enter the name of a city you have visited(enter quit to stop):'
while True:
    place = input(temp)
    if place == 'quit': # 输入quit时循环中断
        break
    else:
        print('I love ' + place)

continue循环

退出当前循环,后面的语句不执行,直接执行下一个循环

i = 0
while i <= 10:
        i += 1
        if i % 2 == 0:
        	continue #如果为偶数就跳过当前循环
         print(i)

课后习题

7-4披萨配料

message = 'input the berden(press quit to end the program):'
active = True # 添加标志
while active:
    berden = input(message)
    if berden == 'quit':
        active = False
    else:
        print('we will add the ' + berden)

7-5电影票

如下

7-6三个出口

message = 'input your age:'
age = input(message)
while age != 'quit':
    age = int(age)
    if age < 0 or age > 150:
        print('get out!')
    elif age > 0 and age < 3:
        print('ticket is free')
    elif age >= 3 and age <=15: # 条件测试
        print('ticket is 10 dollars')
    elif age > 15:
        print('ticket is 15 dollars')
    break
message = 'input your age:'
active = True # 用active控制循环结束的时机
while active:
    age = input(message)
    age = int(age)
    if age < 0 or age > 150:
        print('get out!')
    elif age > 0 and age < 3:
        print('ticket is free')
    elif 3 < age <12:
        print('ticket is 10 dollars')
    elif age >= 12:
        print('ticket is 15 dollars')
    active = False
message = 'input your age:'
active = True
while active:
    age = input(message)
    if age == 'quit': # 使用break在输入'quit'时退出
        break
    else:
        age = int(age)
        if age < 0 or age > 150:
            print('get out!')
        elif age > 0 and age < 3:
            print('ticket is free')
        elif 3 < age <12:
            print('ticket is 10 dollars')
        elif age >= 12:
            print('ticket is 15 dollars')
        active = False

while处理列表和字典

列表间移动元素

uncomfired_users = ['john','kathy','kurt']
comfired_users = [] # 创建已验证空列表
while uncomfired_users: # 当未验证列表不为空时
    vertifying = uncomfired_users.pop() # 删除列表最后一个元素并放进变量vertifying
    print('vertifying:' + vertifying.title())
    comfired_users.append(vertifying) # 删除的元素加入已验证列表
for comfired_user in comfired_users:
    print(comfired_user.title()) # 遍历生成的已验证列表,打印

删除包含特定值的所有列表元素

pets = ['cat','dog','cat','goose','snake','cat']
while 'cat' in pets: # 条件测试
    pets.remove('cat') # 删除指定值
print(pets)

输入填充字典

用户每输入一次就将输入内容添加进字典中

new_dict = {}
active = True
while active:
    keys = input('input the key(enter quit to end the program):')
    if keys == 'quit': # 输入quit就退出程序
        break
    values = input('input the value')
    new_dict[keys] = values # 关键的一步
    
for key,value in new_dict.items(): # 遍历字典
    print(key + ':' + value)

课后习题

7-8熟食店

sandwich_orders = ['tuna sandwich','straberry sandwich','apple sandwich']
finished_sandwiches = []
while sandwich_orders:
    for sandwich_order in  sandwich_orders:
        print('I made your ' + sandwich_order)
        finished_sandwiches.append(sandwich_order)
        sandwich_orders.remove(sandwich_order) # 移除当前遍历的值
for finished_sandwiche in finished_sandwiches:
    print(finished_sandwiche)

疑问:'tuna sandwich’正常在第一位打印,为什么打印出来的是’apple sandwich’在前,'straberry sandwich’在后?

7-9五香烟熏牛肉卖完了

sandwich_orders = ['pastrami','tuna sandwich','pastrami','straberry sandwich','apple sandwich','pastrami']
finished_sandwiches = []
print('all the pastraim sold out')
while 'pastrami' in sandwich_orders:
    sandwich_orders.remove('pastrami') # 删除所有'pastrami'

while sandwich_orders:
    for sandwich_order in  sandwich_orders:
        print('I made your ' + sandwich_order)
        finished_sandwiches.append(sandwich_order)
        sandwich_orders.remove(sandwich_order)

for finished_sandwiche in finished_sandwiches:
    print(finished_sandwiche)

7-10梦想的度假胜地

result = {}
active = True
while active:
    name = input('what is your name(enter quit to end program):')
    if name == 'quit': # 输入quit终止循环
        break
    sight = input('if you could visit one place in the world, where would you go?:')
    result[name] = sight
if result == {}: # 如果直接输入quit,字典为空
    print('no result')
else:
    print('result:')
    for name,sight in result.items():
        print(name + ":" + sight)

小白入门python,欢迎指正与讨论

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值