python从入门到实践第七章_python从入门到实践第七章——输入与循环

'''

函数input()的工作原理:让程序暂停,等待用户输入。在终端输入与输出。

'''

message = input('Tell me something,and I will repeat it back to you: ')

print(message)

name = input('Please enter your name:')

print('hello,'+name+'!')

'''

多行内容输入:将字符串存入变量里,然后再传给input()。input()输入的只是字符串。

'''

prompt = 'if you tell us who you are,we can personalize the messages you see.'

prompt += '\nwhat is your first name?'

name = input(prompt)

print('\nHello,'+name.title()+'!')

'''

使用int()来获取数值输入

'''

age = input('How old are you?')

if int(age) >= 18:

print('true')

'''如果是if age >= 18:则会出现错误,程序无法执行。因为input()将接收的内容全部转换为字符串。'''

'''

判断一个人是否满足过山车的身高

'''

height = input('How tall are you,in inches?')

height =int(height)

if height >= 36:

print('you are tall enough to ride!')

else:

print('you will be able to ride when you are a little older.')

print('-------')

height = input('How tall are you,in inches?')

if int(height) >= 36:

print('you are tall enough to ride!')

else:

print('you will be able to ride when you are a little older.')

'''

求模运算符:%将两个数相除并返回余数。

'''

number = input('enter a number ,and I will tell you if it`s even or odd:')

number = int(number)

if number % 2 == 0:

print('the number '+str(number)+' is even')

else:

print('the number '+str(number)+' is odd')

'''

while循环

'''

current_number = 1

while current_number <= 5:

print(current_number)

current_number += 1

'''

for 语句表示的循环

'''

print('--------')

for current_number in range(1,6):

print(current_number)

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)

print(message)

'''

for 语句表示的循环

'''

print('--------')

for current_number in range(1,6):

print(current_number)

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':#如果输入的为quit则不输出quit而直接结束。

print(message)

'''

使用标志---用于多条件判断

'''

print('--------')

prompt = '\nTell me something, and I will repeat it back to you:'

prompt += '\nEnter "quit" to end the program. '#在字符串中要用""而不要重复用''

active = True

while active:

message = input(prompt)

if message == 'quit':

active = False

else:

print(message)

'''

退出循环:

1、break:直接跳出整个循环,不在运行循环中的余下代码

2、cotinue:返回到循环开头,并根据条件测试结果决定是否继续执行循环。

'''

print('--------')

prompt = '\nplease enter a name of a city you have visited:'

prompt += '\nEnter "quit" when you are finished. '#在字符串中要用""而不要重复用''

while True :

city = input(prompt)

if city == 'quit':

break

else:

print('I`d love to go to '+city.title()+'!')

print('--------')

current_number = 0

for current_number in range(1,11):

if current_number % 2 == 1:

print(current_number)

print('--------')

current_number = 0

while current_number < 10:

current_number += 1

if current_number % 2 == 0:

continue#此处若没有continue将会出错。

print(current_number)

'''

for与while 的用法区别:

for 是遍历列表,便利时不应修改列表。

while是遍历的同时修改列表

'''

'''

列表之间移动元素

'''

unconfirmed_users = ['alice','brain','candace']

confirmed_users = []

while unconfirmed_users :

current_user = unconfirmed_users.pop()

confirmed_users.append(current_user)

print('\nThe following users have been confirmed:')

for confirmed_user in confirmed_users:

print(confirmed_user.title())

'''

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

.remove()

'''

pets = ['dog','cat','dog','goldfish','cat','rabbit','cat']

print(pets)

while 'cat' in pets:

pets.remove('cat')

print(pets)

'''

使用用户输入来填充字典。

'''

responses = {}

polling_active = True

while polling_active:

name = input('\nWhat is your name?')

response = input('which moutain would you like to climb someday?')

responses[name] = response

repeat = input('Would you like to let another person respond?(yes/no)')

if repeat == 'no':

polling_active = False

print('\n---- poll Result----')

for name,response in responses.items():

print(name+' would like to climb'+response+'.')

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值