Jump to content

I was learning python before school started, then had to take a break for 2 weeks, now im getting back into it and i don't remember how to save the return value as a variable.  There may be some other things, I am going to attatch the instructions and what I have, please help.

Thanks

post-119027-0-95756200-1440011975.png

I can help with programming and hardware.

<Script>alert("This website is vulnerable to XSS");</Script>

Link to comment
https://linustechtips.com/topic/433546-simple-python-help-very-simple/
Share on other sites

Link to post
Share on other sites

You aren't returning anything from average() is the problem. You just have return at the end. You need

return total / len(numbers)

Workstation: i7-4930k | Asus Rampage IV Gene | Reference GTX 780 | 32GB Crucial Ballistix | 500GB Samsung 840 EVO | Corsair RM650 | MidNight Black BitFenix Prodigy M

 

Old Rigi5-2500k @ 4.7 GHz | Asus P8P67 Deluxe | EVGA GTX 560 | 16GB Corsair Vengeance | 240 GB Samsung 830 Pro | 1TB Hitachi | CoolerMaster Storm Scout (1)

Link to post
Share on other sites

@littlepigboy5 Why post this in trouble shooting and not programming? I've never touched python before but I would assume it would be just like C++/C/C#/Java.

return insert_Variable_Name_Here

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to post
Share on other sites

 

You aren't returning anything from average() is the problem. You just have return at the end. You need

return total / len(numbers)

There's a lot more wrong than that. I'd suggest looking up a beginners tutorial that starts from the absolute basics that isn't codeacademy.

1474412270.2748842

Link to post
Share on other sites

Like Fizzlesticks said, there's a lot of problematic stuff in the code here.  I'll second a recommendation for some basic introduction to Python.  That said, two ways to do what you're trying to do:

 

First, a very compact way, in two lines (including the def line).  You can write return [stuff], and if [stuff] is an expression or equation rather than a single variable or value, Python will return the value of that expression/equation.

def average(numbers):   return (sum(numbers) / len(numbers))

Second, a way that's much closer to how you've done it.

def average(numbers):    total = sum(numbers)    total = float(total) # Converts total to a float    total /= len(numbers) # equivalent to total = total / len(numbers)    return total

This one sums up all the input numbers, and stores that value in the variable total.  Then it converts total to a float and divides it by the length of the input list.  Finally it returns the variable total.

 

Note: if you're using Python 3, then you don't need to convert to a float value before dividing.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×