Python学习笔记——输入语句和while循环

在这节课我们学习到了python中的输入语句(读取一行的字符串)和while循环
内容也比较简单,直接使用input()函数和while语句即可实现,以下是部分练习题的代码:

#7-3 输入整数
num=int(input('Plz enter a number: '))
if num%10==0:
    print('The number is mutiple of 10.')
else:
    print('The number is not mutiple of 10.')
print('\n')

#7-6 用不同的方式结束循环
count=3
while count>0:  #条件语句控制
    ing=input('Plz enter the ingredient you want: ')
    print('We will add ',ing,' on your pizza.')
    count-=1
print('Plz wait for your pizza.')
active=True
while active:  #使用标识控制
    ing=input('Plz enter the ingredient you want(enter "quit" to break):')
    if ing=='quit':
        active=False
        continue
    else:
        print('We will add ',ing,' on your pizza.')
print('Plz wait for your pizza.')
while 1:
    ing=input('Plz enter the ingredient you want(enter "quit" to break):')
    if ing=='quit':
        break
    else:
        print('We will add ',ing,' on your pizza.')
print('Plz wait for your pizza.')
print('\n')

#7-8&7-9 用循环操控列表(这里用字典实现)
sand_ors={'tuna':2,'pastrami':3,'beef':4}
print('We have sandwichs of:')
for sand in sand_ors:
    print(sand)
while 1:
    order=input('Plz enter your choice and how many sandwich you want(enter "quit" to break): ')
    if order=='quit':
        break
    l=order.split()
    sand_ors[l[0]]-=int(l[1])
    if sand_ors[l[0]]<0:
        print('Sorry, ',l[0],' sandwich has been sold out!')
        print('You can only get ',end='')
        print(sand_ors[l[0]]+int(l[1]),end=' ')
        print('sandwich.')
        sand_ors[l[0]]=0
    else:
        print('Plz wait for your ',l[0],'sandwich')
print('\n')

2018/3/26

猜你喜欢

转载自blog.csdn.net/ltc8600/article/details/79701437
今日推荐