0

Basically all I'm trying to do is get the list to sort and display in different ways. But it seems after I do the first print it will get "TypeError: 'int' object is not callable". I'm assuming that's because I'm at the end of the list and need to restart at the beginning? Not sure how to do that or if that's the reason.

    ##vars

Index = 0
NumSize = 0
NumInput = 0
ManNum = 0

##Asking for list size

NumSize = int(input("How many numbers would you like to enter?:\n====>"))

##Declaring list

Numbers = [0] * NumSize

##Collecting data

for Index in range(NumSize):
    NumInput = int(input("Please enter a number:\n====>"))
    Numbers[Index] = NumInput

##Getting highest number

MaxNum = max(Numbers)

##Printing list

print("::::The numbers you entered were::::")
for Numbers in Numbers:
    print(Numbers)

##Sorting list small to large

Numbers.sort()
print("::::The numbers while sorted::::")
for Numbers in Numbers:
    print(Numbers)

##Sorting list large to small

Numbers.reverse()
print("::::The numbers reversed::::")
for Numbers in Numbers:
    print(Numbers)

##Printing largest number

print("::::Largest number::::\n",
       MaxNum)
1
  • 2
    Your code looks like java, stick to PEP8, seriously. Commented Nov 9, 2014 at 7:51

5 Answers 5

5

You use

for Numbers in Numbers:
    print(Numbers)

You should not name your iterating variable the same name as your collection

try this

for number in Numbers:
    print(number)
Sign up to request clarification or add additional context in comments.

Comments

2

do not call the iterator with the same name as the collection, as you overwrite the later

for Numbers in Numbers:

this destroys "Numbers" array and after the loop "Numbers" is the last element of "Numbers" instead. Change all loops to

for number in Numbers:

Comments

0

Use anything instead of Numbers in your for loop, so that

for i in Numbers:`
    dosomething

If you print Numbers then you will see that it just carry last number you inserted in the list.

print Numbers
>>> #last Number of List         

Comments

0

Use a different variable than Numbers in your loops. You can also use sorted(Numbers) and reversed(Numbers) to reduce the amount of variable assignments you have to do. If you don't plan to reuse MaxNum, you can do that directly in the print as well.

Here is the fully updated program:

##vars
Index = 0
NumSize = 0
NumInput = 0

##Asking for list size
NumSize = int(input("How many numbers would you like to enter?:\n====>"))

##Declaring list
Numbers = [0] * NumSize

##Collecting data
for Index in range(NumSize):
    NumInput = int(input("Please enter a number:\n====>"))
    Numbers[Index] = NumInput

##Printing list
print("::::The numbers you entered were::::")
for number in Numbers:
    print(number)

##Sorting list small to large
print("::::The numbers while sorted::::")
for number in sorted(Numbers):
    print(number)

##Sorting list large to small
print("::::The numbers reversed::::")
for number in reversed(Numbers):
    print(number)

##Printing largest number
print("::::Largest number::::")
print max(Numbers)

Comments

-1

The answer is already given above. But for the explanation purpose here is what is going wrong.

>>> Numbers = [1, 4 , 10, 8]    #This is creating a list.
>>> for Numbers in Numbers:     #Now when you are iterating it definately gives the result
...     print(Numbers)
... 
1
4
10
8
>>> Numbers                     #But now check what is in Numbers
8
>>> 

You didn't wanted the above.

Note: >>> is the python interpretor prompt

As mentioned in the comment, please start writing code in pythonic way. Which is:

  1. You do not need to initialize variables like you did. You just need to assign them before you use them.
  2. When you ask for the inputs. you do need to go do list[index] instead you could use append method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.