0
import random

    numbers = int(input("Please, enter number {}: ".format(i+1)) for i in range(7))

# will display prompt like "Please, enter number 1:"
    print ("numbers entered:"), numbers # this will print the entered numbers

    positives = [num for num in numbers if num > 0]
    negatives = [num for num in numbers if num < 0]

# loop ends here
    print ("Sum of negative numbers is :", sum(negatives))
    print ("Average negative number is :", sum(negatives)*1.0/len(negatives))
    print ("Sum of positive numbers is :", sum(positives))
    print ("Average positive number is :", sum(positives)*1.0/len(positives))

my issue is that when i run in idle i get the following error: "int() argument must be a string or a number, not 'generator" not sure how to fix this... i know that when using python 3 i have to use (int) before input to call on an integer but am unsure how to get this to work.

3 Answers 3

2

As the error says, int does not support generator expressions. However, you try to give it one here:

numbers = int(input("Please, enter number {}: ".format(i+1)) for i in range(7))

The above code is equivalent to this:

numbers = int(
              # This is a generator expression
              input("Please, enter number {}: ".format(i+1)) for i in range(7)
          )

What I think you are trying to do is this:

numbers = [int(input("Please, enter number {}: ".format(i+1))) for i in range(7)]

See a demonstration below:

>>> numbers = [int(input("Please, enter number {}: ".format(i+1))) for i in range(7)]
Please, enter number 1: 1
Please, enter number 2: 2
Please, enter number 3: 3
Please, enter number 4: 4
Please, enter number 5: 5
Please, enter number 6: 6
Please, enter number 7: 7
>>> numbers
[1, 2, 3, 4, 5, 6, 7]
>>>
Sign up to request clarification or add additional context in comments.

2 Comments

Why would you encourage somebody that's obviously just learning the language to write something like that?
@SeanMcSomething - I don't think this is too advanced for him--he himself is using list comprehensions in his code.
0
import random

numbers = []
for i in range(7):
    numbers.append(int(input("Please, enter number {}: ".format(i+1))))

# will display prompt like "Please, enter number 1:"
print ("numbers entered:"), numbers # this will print the entered numbers

positives = [num for num in numbers if num > 0]
negatives = [num for num in numbers if num < 0]

# loop ends here
if len(negatives) > 0:
    print ("Sum of negative numbers is :", sum(negatives))
    print ("Average negative number is :", sum(negatives)*1.0/len(negatives))
if len(positives) > 0:
    print ("Sum of positive numbers is :", sum(positives))
    print ("Average positive number is :", sum(positives)*1.0/len(positives))

Comments

0

That error means exactly what it says - int() only takes strings or objects and you're trying to call int on a generator expression. Stripping it down to something a bit simpler, you're saying int( f(x) for x in range(7) ) which makes about as much sense as saying int( [1, 2, 3, 4, 5, 6, 7] ). Python won't magically apply functions across lists (or generators or iterators or...).

While you could simply toss this into a list comprehension by adding a few characters (as other posters have suggested), the nature of this problem suggests you're just learning Python. Writing ugly, convoluted expressions is a bad habit to get into and makes code hard to read/maintain/expand. You should probably use an actual loop for the sake of readability.

numbers = []
for i in range(7):
    prompt = "Please, enter number {}: ".format(i+1)
    user_input = input(prompt)
    user_num = int(user_input)
    numbers.append(user_num)

It takes up a few more lines but it's easier to follow or modify. Let's say you wanted to remove the limit on how many numbers to input or disallow prime numbers - do you think you'd be able to do that easily with the original version?

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.