避免被零除
Avoiding division by zero
我知道这是基本的,所以感谢您的帮助。
这是我的代码 - 我只是无法使用 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, '%')
我将代码移至函数中,使其更加结构化。该函数如下所示:
def occupied_percent(car_park_spaces):
if not car_park_spaces:
return None
occupied = 0
total_spaces = len(car_park_spaces)
for space in car_park_spaces:
if space:
occupied += 1
return occupied / total_spaces * 100
如果百分比计算成功,输出为 float
,否则输出为 None
。使用该函数时,您必须在打印结果之前检查 return 值。
接下来我创建了简化使用的打印功能:
def print_percentage(car_park_spaces):
result = occupied_percent(car_park_spaces)
print('The list is empty.' if result is None else f'The percentage of occupied spaces is {result}%.')
我运行一些测试用例:
print_percentage([])
print_percentage([0])
print_percentage([1])
print_percentage([1, 0])
print_percentage([1, 0, 1, 1])
产生此输出的那些:
The list is empty.
The percentage of occupied spaces is 0.0%.
The percentage of occupied spaces is 100.0%.
The percentage of occupied spaces is 50.0%.
The percentage of occupied spaces is 75.0%.
请注意,代码可能会进一步简化,因为 Python 倾向于使用大量单行代码。
我知道这是基本的,所以感谢您的帮助。
这是我的代码 - 我只是无法使用 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, '%')
我将代码移至函数中,使其更加结构化。该函数如下所示:
def occupied_percent(car_park_spaces):
if not car_park_spaces:
return None
occupied = 0
total_spaces = len(car_park_spaces)
for space in car_park_spaces:
if space:
occupied += 1
return occupied / total_spaces * 100
如果百分比计算成功,输出为 float
,否则输出为 None
。使用该函数时,您必须在打印结果之前检查 return 值。
接下来我创建了简化使用的打印功能:
def print_percentage(car_park_spaces):
result = occupied_percent(car_park_spaces)
print('The list is empty.' if result is None else f'The percentage of occupied spaces is {result}%.')
我运行一些测试用例:
print_percentage([])
print_percentage([0])
print_percentage([1])
print_percentage([1, 0])
print_percentage([1, 0, 1, 1])
产生此输出的那些:
The list is empty.
The percentage of occupied spaces is 0.0%.
The percentage of occupied spaces is 100.0%.
The percentage of occupied spaces is 50.0%.
The percentage of occupied spaces is 75.0%.
请注意,代码可能会进一步简化,因为 Python 倾向于使用大量单行代码。