Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
September 11th, 2004, 05:00 PM
 re__12
Registered User

Join Date: Sep 2004
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0

Ok, I started python about two weeks ago, and I am wondering how do do a couple of things. I want to, I think use the 'if' comand thingy. it doesn't work. I would think that one must write: (sorry if it is hard to understand)

# Erin Maloney
# 9/11/04
# Chapter 2; Challenge 3
# Program calculates either a 15, 20, or your own choice percent tip from entered bill

total = float(total)
total = 0
total = raw_input("What did your total bill come to? \$")
choice = raw_input("\Press a for a 15 percent tip, press b for a 20 percent tip, press c to figure out a different tip, or press d to exit"
if choice == "a":
print
tip15 = total * .15
print "n\A 15 percent tip would come to \$", tip15
print
choice = raw_input("\Press a for a 15 percent tip, press b for a 20 percent tip, press c to figure out your own tip, or press d to exit"
print

if choice == "b":
print
tip20 = total * .20
print "n\And a 20 percent tip would come to \$", tip20
print
choice = raw_input("\Press a for a 15 percent tip, press b for a 20 percent tip, press c to figure out your own tip, or press d to exit"
print

if choice == "c":
print
tip = raw_input("What tip would you like to leave? %"
print
tip = float(tip)
tip_percent = tip * .01
tipx = total * tip_percent
print "The tip for a % ", tip, "bill would be", tipx
choice = raw_input("\Press a for a 15 percent tip, press b for a 20 percent tip, press c to figure out your own tip, or press d to exit"
print

if choice == "e":
choice = raw_input("\Press a for a 15 percent tip, press b for a 20 percent tip, press c to figure out your own tip, or press d to exit"
print

if choice == "d":
print
end = raw_input("Are you sure? if so, press enter. (If you still want to stay, press e)"

else:
print
print "Nope, that wasn't a choice. Try again"
choice = raw_input("\Press a for a 15 percent tip, press b for a 20 percent tip, press c to figure out your own tip, or press d to exit"
print

#2
September 11th, 2004, 05:39 PM
 Boki
A wanna-be guru of some sort

Join Date: Sep 2004
Location: Either online or offline
Posts: 625
Time spent in forums: 2 Days 4 h 17 m 55 sec
Reputation Power: 19
Ok, next time use code tags when posting code. I don't usually read code if it's not formatted. I did not read through your code, but I did notice the missing of indentation after if statements. That might be one of the problems. Second, you might want to close raw_input() method with a ")", and also consider using while loops.
Anyway, could you post the error that the interpreter issues?

#3
September 12th, 2004, 03:09 AM
 DevCoach
Contributing User

Join Date: Feb 2004
Location: London, England
Posts: 1,585
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372

Python is virtually impossible to read without the indentation and if you paste it straight into a message without wrapping it in [ code ] tags it removes leading whitespace. So don't do that.

Also, some explanation of what you expected the program to do, and what it actually did would help people in tracking down your problem.

Dave - The Developers' Coach

#4
September 15th, 2004, 01:23 PM
 Zoso
Contributing User

Join Date: Jul 2003
Location: France
Posts: 53
Time spent in forums: 1 Day 47 m 5 sec
Reputation Power: 14
I didn't read the whole code but, however, there are some advices that I can tell you right now. First, you can remove the first two lines :
Code:
```total = float(total)
total = 0```

Unless the code you gave us is only a part of your script, the first line should fail because you reference a variable that hasn't been assigned yet. Swapping those two lines would make more sense but it would even be easier to just write
Code:
`total = 0.0`

However, it's still useless because you redefine total on line 3 :
Code:
`total = raw_input("...")`

The good place to cast your total variable is right after line 3 because raw_input is expected to return a string. So, if you have some computation to do with total, I suggest you to write this instead :
Code:
`total = float( raw_input("...") )`

One last remark : don't use the print statement to leave a blank line as in
Code:
```print
print "hello"
print```

You can achieve the same result by writing
Code:
`print "\nhello\n"`