Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Hussein Amr
2,461 PointsWhy is my answer a string instead of an integer
def multiply(*args):
firstnum = 43
secondnum = args
product = firstnum * secondnum
print(product)
multiply(25)
when i run the program, it prints '25' 43 times instead of multiplying them. Why is that? they are both integers
3 Answers
james south
Front End Web Development Techdegree Graduate 33,271 Pointsargs is a tuple, so when you assign it to secondnum, secondnum points to a tuple. the contents of the tuple are then repeated 43 times. you can reproduce this with the following to see what is going on:
print((1,2)*4)
you get (1,2,1,2,1,2,1,2). to get the product of the two numbers as you intend, you need to slice into the tuple using bracket notation.
james south
Front End Web Development Techdegree Graduate 33,271 Pointshave you not taken the python courses here that cover slicing/indexing? it would be in a basic intro course. anyway, this link is from the python docs, the section is on strings but it covers slicing, which works the same for strings, lists, tuples etc.
https://docs.python.org/2/tutorial/introduction.html#strings
Hussein Amr
2,461 Pointsjames south I already know how to slice but i can't apply it here
james south
Front End Web Development Techdegree Graduate 33,271 Pointsyou didn't link to the challenge so i don't know what you are trying to do except from the code you posted. i got your code to multiply the numbers by slicing into the args tuple, per my first answer. if args were longer than one, you would loop through it and each index would be multiplied by firstnum and you could return a new list or tuple.
Hussein Amr
2,461 PointsHussein Amr
2,461 Pointscan you show me how? james south