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

Python

Why does taking input, storing as variable and then printing variable cause input variable to be repeated?

Hi, I'm working on Python Basics - Things that Count and am modifying a quiz example, trying to understand it better. I'm using the following code:

1 age = input ("What is your age? ")

2 days = age*52*7

3 summary = "You are {} days old, Amigo!".format (days)

4 print (summary)

When I run the program, the output is "You are 43434343434343434343434343434343434343 [ETC...] days old, Amigo!"

If I do as is written in the original example: age = 43, it works just fine. Would like to figure out why.

I'm thinking that this has to do with strings vs integers and how they are output but am very new to programming and to the forums. Sorry if a variant of this has already been asked. Thanks!

2 Answers

input returns the String "43" and not the integer 43. When the '*' operator is used, it concatenates multiple copies of the String together.

Thanks for the tip. I think I figured it out. It displays correctly now using the following:

age = input ("What is your age? ")
age = int(age)
days = (age)*52*7
summary = ("You are {} days old!").format (days)
print (summary)