Python Forum
Intializing return value to 0
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Intializing return value to 0
#3
This is a pretty choppy way to do this but I'm not sure why you set the value of num to 0 and then added 1 to it before even checking to see if it equals to 0 first. I rewrote what I (think?) you originally meant:

def running_average():
    total = 0
    num_values = 0
    average = 0
    while True:
        value = input("Enter an integer: ")

        if value == "done":
            break
        else:
            total += int(value)
            num_values += 1
            average = total / num_values
    return average


#print(running_average())
When it says you should initialize your return value to 0, it doesn't mean to literally return 0, but instead, you see how you're returning running_average? What it means is in your first declaration of running_average, you should set it to 0.
Reply


Messages In This Thread
Intializing return value to 0 - by adam10e10 - Apr-11-2019, 12:14 AM
RE: Intializing return value to 0 - by ichabod801 - Apr-11-2019, 02:14 AM
RE: Intializing return value to 0 - by loomski - Apr-11-2019, 05:43 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020