I'm trying to give python a list of numbers like this (5 6 7) and I want it to square each number (or as many numbers as I input). I'm trying out a list comprehension on this. I'm not sure where I'm going wrong. Any help would be great.

def squareEach(nums):
    nums = input("Enter the numbers: ")
    numbers = list(nums)
    square = [i*i for i in numbers]
    print square


Recommended Answers

All 3 Replies

In your definition for squareEach, you are saying that it takes a list of numbers as a parameter. When you call the function, you are not giving it any parameters. The correct definition in this case would be:

def squareEach(num):
    print [i * i for i in nums]

# Example call
squareEach([1, 2, 3, 4, 5])

You could also write the function the way you did (to accept user input for the numbers instead of specifying them as an argument), but that might be a bit complicated because you'd have to process user input as integers and check for invalid input, etc.

Here's one way of doing that:

def squareEach(nums):
    print [i * i for i in nums]

def readInput():
    print "Enter a whitespace separated list of numbers"
    nums = raw_input("==> ")
    return [int(n) for n in nums.split()]
    print "Invalid input. Quitting."

thanks for the help. now im trying to solve it another way. I want to take the input (a list of numbers separated by whitespace) and split it into separate string. Take those strings (of numbers) and square each one through a loop and print the result. I'm having problems again doing this. Here is my code so far:

import string

def main():
    nums = raw_input("Enter a list of numbers: ")
    print sumSquare(nums)

def sumSquare(nums):
    y = string.split(nums)
    for i in range(y):
        square = i**2 + x
    return square

Two problems:

(1) A function should only do one thing, and do it well. So you really want two functions:

One to take a string and split it into a list of numbers.

Another to take a list of numbers and return a list of squares of those numbers.

The advantage of thinking like this is that the second function is already done: squareEach from post #2.

This is an important theme in coding: if your functions do one thing only, then you can re-use them. We like re-using code, a lot. :)

(2) a Much Easier Issue is that you have a TypeError in line 10. It's because split() turns one string into a list of a lot of <b>strings</b> ... which aren't integers, and you never convert them.

So your code could be something like:

def main():
    nums = raw_input("Please enter a list of numbers, separated by commas: ")
    numbers = ParseNumbers(nums)
    squares = squareEach(numbers)
    print "The sum of squares is",sum(squares)

And then you can write the functions. Note that sum() is a built-in function in Python (re-using code again...)


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.