自学Python从入门到放弃,15

用户输入

Python3 中 input() 函数接受一个标准输入数据,返回为 string 类型。
Python2 中 input() 相等于 raw_input() ,用来获取控制台的输入。
raw_input() 将所有输入作为字符串看待,返回字符串类型。而 input() 在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )。

让用户选择何时退出
prompt = '\nTell me something,and I will repaet it back to you:'
prompt += "\n Enter 'quit' to end the program."
message =''
while message !='quit':
  message = input(prompt)
  if message != 'quit':
    print(message)

Tell me something,and I will repaet it back to you:
 Enter 'quit' to end the program.Hello everyone!
Hello everyone!

Tell me something,and I will repaet it back to you:
 Enter 'quit' to end the program.Hello again!
Hello again!

Tell me something,and I will repaet it back to you:
 Enter 'quit' to end the program.quit
>>> 

使用标志

prompt = '\nTell me something,and I will repaet it back to you:'
prompt += "\n Enter 'quit' to end the program."
message =''
active = True##让程序最初处于活动状态
while active:
  message = input(prompt)
  if message == 'quit':
    active = False##这样做简化了while语句,因为不需要在其中做任何比较——相关的逻辑由程序的其他部分处理。只要变量active为True,循环就将继续运行。
    ##用户输入quit我们就将变量active设置为False,这将导致while循环不再继续执行。如果用户输入的不是quit,我们就将输入作为一条消息打印出来。
  else:
    print(message)
Tell me something,and I will repaet it back to you:
 Enter 'quit' to end the program.
Hello
Hello

Tell me something,and I will repaet it back to you:
 Enter 'quit' to end the program.nihao
nihao

Tell me something,and I will repaet it back to you:
 Enter 'quit' to end the program.aaa
aaa

Tell me something,and I will repaet it back to you:
 Enter 'quit' to end the program.quit
>>> 

在要求很多条件都满足才继续运行的程序中,可定义一个变量,用于判断整个程序是否处于活动状态。这个变量被称为标志,充当了程序的交通信号灯。你可让程序在标志为True时继续运行,并在任何事件导致标志的值为False时让程序停止运行。这样,在while语句中就只需检查一个条件——标志的当前值是否为True,并将所有测试(是否发生了应将标志设置为False的事件)都放在其他地方,从而让程序变得更为整洁。

在列表之间移动元素

old_list_s = ['zhangsan','lisi','wangwu','zhaoliu']
new_list_s = []
while old_list_s:
  Temporary_list_s = old_list_s.pop()
  print('new user:'+Temporary_list_s.title())
  new_list_s.append(Temporary_list_s)
print('\nExisting users have:')
for new_list in new_list_s:
  print(new_list.title())

new user:Zhaoliu
new user:Wangwu
new user:Lisi
new user:Zhangsan

Existing users have:
Zhaoliu
Wangwu
Lisi
Zhangsan
>>> 

定义函数

打印简单函数
def happy(name):
  print("Hello",name.title()+'!')
name=input("What's your name?")
happy(name)

What's your name?hucheng
Hello Hucheng!
>>> 

退回字典

def name(first_name,last_name,age=''):
  person ={'first_name':first_name,'last_name':last_name}

  if age:
    person['age'] = age
  return person

people_name1 = name('hu','cheng')
people_name2 = name('li','hongfei',age=21)
print(people_name1)
print(people_name2)


>>> 
{'first_name': 'hu', 'last_name': 'cheng'}
{'first_name': 'li', 'last_name': 'hongfei', 'age': 21}
>>> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值