March 7th, 2013, 10:08 PM
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:
def main (): infi = open ("condAdd.txt", "r") nList = infi.readline().rstrip().split() num1 = eval(nList.find(" ")) num2 = eval (nList.rfind(" ")) if nList== 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.
March 7th, 2013, 11:20 PM
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:
There are other gems to uncover.
>>> 'add 23 88.2' . rstrip() . split()
['add', '23', '88.2']
[/code] are essential for python code and Makefiles!