Troubled by Tuple trouble

I’m doing ex21 in the first book, and get not only one error, but two. I don’t remember (or understand) the problem with a + b, which is about the third line down, but I also get an error message that says, ‘can only concatenate tuple, (not float) to tuple’ further down. Since I have not done anything with tuples, this really puzzles me. I did a search on Google and it took me to stackoverflow.com and only one of the three answers sounded like mine, and I have no idea what they are talking about. I looked through the list of questions on this site with ex21, and that only had one problem but it was not mine. What do I do now? Not2smart.

Hello Ric.

Are you following the book for Python3 or 2?
I must ask you to show us the code.
There must be something that is not obvious for you but other can perhaps see the reason for your error.
The exercise is not about tuples at all.
But we need to see your code to be able to help.

Looking forward to help you.
Ulfen69

1 Like

Ulfen69: This is the code, but it sometimes doesn’t copy exactly like my Notebook++ writes it.

def add(a, b):
print(f"ADDING {a} + {b}")
return a + b

def subtract(a, b):
print(f"SUBTRACTING {a}-{b}")
return a - b

def multiply(a, b):
print(f"MULTIPLYING {a} * {b}")
return a * b

def divide(a, b):
print(f"DIVIDING {a} / {b}")
return a / b

print(“Let’s do some math with just functions!”)

age = (30, 5)
height = subtract (70, 4)
weight = multiply (90,2)
iq = divide(100,2)

print(f"Age: {age}, Height: {height}, Weight: {weight}, IQ: {iq}")

A puzzle for extra credit, type it in anyway.

print(“Here is a puzzle.”)

what = add(age, subtract (height, multiply (weight, divide(iq, 2))))
print("That becomes: ", what, “Can you do it by hand?”)

One more thing. After I copied and pasted it from my Notebook ++, I found I had a period on the last line after ‘that becomes’ rather than a comma. I don’t know if that is my problem though. Thank you for looking at it. Not2smart

Hi Ric.

I found the problem in this part:

There should be:

age = add(30,5)

You just missed to put in the function (add)

And that’s why tuples came up. Python’s syntax for a tuple is parenthesis so it thought you were making age a tuple.

Oh, OK! You learn something every day as they say. Maybe after this I will count the words in a sentence that is in the book, and then count mine. If they don’t line up, I’ll look closer. Thank you for your time. Not2Smart.

After a while you start to get an eye for it. Even then you can spend hours not noticing a missing fullstop!

Hello Ric.

I would suggest that you should build the code piece by piece.

Do the first definition

def add(a, b):
    print(f"Adding {a } and {b}")
    return a + b

#then set a variable with this function
age = add( 20, 30)

#then print the variable.
print(f"Ulfen69 is {age} years old")

Do just this much before typing anymore.
Run the code to test it works before going on.

  • Do next function.
  • Add another variable and give it next function as value.
  • Put this into the print out.
  • Make sure this looks as expected before you continue.

This make you understand the process a little better.

@Not2smart - if you are not using an IDE (and you probably shouldn’t be if you follow Zed’s guidelines), then this site might help you (to save counting words). For worse case scenarios, when you literally cannot see the mistake, cut’n’paste Zed’s text into https://www.diffchecker.com/ and then compare it to your own.

1 Like

Actually, I think most of the text editors I recommend also have a “diff mode”. I’d have to check that but usually you open the two files in two panes and then “diff”. I know how to do it in Vim, but not in others.

Yeah it seems Atom needs a plug-in but there is one. VS Code certainly does but only on variations of the same document and Notepad+ has compare feature.

It would seem like a fundamental feature for all editors. Likewise I’m still using Vim

Actually the compare fuction of Notepad++ is really cool. Don’t use Notepad++ very often but the compare feature was really useful for a project where I had to debug weird templates with a lot of inline styles for a webshop. Everytime they updated the software I had to manually redo some changes :scream: Whithout the compare feature of Notepad++ I would have been lost.

1 Like