1.用户输入和while循环
python2.7使用raw_input()来提示用户输入与python3中的input()一样,也将解读为字符串。
name=input("please enter your name: ") print("Hello, "+name+"!")
please enter your name: Eric Hello, Eric!
2.函数
(1)传递任意数量的实参
def make_pizza(*toppings): print(toppings) make_pizza('pepperoni') make_pizza('mushrooms','green peppers','extra cheese')
('pepperoni',) ('mushrooms', 'green peppers', 'extra cheese')
形参名*toppings中的星号让Python创建一个名为toppings的空元组,并将所有收到的值封装到这个元组中。
(2)使用任意数量的关键字实参
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 user_profile=build_profile('albert','einstein',location='princeton',field='physics') print(user_profile)
{'first_name': 'albert', 'location': 'princeton', 'field': 'physics', 'last_name': 'einstein'}
形参**user_info中的两个星号让Python创建了一个名为user_info的空字典,接受任意数量的关键字实参。