Python学习笔记——输入语句和while循环
2018-03-26 17:46
806 查看
在这节课我们学习到了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 yo
4000
u 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
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 yo
4000
u 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
相关文章推荐
- Python学习笔记(10)- 用户输入和while循环
- 学习笔记☞ python 基础(python3)【三】(转义字符,字符串,while循环,break语句,列表)
- Python学习笔记04-字典和用户输入和 while 循环
- 【Python学习笔记】for和while循环语句
- python学习笔记之四:条件,循环和其他语句
- python学习笔记——循环语句条件语句
- 【day 5】python编程:从入门到实践学习笔记-用户输入和while循环(末答案)
- python学习2——if语句、elif、if嵌套、while循环、while嵌套
- Python学习笔记五:while语句
- Python学习笔记(6)-while循环、while嵌套
- Python学习笔记4_选择语句与循环语句
- Python基础教程学习笔记----第五章 条件、循环和其他语句
- Python学习笔记03 for循环和while循环
- python 条件,循环和其他语句的学习笔记
- Python学习笔记(五)条件,循环和其他语句(上)
- [python学习笔记]条件,循环和其他语句
- python学习笔记5-条件,循环和其他语句
- Python学习:两种循环语句for和while
- python学习之语句:循环语句(for,while)
- Python学习笔记(五),条件,循环和其它语句(下)