1. Welcome to Tacoma World!

    You are currently viewing as a guest! To get full-access, you need to register for a FREE account.

    As a registered member, you’ll be able to:
    • Participate in all Tacoma discussion topics
    • Communicate privately with other Tacoma owners from around the world
    • Post your own photos in our Members Gallery
    • Access all special features of the site

Programming in Python

Discussion in 'Technology' started by isu2014, Apr 4, 2013.

  1. Apr 4, 2013 at 1:17 PM
    #1
    isu2014

    isu2014 [OP] RAT Products

    Joined:
    Apr 12, 2010
    Member:
    #35140
    Messages:
    12,630
    Gender:
    Male
    First Name:
    Ryan
    Farmington, MN
    Vehicle:
    01 SUPERCHARGED TRD OR MANUAL 4x4 V6
    View the build. Too much to list.
    I'm writing a program for class and I am struggling. Python is the programming software we use. The task:

    User inputs-
    enter the number of mouse traps (ie. 60 is our test number)
    are you an iowa resident? (y/n)

    Outputs-
    Subtotal (ie. 115)
    Tax (ie. 0 if iowa resident is answered n, 6.9 if iowa resident is answered y)
    Total (ie. 115 or 121.9, depending on tax)

    My code so far is attached. When I run the code, I get a sytax error at the colon after if res == y:
    I'm not sure what to do to make this work. I've tried everything I know.

    Screen Shot 2013-04-04 at 3.15.44 PM.png
     
  2. Apr 4, 2013 at 1:19 PM
    #2
    ChamYota

    ChamYota AKA Crash Bandicoot

    Joined:
    Jul 6, 2011
    Member:
    #59431
    Messages:
    8,202
    Gender:
    Male
    First Name:
    Cam
    Clemson,South Carolina
    Vehicle:
    05 PreRunner AND 82' Toyota P/U 4wd
    H1 Morimoto projector with XB35 4300K bulbs with Mini-gatling gun shrouds, BHLM. Debadged,OEM roof rack,TW sticker,Sockmonkey Beside decals,265/70/16 Nitto terra grapplers with dick cepek DC'2 wheels,weathertech floor liners(front/back),USA Spec Ipod reader,Scanguage II,Bestop Supertopper and more in the works...
    I did python my first semester in college.

    Is raw_input("") a valid input reading code? i've never seen that.
     
  3. Apr 4, 2013 at 1:23 PM
    #3
    isu2014

    isu2014 [OP] RAT Products

    Joined:
    Apr 12, 2010
    Member:
    #35140
    Messages:
    12,630
    Gender:
    Male
    First Name:
    Ryan
    Farmington, MN
    Vehicle:
    01 SUPERCHARGED TRD OR MANUAL 4x4 V6
    View the build. Too much to list.
    raw_input is used when the input is a string and input is for integers.
     
  4. Apr 4, 2013 at 1:31 PM
    #4
    Shaowei

    Shaowei Well-Known Member

    Joined:
    Feb 1, 2010
    Member:
    #30483
    Messages:
    68
    Gender:
    Male
    First Name:
    Ryan
    New Mexico
    Vehicle:
    05 Tacoma 4x4 LB
    Try this:

    if res == 'y':
    blah blah blah
    else:
    blah blah blah

    The variable 'res' is a string. You need to compare it to a string. Without the quotes around y, Python considers it a variable and not a string. Hope this helps!
     
  5. Apr 4, 2013 at 1:39 PM
    #5
    isu2014

    isu2014 [OP] RAT Products

    Joined:
    Apr 12, 2010
    Member:
    #35140
    Messages:
    12,630
    Gender:
    Male
    First Name:
    Ryan
    Farmington, MN
    Vehicle:
    01 SUPERCHARGED TRD OR MANUAL 4x4 V6
    View the build. Too much to list.
    Tried it and I get the same syntax error. :( I'm gonna leave it like this though, since you are probably correct.
     
  6. Apr 4, 2013 at 1:43 PM
    #6
    Shaowei

    Shaowei Well-Known Member

    Joined:
    Feb 1, 2010
    Member:
    #30483
    Messages:
    68
    Gender:
    Male
    First Name:
    Ryan
    New Mexico
    Vehicle:
    05 Tacoma 4x4 LB
    Upon further inspection, the raw_input line in your attached image is missing a closing parentheses. I changed mine to match yours and I get the same error. Make sure you have closed off your parentheses in raw_input.
     
  7. Apr 4, 2013 at 1:44 PM
    #7
    Tuckerhump

    Tuckerhump Gentlemans Sausage

    Joined:
    May 26, 2012
    Member:
    #79561
    Messages:
    1,120
    Gender:
    Male
    CO
    Vehicle:
    09 DCLB SR5 4x4
    yolo
    x2, thats what i was thinking the error could be coming from
     
  8. Apr 4, 2013 at 1:50 PM
    #8
    isu2014

    isu2014 [OP] RAT Products

    Joined:
    Apr 12, 2010
    Member:
    #35140
    Messages:
    12,630
    Gender:
    Male
    First Name:
    Ryan
    Farmington, MN
    Vehicle:
    01 SUPERCHARGED TRD OR MANUAL 4x4 V6
    View the build. Too much to list.
    You are correct sir! Can't believe I missed that! Thank you much.
     
  9. Apr 5, 2013 at 5:33 PM
    #9
    wileyC

    wileyC Well-Known Member

    Joined:
    Oct 22, 2011
    Member:
    #65685
    Messages:
    2,931
    Gender:
    Male
    Centerville, OH
    Vehicle:
    2012 PreRunner Double Cab TRD Sport
    ...also, you can add a slight bit of robustness/improvement when dealing w/ string comparisons...

    for example:

    instead of;

    if res == "y":

    use;

    if res.strip() == "y":

    the strip method will remove any leading and trailing whitespace from strings...

    also, you may want to ensure that the "case" doesn't matter;

    if res.lower() == "y":

    ;)
     
To Top