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
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()...
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...
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...
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]+" "+...
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...
convert this code to accept int value instead of float values using python. Make sure to...
convert this code to accept int value instead of float values using python. Make sure to follow the same code. do not change the steps and make sure to point to what code you replaced. make sure to have 2 files Method:----------------------- #define a python user difined method def get_float_val (prompt): is_num = False str_val = input (prompt) #prming read for our while #while is_num == False: (ignore this but it works) old school while not is_num: try: value =...
I have the below homework problem for a python programming class, and i really need some...
I have the below homework problem for a python programming class, and i really need some help!! First, take a set of 6 grades from a user and average them. Provide the average to the user. You need to check to make sure the grades are within the normal range. If the grade is less than 0 or more than 100, issue a warning to the user. You don't need to take the grade again, just let the user know....
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
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()...