Exercise 21 study Drills part 4

Can you tell me why “my created code” gives a syntax error unless I comment out the previous called functions. I assume it’s holding some values that must be reset.

#function to add two numbers
def add(a, b):
    print(f"ADDING {a} + {b}")
    return a + b

#function to subtract two numbers
def subtract(a, b):
    print(f"SUBTRACTING {a} - {b}")
    return a - b

#function to multiply two numbers
def multiply(a, b):
    print(f"MULTIPLYING {a} * {b}")
    return a * b

#function to divide two numbers
def divide(a, b):
    print(f"DIVIDING {a} / {b}")
    return a / b

#print string
print("Let's do some math with just functions!")

#Call each function using two parameters and assigning to a different variables
"""
age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)

#print string and the contents of the appropriate variable
print(f"Age: {age}, Height: {height}, Weight: {weight}, IQ: {iq}")


#A puzzle for the extra credit, type it in anyway.
print("Here is a puzzle.")

#start with inner most parenthesis plugging in the contents from each variable
#performing operations and assigning to variable
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))

#print strings and variable contents in between
print("That becomes: ", what, "Can you do it by hand?"

"""


#This part of code gives syntax error unless previously called function is commented out
#my created problem - (25/5) x 2 -1 + 3 = 12

answer = add(3 , subtract(multiply(2 ,divide(25, 5)), 1))

print("\n")
print(f"Here is my problem: {answer} \n")

When I run your code without the pair of “”", this is what I get:

File “forum_help1.py”, line 51
answer = add(3 , subtract(multiply(2 ,divide(25, 5)), 1))
^
SyntaxError: invalid syntax

When I see a syntax error on a given line, I often find the mistake on the previous line. Your last print statement doesn’t have a closing parenthesis. Add that and the whole thing should run properly.

Dangit. I should have been more thorough. I appreciate your help. It worked like a charm after the correction. Have a great day!

The general rule is if you get an error then you have to assume you are totally and completely wrong until you can prove that you are not. Most people though operate the inverse way and assume what they see is correct until they’re proven wrong. That’s a difficult shift, but once it happens you’ll be able to fix errors easier.

Great advice. I know that I’m a ways away, but understanding written code is easier than actually creating code to create something that you want to do. Is there any great advise for that? Can I also ask if you have something (tutorial or book) that follows this book? A little more advanced, but not too far out there. Thank you. Great lessons and videos so far.

Billy

I actually get into that problem in the videos, but then hammer it in the Learn More Python 3 The Hard Way book https://learncodethehardway.org/more-python-book/

In that book it’s all about creating a ton of project from vague and kind of terrible descriptions, but then you can cheat and watch me do it. The key thing is I lead you through a progression that basically takes you from hacking quick and dirty code to being more professional and measuring your quality as you go. It also covers some of the main CS topics very lightly, like algorithms and programming languages, but focuses a lot on the system tools in unix.

The general advice though is, until you are “fluent” in python, you need to work on making results happen. As an adult person you can probably visualize what you want or write up a summary of how something should work. You most likely know how to draw and/or English, so this is pretty easy. The process then should be to write up or draw what you want, then write code that makes that happen. Eventually though you’ll be able to just write code and skip the english step.

For example, if I wanted to extract all the odd numbers from a list of numbers, I would just sit down and start writing Python. You however should write up an English list of comments, then put the Python under it:

 # open the list of numbers file
 # read each line and convert to int
 # if the element is % 2 == 0 then it's even, keep it
 # otherwise skip it

That could be one example of what you would do for that problem. Then you write the Python under that (keeping in mind that this is not a very good way to solve the problem, but a decent start for a beginner):

# open the list of numbers file
nums = open('mynumbers.txt')
results = []
# read each line and convert to int
for line in nums.readlines():
    n = int(line)
    # if the element is % 2 == 0 then it's even, keep it
    if n % 2 == 0:
        results.append(n)
    # otherwise skip it

You could then remove the comments once it was working and start to refine this. What you do in this case is simply work toward a result you want, and use Python comments as a TODO list or design guide. Once you have that it’s simply a matter of translating that into Python.

Awesome, Zed. I think I’m going to buy the next book now so that I can keep rolling. Thank you. Write in English first - convert to python.

1 Like