#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    1
    Rep Power
    0

    Question I am lost; please help


    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


    *thanks for your help.
  2. #2
  3. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    20
    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?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Please read the 'asking for help' thread at the top of the forum.

    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
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    France
    Posts
    53
    Rep Power
    15
    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"

IMN logo majestic logo threadwatch logo seochat tools logo