The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

$ sign in Python homework

beefbeef Registered User regular
edited September 2007 in Help / Advice Forum
My lab assignment in Python requires the output to be in dollars with the $ sign right next to the number.

Like this: $46.95

I wrote the program in like 20 mins, but I'm new at this and this damn dollar sign has been kicking my ass for an hour.

Anyone care to drop some science on me?

beef on

Posts

  • AbsoluteHeroAbsoluteHero __BANNED USERS regular
    edited September 2007
    You sure thats not the shell designation or whatever?

    Like in a terminal


    $ ls
    $ echo
    $ Starting program:
    $ 46.95

    AbsoluteHero on
  • telcustelcus Registered User regular
    edited September 2007
    I assume you mean that the $ sign won't print?

    It might be a reserved character in Python, in which case, escaping it should do the trick. Escaping a character should be as simple as putting a backslash before it:
    \$ in this case.

    telcus on
    [SIGPIC][/SIGPIC]
  • beefbeef Registered User regular
    edited September 2007
    Yeah, we're not that advanced yet.

    This program is basically just to multiply/add money and I don't know where to put the $ other than a print statement, but that leaves a space.

    beef on
  • beefbeef Registered User regular
    edited September 2007
    telcus wrote: »
    I assume you mean that the $ sign won't print?

    It might be a reserved character in Python, in which case, escaping it should do the trick. Escaping a character should be as simple as putting a backslash before it:
    \$ in this case.

    I keep trying this, but I guess I don't know where to put it.

    right now my final line is like this

    print 'The total is:', total

    beef on
  • telcustelcus Registered User regular
    edited September 2007
    total = 'The total is $'+str(total)
    print total
    
    should do what you want

    telcus on
    [SIGPIC][/SIGPIC]
  • DocDoc Registered User, ClubPA regular
    edited September 2007
    yeah, what telcus said.

    Doc on
  • telcustelcus Registered User regular
    edited September 2007
    Doc wrote: »
    so like

    print "The total is: $", total

    I think the problem is that the print statement is inserting a space between the literal and the variable and he doesn't want that space.

    telcus on
    [SIGPIC][/SIGPIC]
  • DocDoc Registered User, ClubPA regular
    edited September 2007
    telcus wrote: »
    Doc wrote: »
    so like

    print "The total is: $", total

    I think the problem is that the print statement is inserting a space between the literal and the variable and he doesn't want that space.

    Yeah, I fixed that.

    Doc on
  • beefbeef Registered User regular
    edited September 2007
    telcus wrote: »
    Doc wrote: »
    so like

    print "The total is: $", total

    I think the problem is that the print statement is inserting a space between the literal and the variable and he doesn't want that space.
    telcus wrote: »
    total = 'The total is $'+str(total)
    print total
    
    should do what you want

    Right on both counts, thank you.

    I tried "+str()" earlier and I must have put a space somewhere.

    Thanks for all the replies. This can be closed

    beef on
This discussion has been closed.