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:
When you fix problem 2, you end up with problem 3, which is...
Problem 3: malformed for-loop. This:
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:
the old value for status is thrown away.
This is probably the code you meant to write: