Python Forum
Overcoming ZeroDivisionError: division by zero Error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Overcoming ZeroDivisionError: division by zero Error
#7
#! /usr/bin/env python3
from ast import literal_eval

with open('test.txt') as file:
    for line in file:
        name, values = line.split('=')
        values = literal_eval(values)
        if sum(values) == 0:
            pass
        else:
            print(f'District: {name}')
            print(f'Hal received {values[3]} votes AVG: - {values[3]/sum(values):.2%}.')
test.txt
Output:
p3624=[454, 434, 175, 178, 82, 92, 6] p3635=[0, 0, 0, 0, 0, 0, 0] p3628=[45, 44, 175, 178, 82, 92, 6]
Output:
District: p3624 Hal received 178 votes AVG: - 12.53%. District: p3628 Hal received 178 votes AVG: - 28.62%.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Messages In This Thread
RE: Overcoming ZeroDivisionError: division by zero Error - by menator01 - Jun-11-2020, 09:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  ZeroDivisionError laurawoods 1 338 Apr-11-2024, 03:27 AM
Last Post: deanhystad
  I try to import Vpython and I get a "ZeroDivisionError" Jon2222 16 2,927 Jul-26-2023, 07:37 AM
Last Post: Jon2222
  Division questions Dionysis 5 1,172 Feb-14-2023, 02:02 PM
Last Post: Dionysis
  Division by zero and value of argument Lawu 5 3,373 Jul-01-2022, 02:28 PM
Last Post: Lawu
  Floor division problem with plotting x-axis tick labels Mark17 5 2,244 Apr-03-2022, 01:48 PM
Last Post: Mark17
  Division calcuation with answers to 1decimal place. sik 3 2,227 Jul-15-2021, 08:15 AM
Last Post: DeaD_EyE
  Floor division return value Chirumer 8 3,998 Nov-26-2020, 02:34 PM
Last Post: DeaD_EyE
  Integer division plozaq 2 2,058 Sep-28-2020, 05:49 PM
Last Post: plozaq
  Division of an integer into sub-numbers Richard_SS 4 3,039 Jun-14-2019, 11:47 AM
Last Post: DeaD_EyE
  ZeroDivisionError CIVILmath 2 2,594 Jan-17-2019, 02:38 AM
Last Post: CIVILmath

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020