学习要点def 用于函数创建，函数代码块以 def 关键词开头，后接函数标识符名称和圆括号 ()*args意思是告诉python把函数中所有参数都接收进来，放进args列表中去。程序代码 # this one is like your scripts with argv def print_two(*args): arg1, arg2 = args print(f"arg1: {arg1}, arg2: {arg2}") # ok, that *args is actually pointless, we can just do this def print_two_again(arg1, arg2): print(f"arg1: {arg1}, arg2: {arg2}") # this just takes one argument def print_one(arg1): print(f"arg1: {arg1}") # this one takes no arguments def print_none(): print("I got nothin'.") print_two("Zed","Shaw") print_two_again("Zed","Shaw") print_one("First!") print_none() ```` ### 输出结果 arg1: Zed, arg2: Shawarg1: Zed, arg2: Shawarg1: First!I got nothin'.
程序代码： cars = 100 space_in_a_car = 4.0 drivers = 30 passengers = 90 cars_not_drivers = cars - drivers cars_driven = drivers carpool_capacity = cars_driven * space_in_a_car #拼车的承载量 average_passengers_per_car = passengers / cars_driven print("There are", cars, "cars available.") print("There are only", drivers, "drivers available.") print("There will be", cars_not_drivers, "empty cars today.") print("We can transport", carpool_capacity, "people today.") print("We have", passengers, "to carpool today.") print("We need to put about",average_passengers_per_car, "in each car.") 输出结果： There are 100 cars available. There are only 30 drivers available. There will be 70 empty cars today. We can transport 120.0 people today. We have 90 to carpool today. We need to put about 3.0 in each car.