开发者

Problem with a function in Python 2.5 - what the argument should be; if an "if" statement is appropriate; making an accumulator work

开发者 https://www.devze.com 2023-02-12 04:51 出处:网络
I would appreciate help on this code.I\'m trying to get a function to print results.The program takes random numbers and determines whether they are even or odd.That works.It is supposed to then give

I would appreciate help on this code. I'm trying to get a function to print results. The program takes random numbers and determines whether they are even or odd. That works. It is supposed to then give a tally of how many numbers are odd and how many are even.

I'm trying to build that into the "tally_status_count" function but can't get it to work. I originally set up the 'even_total' and 'odd_total' variables as global variables, but then tried moving them into the function.

I'm lost. Any help would be appreciated.

Regards

Code:

import random

even_total = 0
odd_total = 0

def main():

    print 'Number\tStatus'
    print'______________'

    for count in range (10):
        number = random.randint(1, 10)
        status = odd_even(number)

        print number, '\t', status

    tally_status_count(odd_even)           

#Function to determine odd or even status
def odd_even(number):
    if (number % 2) == 0:
        status = 'Even'
    else:
        status = 'Odd'
    return status

#Function to tally odd and even counts
def tally_status_count(odd_even):

    even_total = 0
    odd_total = 0

    for status in range (status):
        if status == 'Even':
            even_total =开发者_运维技巧 even_total + 1
        else:          
            odd_total = odd_total + 1


    print
    print 'The total count of even numbers is: ', even_total
    print 'The total count of odd numbers is:  ', odd_total

main()


I see several problems with your code.

Problem 1: tally_status_count takes an argument which is never used. There's nothing wrong with this, but it's odd and it means you're probably not sure how to do what you're trying to do.

Problem 2: tally_status_count uses a variable which doesn't exist, 'status.' I am guessing that you probably intended status to be passed as an argument to the function. In that case, the function definition should look like this:

def tally_status_count(status):

When you fix problem 2, you end up with problem 3, which is...

Problem 3: malformed for-loop. This:

for status in range (status):

Is nonsense and won't work. range() is a function that takes some integers and returns a list of integers. If 'status' is a string, you can't use it with range().

Problem 4: You only get the status for the last random number, not for all of them. Every time you run this line:

status = odd_even(number)

the old value for status is thrown away.

This is probably the code you meant to write:

import random

even_total = 0
odd_total = 0

def main():

    print 'Number\tStatus'
    print'______________'
    statuses = [] #the status for each random number is added to this list

    for count in range (10):
        number = random.randint(1, 10)
        current_status = odd_even(number)

        print number, '\t', current_status

        statuses += [current_status] #add to the list

    tally_status_count(statuses) #go through the list and total everything up

#Function to determine odd or even status
def odd_even(number):
    if (number % 2) == 0:
        status = 'Even'
    else:
        status = 'Odd'
    return status

#Function to tally odd and even counts
def tally_status_count(statuses):

    even_total = 0
    odd_total = 0

    for status in statuses:
        if status == 'Even':
            even_total = even_total + 1
        else:          
            odd_total = odd_total + 1


    print
    print 'The total count of even numbers is: ', even_total
    print 'The total count of odd numbers is:  ', odd_total

main()


import random

def main():
    even_total = 0
    odd_total = 0

    for i in xrange(10):
        nbr = random.randint(1,10)
        if nbr % 2 == 0:
            even_total += 1
        else:
            odd_total += 1

    print 'even total', even_total
    print 'odd total', odd_total

if __name__ == "__main__":
    main()

This sounds like homework. What are the exact requirements? Do you need to separate the tally-er and odd_even into separate functions? It's easier if you don't. What arguments do they need to accept? What do they have to return?

Returning a string from this odd_even function is weird. Strings are ugly. I'd write a function like:

def is_even(nbr):
    return nbr % 2 == 0

Instead. The tally function could return a tuple (even_count, odd_count) if you wanted it to. It should probably accept a function and a list as arguments.

Like so:

import random

def is_even(nbr):
    return nbr % 2 == 0

def tally(func, iterable):
    yes = 0
    no = 0
    for x in iterable:
        if func(x):
            yes += 1
        else:
            no += 1
    return (yes, no)

def main():
    even_total, odd_total = tally(is_even, xrange(10))

    print 'even total', even_total
    print 'odd total', odd_total

if __name__ == "__main__":
    main()

Moving the totals into the function didn't work for you because they're limited to the scope of the function. After the function exists, they cease to exist. You have to return them, or keep them as globals/define them in a higher scope (you could write a class as well).

0

精彩评论

暂无评论...
验证码 换一张
取 消