I am a student taking python programming. Can this problem be modified using the define main method, def main()?
import random
#function definition
#check for even and return 0 if even
def isEven(number):
if(number%2==0):
return 0
#return 1 if odd
else:
return 1
#count variables
even =0
odd = 0
c = 0
#loop iterates for 100 times
for i in range(100):
#generate random number
n = random.randint(0,1000)
#function call
val = isEven(n)
#check value in val and increment
if(val==0):
even = even + 1
else:
odd = odd + 1
c = c + 1
print (n, end = ' ')
#print new line
if(c == 10):
print()
c = 0
#print even and odd count
print("Out of 100 random numbers, ",odd," were odd, and ",even,"
were even.")
Modified code with main function
import random
#function definition
#check for even and return 0 if even
def isEven(number):
if(number%2==0):
return 0
#return 1 if odd
else:
return 1
# main function definition
def main():
#count variables
even =0
odd = 0
c = 0
#loop iterates for 100 times
for i in range(100):
#generate random number
n = random.randint(0,1000)
#function call
val = isEven(n)
#check value in val and increment
if(val==0):
even = even + 1
else:
odd = odd + 1
c = c + 1
print (n, end = ' ')
#print new line
if(c == 10):
print()
c = 0
#print even and odd count
print("Out of 100 random numbers, ",odd," were odd, and ",even,"
were even.")
# calling main() function
main()
Screen shot of the code
Screen shot of the output
Get Answers For Free
Most questions answered within 1 hours.