DEV Community

Durga Pokharel
Durga Pokharel

Posted on

Day 3 of 100DaysOfCode: Python Program To Calculate Electric Bill

This is my 3rd day of #100daysofcode. I learned alots of thing about python basis with the help of Goggle, youtube and Coursera.
Today I learned specially about looping, List, file handling, function etc. I also did some course of coursera in the topic python data structure. Here is a code of mine which I used to calculate the amount of money purchasing for electric bill. In this code I used multiple if else statements for multiple conditions. There are different amount according to units consumed criteria.

# python program to calculate electric bill
units = int(input('Please enter the number of unit you consumed: '))
if(units < 50):
    amount = units * 2.60
    surcharge = 25
elif(units <= 100):
    amount = 130 + ((units - 50)*3.25)
    surcharge = 35
elif(units <= 200):
    amount = 130 + 162.50 + ((units - 100)* 5.26)
    surcharge = 45
else:
    amount = 130 + 162.50 + 526 + ((units - 200)*8.45)
    surcharge = 75
total = amount + surcharge 
print('\n Electricity Bill = %.2f' % total)
Enter fullscreen mode Exit fullscreen mode

#100DaysOfCode , day 3
* Learn about the python basis.
* Python code about the electric bill.
* Use of if and elif. pic.twitter.com/VTIGj4XPzX

— Durga Pokharel (@mathdurga) December 26, 2020

Top comments (6)

Collapse
 
natysmeeth profile image
NatySmeeth • Edited

Thank you so much! It's hard for e to calculate my bills before they come. However, I’m very surprised with It each time, and the sum always seems to be overestimated. I’m even thinking of switching to an alternative energy source after seeing this artickle energyaustralia.com.au/home/electr.... I think that solar batteries may save me a lot of money each month. Moreover, they save our ecology and let us waste less natural resources. I know that such resources may be all used in forty-fifty years, so what is left of our children? it has been my main concern lately, and I want to do something good for our planet.

Collapse
 
nitinkatkam profile image
Nitin Reddy

Kudos to you for progressing so much in only 3 days!

On the first line of your program, where you are converting the input into an integer, consider adding a check for isnumeric(). The reason for this is if the user enters invalid input (Eg. an alphabet), the program will exit with a ValueError exception. You will most likely learn about exception handling later.

Here's an example:

input_text = input('Enter a number')
if input_text.isnumeric():
  #write your logic here
  pass
else:
  print('The input was invalid')
Enter fullscreen mode Exit fullscreen mode

You can take the input check further - if the user enters a decimal number, converting the input to an integer will fail with a ValueError exception.

Collapse
 
nitinkatkam profile image
Nitin Reddy

UPDATE: I searched and found a ".isdigit()" method for strings which helps check if the string contains a positive integer. You can use ".isdigit()" instead of ".isnumeric()".

To allow negative numbers (Eg. when the electric company is unable to get the meter reading and uses an average-unit-count to bill you for the month, the next number of units can be in the negative), you can use an "or" in the condition with input_text.starts_with("-") and input_text[1:].isdigit()

Collapse
 
iamdurga profile image
Durga Pokharel

Thank you for sharing.

Collapse
 
iamdurga profile image
Durga Pokharel

Thank you for suggestion

Collapse
 
otumianempire profile image
Michael Otu • Edited

Did well on the 3rd day..