python3接收用户输入的语句是_Python之用户输入以及while语句、函数

1>函数input()

让程序暂停运行,等待用户输入一些程序,获取用户输入,Python将其存储在变量中。

记住:Python2.7版本获取用户输入的是:raw_input()

使用input的话,会把用户的输入当做Python的代码来运行。

2>while可以用break语句退出循环

使用while循环时,当不再运行余下代码时,也不管前面测试结果如何时,

可以直接使用break语句。

while True:

city=raw_input("please input the city which you want to visit ?")

if city=='quit':

break

else:

print ("city:"+city.title())

2>while循环可以用contiued回答循环的开头:

要返回循环的开头而不是像break语句那样不再执行余下的语句,

就使用continue语句只退出当前的循环。

curr_number=0

while curr_number<10:

curr_number+=1

if curr_number%2==1:

continue

else:

print curr_number```

#####3>while循环

在列表中cat出现的次数是两次,循环的次数也是两次哦

并且利用循环删除列表中指定的函数

```python

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

print pets

while 'cat' in pets:

pets.remove('cat')

print pets

4>函数

关键字传实参,准确指出形参名和与之对应的实参名。

还可以设置默认值,当设置形参默认值时,必须列出没有默认值的形参。

即分为位置实参以及关键字实参

def favourite_book(love_book,hate_book):

print("My love_book is:"+love_book.title())

print("My hate_book is:"+hate_book.title())

favourite_book(love_book='windy',hate_book='san zhi laohu')

favourite_book(hate_book='san zhi laohu',love_book='windy')

5>在给函数传递列表参数时,不想修改列表可以同过将列表参数传递给函数,

在调用列表参数时传入的是列表的切片表示法。

function_name(list_name[:])

具体如下(####):

magicians=['lily','winney','sam','jonh']

def create_magician(magicians_list):

new_magicians=[]

"""for magician in magicians:

new_magician=magician+"the Greate !"

new_magicians.append(new_magician)

return new_magicians"""

while magicians_list:

curr_magician=magicians_list.pop()

new_magicians.append(curr_magician)

return new_magicians

def show_tow_magicians(new_magicians,old_magicians):

print ("new_magicians includes :"+str(new_magicians))

print

print("old_magicians includes :"+str(old_magicians))

##########

new_magicians=create_magician(magicians[:])

show_tow_magicians(new_magicians=new_magicians,old_magicians=magicians)```

#####6>传递任意数量的实参

结合使用位置实参以及任意数量的实参

使用指针*加变量名(例:\*p)接受任意数目的实参,

!*p将接受的多个实参,存储在列表中

!在编写函数前,将固定个数的型参放在前面,接受任意数目的实参放在后面。

```python

def makeing_pizza(size,*toppings):

print ("Making a "+str(size)+" pizza,-inch pizza with the following toppings:")

for topping in toppings:

print(topping)

makeing_pizza(12,'mushroom','cheese','beef')

7>使用任意数量的关键字实参

有时候,需要接受任意数量的实参,但是预先并不知道传递给函数的是什么样的信息,

可以将函数编写成能够接受任意数量的键值对,调用语句提供了多少就接受多少。

例如:你在编写一个网站,新用户填写而写可选资料时,你不确定用户会填写哪些可选信息,

也不确定会是什么样的信息,这时就可以用*p表示接受任意数量的关键字实参。

!**p的两个星号让Python创建了个名为p的空字典,并且将所有接收到的键值对都存储在这个字典中。

def build_profile(first,last,**user_info):

profile={}

profile['first_name']=first

profile['last_name']=last

for key,value in user_info.items():

profile[key]=value

return profile

my_profile=build_profile('fung','winney',city='guangzhou',age='22')

print ("My name is :"+my_profile['first_name'].title()+" "+my_profile['last_name'])

print ("I live in "+my_profile['city'].title()+" "+"and my age is "+my_profile['age']+' .')

!!!总结6、 7这两个知识点,分别是*P只含有一个星号,且把接收的任意个实参存储在列表中,

而**P则有两个星号,且将接收的任意实参存储到字典中且接收的任意实参是键值对。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值