School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,015 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,075 people online right now. Registration is fast and FREE... Join Now!




bill calculator

 

bill calculator

chrishaizimsque

12 Oct, 2009 - 12:45 AM
Post #1

New D.I.C Head
*

Joined: 11 Oct, 2009
Posts: 5


My Contributions
i dont know what i am doing wrong in this code can i get some help

# a4-3.py
# chris haizimsque
# ncit 106 fall 2009

kwh = input("enter kilowatt-hours used: ")
if kwh < 0:
print "/nreckeck input value!"
print "(it was negative, and meters/ndon`t run backward.)"
else:
if kwh <= 500.0:
amounr = 20.0
elif kwh <= 1000.0:
amounr = 20.0 + 0.03*(kwh-500)
else:
amount = 35.0 + 0.02*(kwh-1000)
print "bill amount = $%1.2f" % amount




this is the thingy i get back that i am doing wrong

Traceback (most recent call last):
File "C:/Python26/bill calculator.py", line 16, in <module>
print "bill amount = $%1.2f" % amount
NameError: name 'amount' is not defined

User is offlineProfile CardPM
+Quote Post


5thWall

RE: Bill Calculator

12 Oct, 2009 - 10:35 PM
Post #2

D.I.C Regular
Group Icon

Joined: 17 Sep, 2008
Posts: 298



Thanked: 9 times
My Contributions
You only define amount once, in the final else. You might want to check the spelling on amounr. wink2.gif
User is offlineProfile CardPM
+Quote Post

dave_6_10

RE: Bill Calculator

24 Oct, 2009 - 11:37 AM
Post #3

New D.I.C Head
Group Icon

Joined: 19 Mar, 2008
Posts: 31


Dream Kudos: 25
My Contributions
It is actually theoretically possible for a meter to run backwards. In which case the electric company would actually send you a small check for manufacturing electric to their grid. This was done when a friend's dad set up a solar power generator in his home and for the first 2 months while he was testing the output he remained hooked up to his power company. They did send him a check in the amount of $3.87 however they also gave him a big runabout for doing this.
User is offlineProfile CardPM
+Quote Post

code_m

RE: Bill Calculator

24 Oct, 2009 - 07:39 PM
Post #4

D.I.C Head
**

Joined: 21 Apr, 2009
Posts: 124



Thanked: 8 times
My Contributions
here is the problem: if kwh < 0:

Since you set kwh using input, it is now a string, and python will not (and should not) compare a string to the int value of 0.

To fix this you must make sure the user inputs an integer and then convert that to int.

oh, and do check your spelling too.

This post has been edited by code_m: 24 Oct, 2009 - 07:41 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 07:34AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month