Basics - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Basics

francisstokesfrancisstokes Posts: 43Member
[b][red]This message was edited by francisstokes at 2006-5-28 8:53:8[/red][/b][hr]
[b][red]This message was edited by francisstokes at 2006-5-28 8:51:50[/red][/b][hr]
I thought i'd take a little look at python yesterday (since ive never used it), and i made a little pound/stone converter, but i cant seem to get it to work (i get a syntax error). Heres the code:

[code]

input = string;

input = raw_input("Convert (p)ounds to stones, or (s)tones to pounds?")

if input == "p":

pnds = float;
stns = float;
pnds = float(raw_input("Value in pounds...")
print 'stones : ', (pnds / 14) # Syntax error occurs here

elif input == "s":

pnds = float;
stns = float;
stns = float(raw_input("Value in stones...")
print 'pounds : ', (stns * 14)

print


[/code]




Comments

  • infidelinfidel Posts: 2,900Member
    [b][red]This message was edited by Moderator at 2006-5-30 8:54:12[/red][/b][hr]
    Wow, I'll admit this one threw me for a minute, but the problem is the line above is missing a right-parenthesis.

    Here is my version of your script:

    [code]
    input = raw_input("Convert (p)ounds to stones, or (s)tones to pounds?")

    if input == "p":
    pnds = float(raw_input("Value in pounds...")[red]) # missing right paren[/red]
    print 'stones : ', (pnds / 14)

    elif input == "s":
    stns = float(raw_input("Value in stones...")[red]) # missing right paren[/red]
    print 'pounds : ', (stns * 14)

    print
    [/code]

    Python automatically "declares" variables when you assign something to them, so all of the "input = string" and "pnds = float" stuff you had was just useless. Not only that, but "input = string" is invalid because nothing named "string" exists at that point, so it raises an error. "pnds = float" works because there is an object named "float" built-in to python, it's the float type.

    So, strip out the bogus "declaration" lines and add the missing parentheses, and you're good to go. Good job and thanks for giving Python a try.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]



  • francisstokesfrancisstokes Posts: 43Member
    Yeah i found out about the not having to declare things yesterday. I come from a mainly C# / .NET background, so i just naturally assumed you had to declare everything. Thanks

Sign In or Register to comment.