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

My initial statement won't print and it won't calculate my average.

This is supposed to print an initial statement and then take in some numbers and average them. It's not printing the initial statement but it is allowing me to input my numbers. Lastly, it's not averaging up the numbers as it's supposed to. It's giving me this error:

for counter in range(number): TypeError: 'str' object cannot be interpreted as an integer

So, it had given me that error in other parts of my code, so I just added int () and then those sections worked but this time it says that int isn't supposed to go there. So....any sugestions?

def main(): 

#A Basic For loop
    print("I will display the numbers 1 through 5. ")
    for number in range [1, 2, 3, 4, 5]:
        print(number)


#The Second Counter code
    for seconds in range (1, 61):
        print(number)


#The Accumulator code

total = 0
for counter in range(5):
    number = input ("Enter a number: ")
    total = int(total) + int(number)
    print(number)


#The Average Age code
averageAge = 0
totalAge = 0
for counter in range(number):
    number = input ("Enter an age: ")
    totalAge = int(totalAge)/int(number)
    print("The average age is ")




#calls main
main()

Oh, plus, it's not counting, 1-61 as it's supposed to.

12 Answers

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,703 Points

Hey Cheri Castro

I added some comments.

def main(): # defining a function, NOT running it yet.

    #A Basic For loop
    print("I will display the numbers 1 through 5. ")

    for number in range [1, 2, 3, 4, 5]: # parenthesis ( ) should be used here, not [ ]
        # this will cause a not subscriptable error.

        print(number)

    #The Second Counter code
    for seconds in range (1, 61):
        print(number)


# ===========================
# CODE BETWEEN THESE LINES RUNS FIRST

# Because its outside the main() function.

#The Accumulator code

total = 0
for counter in range(5):
    number = input ("Enter a number: ")
    total = int(total) + int(number)
    print(number)


#The Average Age code
averageAge = 0
totalAge = 0

# Added this statement to check the number, and its type.
print('Number is: ', number, ' Numbers type is: ', type(number))

# your error happens here... the number variable was still a 'str' at this point
for counter in range(number): 
    number = input ("Enter an age: ")
    totalAge = int(totalAge)/int(number)
    print("The average age is ")
# ======================================


# ==== CODE BELOW RUNS NEXT =====

#calls main
main()  # this is when you are actually running the code inside main()

Thank you, I'm not sure that I understand all of the explanations but I'm going to play around with it and mull it over and see if I can come to understand it. The instructions, in my homework assignment called for the brackets, that's why I used the brackets. I'm going to go back and review that information and the video that came with it and see if I'm wrong on that. If I understand you correctly Chris, you are saying that the use of brackets has caused the initial statement and the rest of the code not to print and run, because it will cause an error that won't explain itself as an error. The second part, where it's supposed to count, isn't counting because it's tabbed over to the right and so, it's outside the main function? I think that's what you are telling me. I hope that I am understanding that right. I'm not sure what to do about that because Python keeps telling me that I need to tab the first print statement and then it wants me to tab the second part of the code, the counting part. I'm going to see about fixing the parenthesis issue and then go from there for now.

I've included the instructions that are given but when I went back to view the video, the lady doesn't actually show an example on whether to use the brackets or not. Do you think they show an example using the brackets, accidentally, or maybe even to throw us(the students in the class) off, to see if we know better than to use the brackets or parenthesis? If it was a typo, ok, no problem but if it was to specifically throw us off to see if we knew better, well I don't know better. If it's the latter then, that seems an underhanded way to present the material and not conducive to actually learning this stuff. Any thoughts on the matter? Not asking you to judge my school or teachers but just what the materials intent might have been.

Step 1: Start the IDLE Environment for Python. Prior to entering code, save your file by clicking on File and then Save. Select your location and save this file as Lab6-3.py. Be sure to include the .py extension.

Step 2: Document the first few lines of your program to include your name, the date, and a brief description of what the program does.

Step 3: Start your program with the following code for main:

Lab 6-3 Practicing for loops

the main function

def main():

#A Basic For loop

The Second Counter code

#The Accumulator code


#The Average Age code

calls main

main()

Step 4: Under the documentation for A Basic For Loop, add the following lines of code:

print 'I will display the numbers 1 through 5.' for num in [1, 2, 3, 4, 5]: print num

I had originally had num in the areas where it called for that to be typed but, nothing would run at all and it kept coming up with errors. So, I changed them all to number rather than num. Some areas in the instructions called for num others called for number, so I just made it uniform because from my limited experience Python doesn't try to interpret what you might have meant, it wants you to be uniform across the entirety of the code.

Ok, that didn't post like I had hoped it would, that's confusing.

I'm going to try again, so maybe it will make sense.

Step 1: Start the IDLE Environment for Python. Prior to entering code, save your file by clicking on File and then Save. Select your location and save this file as Lab6-3.py. Be sure to include the .py extension.

Step 2: Document the first few lines of your program to include your name, the date, and a brief description of what the program does.

Step 3: Start your program with the following code for main:

#Lab 6-3 Practicing for loops

#the main function
def main():

    #A Basic For loop


#The Second Counter code


    #The Accumulator code


    #The Average Age code

#calls main
main()

Step 4: Under the documentation for A Basic For Loop, add the following lines of code:

print 'I will display the numbers 1 through 5.'
for num in [1, 2, 3, 4, 5]:
print num

I hope this time, I've done it correctly, so it will post in a way that makes sense.

Ok, I started a new Python code, with just the initial section to see how it would work, since there are other errors causing the program not to run at all. Due to the entire program not running, I had originally though that it might have been because I was supposed to put range in it, where the instructions had left it out but when I tried on it's own, and left the range out and left the brackets in, it worked except, it prints the statement at the end instead of the beginning. So, I'm not sure how to fix that. I've tried to untab the first print statement but it results in an error that says a tab is expected. So, what I have is this...

def main():

#A Basic For loop
    print("I will display the numbers 1 through 5. ")
for number in [1, 2, 3, 4, 5]:
    print(number)


main()

So, that is working sort of, the statement is supposed to be printed at the top. Maybe if I place the print statement under the for portion? I'm going to try it.

Ok, this works, had to play with it some but got the first part working now on to the second part.

def main():

#A Basic For loop
   print("I will display the numbers 1 through 5. ") 
   for number in [1, 2, 3, 4, 5]: 
       print(number)


main()

Oh my goodness, I finally got the second part to run but had to play with the code as well and it is printing before the first part, which it isn't supposed to be that way. I think it's because which ever statement is further to the left, prints/runs first and then the one to the left of that prints next but the first statement is supposed to print first and the second of course second. The first statement,Python, won't allow me to start it flush to the left and the second statement won't allow me to indent it or tab to the right of where the first statement is starting. Wow, this is frustrating.

def main():

#A Basic For loop
   print("I will display the numbers 1 through 5. ") 
   for number in [1, 2, 3, 4, 5]: 
       print(number)

#The Second Counter code
for seconds in range(1, 61):
    print(seconds)


main()

Oh my goodness, your suggestions are helping me little by little play around with this thing and get it to working, little by little. I've got sections one, two, and three working correctly now. On to section four. I'm not going to re-post code unless I run into another problem.

Ok, oh my goodness, I'm down to the wire here, everything is working but...the last section of code, in the averageAge section. It all works until I get to the statement where it is supposed to give me the averageAge. With the test numbers that the instructions give me, it should be averaging to the tune of 31, as an averageAge but mine is coming up with 6.0.??? I checked back to insure that I've put my statements in the correct order, according to the instructions and it seems I have but something is clearly not quite right because I've input six varying ages and so the averageAge should be 31 but it's not working out that way. I'll keep playing with it to see if I can get it but until then, here's the code.

def main():

#A Basic For loop
   print("I will display the numbers 1 through 5. ") 
   for number in [1, 2, 3, 4, 5]: 
       print(number)

#The Second Counter code
   for seconds in range(1, 61):
       print(seconds)

#The Accumulator code
   total = 0
   for counter in range(5):
       number = input("Enter a number: ")
       total = int(total) + int(number)

   print("Your total is: ", total)

#The Average Age code
   averageAge = 0
   totalAge = 0
   number = int(input("How many ages do you want to enter?: "))
   for counter in range(number): 
       age = int(input("Enter an age: "))
       totalAge = int(totalAge) + int(number)

   averageAge = int(totalAge)/int(number)
   print("The average age is: ", averageAge)


main()

Aha!!!!! Got it!!!!! Finally!!!!! I switched some things around because I saw that what I was trying to divide, didn't totally make sense. Here's the final section. Thank you for your suggestions, it put me on the right track, even if it took me a little while.

#The Average Age code
   averageAge = 0
   totalAge = 0
   number = int(input("How many ages do you want to enter?: "))
   for counter in range(number): 
       age = int(input("Enter an age: "))
       totalAge = int(totalAge) + int(age)

   averageAge = int(totalAge)/int(number)
   print("The average age is: ", averageAge)
Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,703 Points

Ah glad you got it figured out! But yes you have to becareful with whitespace in Python. Python reads the whitespace to interpret which block its nested within.. whether that be a for block or if or within a function block. Glad you found your way to the answer though! :)

me too, I don't think I could've got there without your help. :)