我将不胜感激有关此代码的帮助。我正在尝试获取打印结果的功能。该程序采用随机数并确定它们是偶数还是奇数。这样可行。然后应该计算有多少奇数和多少偶数。
我正在尝试将其构建到“tally_status_count”函数中,但无法使其正常工作。我最初将“even_total”和“odd_total”变量设置为全局变量,但后来尝试将它们移动到函数中。
我迷路了。任何帮助,将不胜感激。
问候
代码:
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()