笨方法學(xué)python-習(xí)題4-變量和命名

習(xí)題4-變量和命名

  • python變量和命名練習(xí)程序
  • 附加練習(xí)

python變量和命名練習(xí)程序

# -*-coding: utf-8 -*-
cars = 100
space_in_a_car = 4.0
drivers = 30
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 availabes."
print "There are only", drivers,"drivers availabe."
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"

運(yùn)行結(jié)果

PS F:\python大師\習(xí)題> 
PS F:\python大師\習(xí)題> python .\ex4.py
There are 100 cars availabes.
There are only 30 drivers availabe.
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 in each car

附件練習(xí)

When I wrote this program the first time I had a mistake, and Python told me about it like this:

Traceback (most recent call last):
  File "ex4.py", line 8, in <module>
    average_passengers_per_car = car_pool_capacity / passenger
NameError: name 'car_pool_capacity' is not defined

原因:變量未定義

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容