#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Python decisions program


    Im in intorduction to programming and this one assignment has me clueless. I'm supposed to:
    "In this program, you are going to read a file and process the data based on the first token on the line.

    Download file conAdd.txt and examine it. It is a one line file with the word ‘add’ or ‘sub’ followed by two numbers.

    Your program is to add or subtract the second number from the first number based on the first word, i.e. operation."

    So heres what i got so far:
    Code:
    def main (): infi = open ("condAdd.txt", "r") nList = infi.readline().rstrip().split() num1 = eval(nList.find(" ")) num2 = eval (nList.rfind(" ")) if nList[1]== sub: sub = num2-num1 print (sub) main ()

    I don't need you to solve it for me but i just need some hints or advice.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,961
    Rep Power
    481
    I suggest you try experiments in the interpreter. It's not like you're sharing two punch card machines with crowds of students trying to meet tomorrow's deadline.

    The result of split() will surprise you,
    you'll see that looking for spaces in nlist is futile:
    Code:
    >>> 'add 23 88.2' . rstrip() . split()
    ['add', '23', '88.2']
    >>>
    There are other gems to uncover.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo