Create a python program called code.py. Write a function named total_odds_evens() that takes an integer parameter called num. Your function must use a loop to repeat the following steps num number of times. It randomly selects an integer number x in the range [1 ... 100] (inclusive) an prints whether x is odd or even. It also maintains the sum of all odd x values and the sum of all even x values separately (i.e., saves the results in two different variables). Finally, it returns both sums.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
Create a python program called code.py.
Write a function named total_odds_evens() that takes an integer parameter called
num. Your function must use a loop to repeat the following steps num number of
times. It randomly selects an integer number x in the range [1 ... 100] (inclusive) and
prints whether x is odd or even. It also maintains the sum of all odd x values and the
sum of all even x values separately (i.e., saves the results in two different variables).
Finally, it returns both sums.
To test your function, ask the user for an integer value (input validation is not
necessary here) and use this value to call your total_odds_evens() function. Store the
returned values separately and print them to test the results.
Hint: to select a random integer, use the following.
import random and call random.randint(1,100) that returns an int from the
given range.
Transcribed Image Text:Create a python program called code.py. Write a function named total_odds_evens() that takes an integer parameter called num. Your function must use a loop to repeat the following steps num number of times. It randomly selects an integer number x in the range [1 ... 100] (inclusive) and prints whether x is odd or even. It also maintains the sum of all odd x values and the sum of all even x values separately (i.e., saves the results in two different variables). Finally, it returns both sums. To test your function, ask the user for an integer value (input validation is not necessary here) and use this value to call your total_odds_evens() function. Store the returned values separately and print them to test the results. Hint: to select a random integer, use the following. import random and call random.randint(1,100) that returns an int from the given range.
Expert Solution
Step 1

define a function

i=0
    sumeven=0
    sumodd=0
    while i<num:
        x=random.randint(1,101)
        if x%2==0:
            print(x," is even")
            sumeven=sumeven+x
        else:
            print(x," is odd")
            sumodd=sumodd+x
        i=i+1
    return sumeven,sumodd

 

call function and pass a number to it. 

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education