Question

I am a student taking python programming. Can this problem be modified using the define main...

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.")

Homework Answers

Answer #1
  • The given program contain a function isEven()
  • The rest of the code can be placed inside main() function
  • def main(): is used to define main function
  • In order to execute main() function,it should be explicitly called
  • The last statement main() calls the main() function.
  • The statements def isEven(number): ,def main():, main() have the same indentation.First two are function definition and the third one is function calling.
  • Execution begins from this main() function calling statement

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
##4. What will the following program display? ##def main(): ## x = 1 ## y =...
##4. What will the following program display? ##def main(): ## x = 1 ## y = 3.4 ## print(x, y) ## first printing ## change_us(x, y) ## print(x, y) ##second printing ## ##def change_us(a, b): ## a = 0 ## b = 0 ## print(a, b) ## ##main() ## ##Yes, yes, main() displays ##1 3.4 ##0 0 ##1 3.4 ## The question is: why x and y are still the same while the second printing of (x,y)? ## It seems...
i need this code to print a number of stars depending on how much the user...
i need this code to print a number of stars depending on how much the user wants and after * it prints no stars. if the user enters a negative number it should print "error invalid number" this is my code so far: def stars(n,i): stars(n, 1) if n <= 0: return "No stars" if i <= n: print("* ", end="") stars(n, i + 1) else: print("no stars") stars(n - 1, 1) n = int(input("enter an integer")) def main(): stars()...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21,...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21, all players who are still standing win. But the players that are not standing have already lost. If the Dealer does not go over 21 but stands on say 19 points then all players having points greater than 19 win. All players having points less than 19 lose. All players having points equal to 19 tie. The program should stop asking to hit if...
I am working on exercise 5.30 from Introduction to Computing using python (Author: Perkovic). I was...
I am working on exercise 5.30 from Introduction to Computing using python (Author: Perkovic). I was looking at the solution and was able to understand what to do. However, when I implement the temp function as indicated, I keep getting this error "ValueError: the first two maketrans arguments must have equal length". However, it seems my two arguments are equal length, so I'm not sure what I am doing wrong! print('Exercise 5.30') def many(file): infile = open(file) content = infile.read()...
In python: I am trying to construct a list using enumerate and takewhile from a Fibonaci...
In python: I am trying to construct a list using enumerate and takewhile from a Fibonaci generator. So far the code I have is the following: def fibonacci(): (a, b) = (0, 1) while True: yield a (a, b) = (b, a + b) def createlist(n, fib): return [elem for (i, elem) in enumerate(takewhile(lambda x: x < n, fib)) if i < n] I only get half the list when I do: print(createlist(n, fibonacci())) Output: [0, 1, 1, 2, 3,...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
This is my code, python. I have to search through the roster list to find a...
This is my code, python. I have to search through the roster list to find a player using their number. it says list index out of range. it also says there is error in my main. def file_to_dictionary(rosterFile): myDictionary={}       with open(rosterFile,'r') as f: data=f.read().split('\n')       for line in data:    (num,first,last,position)=line.split() myDict=[first, last, position] myDictionary[num]=myDict print (myDictionary) return myDictionary file_to_dictionary((f"../data/playerRoster.txt"))    def find_by_number(number): player=None    second=[] foundplayer= False myDictionary=file_to_dictionary((f"../data/playerRoster.txt")) for p in myDictionary: fullplayer=p.split() second.append([fullplayer[0], (fullplayer[1]+" "+...
Javascript please show me how to do these two exercises and comment so I can understand...
Javascript please show me how to do these two exercises and comment so I can understand what's happening. Thank you. // Execise #1 /* Modify the function so that it returns the length of an array recursively, without using the .length property. Use recursion! Hint: when you index outside of an array, it returns undefined */ function getLength(array, i = 0) { } // To check if you've completed the challenge, uncomment these console.logs! // console.log(getLength([1])); // -> 1 //...
In Python def main(): c = 10 print("Welcome to Roderick's Chikkin and Gravy") e = False...
In Python def main(): c = 10 print("Welcome to Roderick's Chikkin and Gravy") e = False while not e: x = input("Would you like some chikkin?") if x == "Y" or x == "y": c = f(c) else: e = True if c == 0: e = True print("I hope you enjoyed your chikkin!") def f(c): if c > 0: print("Got you some chikkin! Enjoy") return c-1 else: print("No chikkin left. Sorry") return 0 main() For practice with lists, create...
Python problem: write a main function that ask user for 2 file name, open files and...
Python problem: write a main function that ask user for 2 file name, open files and read the 1st line of each and compare them using hamming function(below is the hamming function I wrote). Just assume the fist line of each file only contains 0s and 1s, such as 0100111101. The main function may have to do some extra work to remove newline characters or other whitespace from the text read from each file. This is hamming function that need...