Problem with a function in Python 2.5 - what the argument should be; if an "if" statement is appropriate; making an accumulator work -
i appreciate on code. i'm trying function print results. program takes random numbers , determines whether or odd. works. supposed give tally of how many numbers odd , how many even.
i'm trying build "tally_status_count" function can't work. set 'even_total' , 'odd_total' variables global variables, tried moving them function.
i'm lost. appreciated.
regards
code:
import random even_total = 0 odd_total = 0 def main(): print 'number\tstatus' print'______________' count in range (10): number = random.randint(1, 10) status = odd_even(number) print number, '\t', status tally_status_count(odd_even) #function determine odd or status def odd_even(number): if (number % 2) == 0: status = 'even' else: status = 'odd' return status #function tally odd , counts def tally_status_count(odd_even): even_total = 0 odd_total = 0 status in range (status): if status == 'even': even_total = even_total + 1 else: odd_total = odd_total + 1 print print 'the total count of numbers is: ', even_total print 'the total count of odd numbers is: ', odd_total main()
i see several problems code.
problem 1: tally_status_count takes argument never used. there's nothing wrong this, it's odd , means you're not sure how you're trying do.
problem 2: tally_status_count uses variable doesn't exist, 'status.' guessing intended status passed argument function. in case, function definition should this:
def tally_status_count(status):
when fix problem 2, end problem 3, is...
problem 3: malformed for-loop. this:
for status in range (status):
is nonsense , won't work. range() function takes integers , returns list of integers. if 'status' string, can't use range().
problem 4: status last random number, not of them. every time run line:
status = odd_even(number)
the old value status thrown away.
this code meant write:
import random even_total = 0 odd_total = 0 def main(): print 'number\tstatus' print'______________' statuses = [] #the status each random number added list count in range (10): number = random.randint(1, 10) current_status = odd_even(number) print number, '\t', current_status statuses += [current_status] #add list tally_status_count(statuses) #go through list , total #function determine odd or status def odd_even(number): if (number % 2) == 0: status = 'even' else: status = 'odd' return status #function tally odd , counts def tally_status_count(statuses): even_total = 0 odd_total = 0 status in statuses: if status == 'even': even_total = even_total + 1 else: odd_total = odd_total + 1 print print 'the total count of numbers is: ', even_total print 'the total count of odd numbers is: ', odd_total main()
Comments
Post a Comment