【发布时间】:2020-06-25 15:52:28
【问题描述】:
我知道这是基本的,所以任何帮助表示赞赏。
这是我的代码 - 我只是无法使用 if 语句来避免除以零的工作。
谁能帮忙?
# problem: calculate what percent of car park spaces are occupied
# input: integers 1 or 0, 1 signals an occupied space and 0 is empty
car_park_spaces = []
# sub problem: number of occupied spaces
occupied = 0
for car_park_space in car_park_spaces:
if car_park_space == 1:
occupied += 1
occupied_spaces = occupied
# sub problem: find the length of the list
percentage = occupied_spaces / len(car_park_spaces) * 100
# output: percent of occupied spaces
if not car_park_spaces:
print('The list is empty')
else:
print ('The percentage of occupied spaces is', percentage, '%')
【问题讨论】:
标签: python python-3.x python-2.7 divide-by-zero