1. 環境介紹
我使用的環境是windows/notepad++/Python 3.7.2
2. 代碼
# coding:utf-8
# 100輛車
cars = 100
# 一個車里有4個空位
space_in_a_car = 4.0
# 30個司機
drivers = 30
# 90個乘客
passengers = 90
# 不能開的車 = 車總量 - 司機的數量
cars_not_driven = 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_driven,"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")
3.輸出
ex04-1輸出.PNG
4.附加練習
無
5.總結
- 變量可以賦值給數字、字符串、另一個變量或者運算式;
- 使用print()函數輸出變量需要使用逗號 , 隔開 ;
- 在運算式中,任何一個數字使用浮點數,得出的結果也是浮點數
- / 和 //存在這一定區別,前者是自然除法,會得出浮點數,后者是整除法,不會得出浮點數。