python从入门到实践 第七章习题 (高级编程技术 week4-1)

python从入门到实践 第七章习题 (高级编程技术 week4-1)

这一节课主要讲的是在python中用户输入的处理以及while循环的编写。

7.1 函数input()

需要注意的一点是,input()函数获取的用户输入一般都是字符串形式的,需要对数据进行进一步的处理(如课本示例中的int()函数将字符串转成数字)。

7-1 汽车租赁

print('Whick kind of cars do you want?')
car=('please enter the car you want : ')
print('Let me see if I can find you a Subaru')

7-3 10的整数倍

number = input('Please enter an number : ')
number = int(number)
if number % 10 == 0:
    print(number, ' is not an integer multiple of ten.')
else:
    print(number, ' is an integer multiple of ten.')

7.2 while循环的编写

在这一张,出现了三个新的关键字:while,break,continue

7-4 披萨配料

message = ''
while 1:
    message = input('Please enter ingredients for pizza')
    if message == 'quit'
        break
    else:
        print('We will add this ingredients in pizza!')

7-5 电影票

active = True
while active:
    age = input('Please enter your age : ')
    age = int(age)
    if age < 3:
        print('You are free!')
    elif 3 <= age && age <= 12:
        print('Your fee is ten dollars')
    else:
        print('Your fee is fifteen dollars')

7.3 使用while循环处理列表和字典

笔记:for循环是一种遍历列表的有效方式,但在for循环中不应修改列表,否则将导致Python难以跟踪其中的元素。要在遍历列表的同时对其进行修改,可使用while循环。通过将while循环同列表和字典结合起来使用,可收集、存储并组织大量输入,供以后查看和显示。

7-8 熟食店


sandwich_orders = ['sandwich1','sandwich2', 'sandwich3']

finished_sandwiches = []

while sandwich_orders:
    make_sandwich = sandwich_orders.pop()
    print('I made your ', make_sandwich,'.')
    finished_sandwiches.append(make_sandwich)

for finished_sandwich in fornished_sandwich:
    print(finished_sandwich, ' has finished.')

7-10 梦想的度假胜地

result = {}

active = True

while active:
    name = input('Please enter your name')
    respone = input('If you could visit on place in the world, where would you go?')
    result[name] = respone
    q = inpur('Quit? [N]/Y')
    if q == 'Y'
        break

for name, respone in result.items():
    print(name.title(), ' : ', respone)

猜你喜欢

转载自blog.csdn.net/wyfwyf12321/article/details/79708379