Just starting with python, encountering 1 problem...


Results 1 to 15 of 15

Thread: Just starting with python, encountering 1 problem...

  1. #1
    Join Date
    Oct 2002
    Location
    Soviet Missile Silo *duh*
    Posts
    99

    Post Just starting with python, encountering 1 problem...

    I have just started to learn python, and I am loving it, I have tried to learn other languages before, but this is really workin, but I am having one problem,

    Any time I have an "If" statement, Such as:

    if shape = 1:

    or

    if temperature > 50:

    Anyone know the problem?

  2. #2
    Join Date
    Oct 2002
    Location
    Soviet Missile Silo *duh*
    Posts
    99
    Here is a complete Code I am using as I am just starting out,

    Code:
    # Area Calculation Program
    
    print "Welcome to the Area Calculation program"
    print "---------------------------------------"
    print
    
    # Print out the menu:
    print "Please select a shape:"
    print "1  Rectangle"
    print "2  Circle"
    
    # Get the user's choice:
    shape = input("> ")
    
    #calculate the area:
    if shape = 1:
        height = input("Please enter the height:  ")
        width = input("Please enter the width:  ")
        area = height*width
        print "The area is", area
    else:
        radius = input("Please enter the radius:  ")
        area = 3.14(radius**2)
        print "The area is", area

  3. #3
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,936
    And, err, what's wrong with it? Error messages? Does it just not run right?

  4. #4
    Join Date
    Oct 2002
    Location
    Soviet Missile Silo *duh*
    Posts
    99
    Hah, I am a idiot, didn't post the error message..


    Code:
      File "C:/Documents and Settings/Russell Johnston/My Documents/Python stuff/askcalc", line 16
        if shape = 1:
                 ^
    SyntaxError: invalid syntax

  5. #5
    Join Date
    Mar 2000
    Posts
    251

    ==

    if shape == 1: ??
    'Scuse me while I kiss the sky

  6. #6
    Join Date
    Oct 2002
    Location
    Soviet Missile Silo *duh*
    Posts
    99
    Thank you so much!

  7. #7
    Join Date
    Oct 2002
    Location
    Soviet Missile Silo *duh*
    Posts
    99
    New problem though --

    Code:
    Please select a shape:
    1  Rectangle
    2  Circle
    > 2
    Please enter the radius:  2
    Traceback (most recent call last):
      File "C:\Documents and Settings\Russell Johnston\My Documents\Python stuff\askcalc", line 23, in ?
        area = 3.14(radius^2)
    TypeError: 'float' object is not callable
    I am assuming something is going wrong with the process where it cannot determine the answer, hence the float, I am guessing though.

  8. #8
    Join Date
    Oct 2002
    Location
    Soviet Missile Silo *duh*
    Posts
    99
    Meh, I fixed it, Just did radius*radius*3.14 instead.

  9. #9
    Join Date
    Oct 2002
    Location
    Soviet Missile Silo *duh*
    Posts
    99
    New Problem...

    Code:
    temperature = input("What is the temperature of the spam?")
    
        if temperature > 50:
            print "The salad is properly cooked."
        else:
            print "Cook the salad some more."
    The problem is with the "if", Which I cannot find out the problem to.

  10. #10
    Join Date
    Jan 2000
    Location
    Houston, TX, USA
    Posts
    9,994
    Originally posted by Russ356
    New problem though --

    Code:
    Please select a shape:
    1  Rectangle
    2  Circle
    > 2
    Please enter the radius:  2
    Traceback (most recent call last):
      File "C:\Documents and Settings\Russell Johnston\My Documents\Python stuff\askcalc", line 23, in ?
        area = 3.14(radius^2)
    TypeError: 'float' object is not callable
    I am assuming something is going wrong with the process where it cannot determine the answer, hence the float, I am guessing though.
    Text(stuff) is the syntax for a function call (where Text is the name of the function). That's what it was parsing it as. Also the exponentiation operator in python is **, not ^. You want:

    Code:
        area = 3.14 * (radius**2)
    We love Sensei!!
    A 1:1 ratio of Arby's sauce to Horsey sauce must be maintained.
    Like Linux? Want to like it more? Try Debian
    Best. (Python.) IRC bot. ever.
    Geekology

  11. #11
    Join Date
    Jan 2000
    Location
    Houston, TX, USA
    Posts
    9,994
    Originally posted by Russ356
    New Problem...

    Code:
    temperature = input("What is the temperature of the spam?")
    
        if temperature > 50:
            print "The salad is properly cooked."
        else:
            print "Cook the salad some more."
    The problem is with the "if", Which I cannot find out the problem to.
    Error message please.
    We love Sensei!!
    A 1:1 ratio of Arby's sauce to Horsey sauce must be maintained.
    Like Linux? Want to like it more? Try Debian
    Best. (Python.) IRC bot. ever.
    Geekology

  12. #12
    Join Date
    Jan 2000
    Location
    Houston, TX, USA
    Posts
    9,994
    Actually, I see an error

    input (use raw_input, btw) returns a string, which you can't compare to an integer. Either cast the input to an integer or ... yeah, just do that.
    We love Sensei!!
    A 1:1 ratio of Arby's sauce to Horsey sauce must be maintained.
    Like Linux? Want to like it more? Try Debian
    Best. (Python.) IRC bot. ever.
    Geekology

  13. #13
    Join Date
    Oct 2002
    Location
    Soviet Missile Silo *duh*
    Posts
    99
    I am assuming the raw helped but the error message says

    File "C:\Documents and Settings\Russell Johnston\My Documents\Python stuff\Spamsaladtemp.py", line 3
    if temperature > 50:
    ^
    SyntaxError: invalid syntax

  14. #14
    Join Date
    Oct 2002
    Location
    Soviet Missile Silo *duh*
    Posts
    99
    I got a new Error too!


    Code and message:
    Code:
    if f == 1:
            # Do something...
        elif f == 2:
    # Do something else...
        elif f == 3:
    # Do something completely different...
        else:
    # If all else fails...
    
      File "C:/Documents and Settings/Russell Johnston/My Documents/Python stuff/elseif.pyw", line 3
        elif foo == 2:
           ^
    SyntaxError: invalid syntax

    It seems it doesn't like when I use "if" any time.

  15. #15
    Join Date
    Jan 2000
    Location
    Houston, TX, USA
    Posts
    9,994
    Originally posted by Russ356
    I got a new Error too!


    Code and message:
    Code:
    if f == 1:
            # Do something...
        elif f == 2:
    # Do something else...
        elif f == 3:
    # Do something completely different...
        else:
    # If all else fails...
    
      File "C:/Documents and Settings/Russell Johnston/My Documents/Python stuff/elseif.pyw", line 3
        elif foo == 2:
           ^
    SyntaxError: invalid syntax

    It seems it doesn't like when I use "if" any time.
    Empty if/elif/else clauses are syntax errors. You can't have an if statement with no thing in it.
    We love Sensei!!
    A 1:1 ratio of Arby's sauce to Horsey sauce must be maintained.
    Like Linux? Want to like it more? Try Debian
    Best. (Python.) IRC bot. ever.
    Geekology

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •