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则有两个星号,且将接收的任意实参存储到字典中且接收的任意实参是键值对。

    原文作者:exmexm
    原文地址: https://www.jianshu.com/p/8099dcfcdd76
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞