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 RAT PRODUCTS, Apr 4, 2013.

  1. Apr 4, 2013 at 1:17 PM
    #1
    RAT PRODUCTS

    RAT PRODUCTS [OP] Well-Known Member

    Joined:
    Apr 12, 2010
    Member:
    #35140
    Messages:
    13,728
    Gender:
    Male
    First Name:
    Ryan
    Farmington, MN
    Vehicle:
    Cummins Coal Roller
    Smokin with a smarty.
    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.jpg
     
  2. Apr 4, 2013 at 1:19 PM
    #2
    ChamYota

    ChamYota Crash Bandicoot Or Groot AKA Cham "Scottalot" Yota

    Joined:
    Jul 6, 2011
    Member:
    #59431
    Messages:
    12,356
    Gender:
    Male
    First Name:
    Cam
    Columbia,South Cakalacky
    Vehicle:
    05' Taco 4x4 swapped/ 2007 Toyota Land Cruiser
    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
    RAT PRODUCTS

    RAT PRODUCTS [OP] Well-Known Member

    Joined:
    Apr 12, 2010
    Member:
    #35140
    Messages:
    13,728
    Gender:
    Male
    First Name:
    Ryan
    Farmington, MN
    Vehicle:
    Cummins Coal Roller
    Smokin with a smarty.
    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
    RAT PRODUCTS

    RAT PRODUCTS [OP] Well-Known Member

    Joined:
    Apr 12, 2010
    Member:
    #35140
    Messages:
    13,728
    Gender:
    Male
    First Name:
    Ryan
    Farmington, MN
    Vehicle:
    Cummins Coal Roller
    Smokin with a smarty.
    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
    travel_taco

    travel_taco Gentlemans Sausage

    Joined:
    May 26, 2012
    Member:
    #79561
    Messages:
    1,582
    Gender:
    Male
    CO
    Vehicle:
    09 Rust Bucket
    anti-fuel saving mods
    x2, thats what i was thinking the error could be coming from
     
  8. Apr 4, 2013 at 1:50 PM
    #8
    RAT PRODUCTS

    RAT PRODUCTS [OP] Well-Known Member

    Joined:
    Apr 12, 2010
    Member:
    #35140
    Messages:
    13,728
    Gender:
    Male
    First Name:
    Ryan
    Farmington, MN
    Vehicle:
    Cummins Coal Roller
    Smokin with a smarty.
    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:
    3,198
    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":

    ;)
     

Products Discussed in

To Top