Question

In: Computer Science

Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs...

Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs and prints the number of even and odd inputs in the sequence. please explain. Thanks

Solutions

Expert Solution

We have to read unknown number of inputs, so we can read the input sequence as a string.

We can use split() function to split the string input into individual inputs, but the type of each value will still be string.

Use int() function to convert each value to integer and implement the program as follows:

  1. read sequence of integers as a single string
  2. split sequence of integers into a list of string values using split()
  3. initialize odd count to 0
  4. initialize even count to 0
  5. for each value in values list, do the following,
  6. convert each value into integer using int()
  7. check if the integer value is even or odd using modulus operator (%)
  8. increment even count if the integer value is even
  9. increment odd count if the integer value is odd
  10. print even count and odd count

Program:

Note: Please double-check the indentation using the screenshot provided as a reference.


sequence = input("Enter a sequence of integers: ")  # read sequence of integers as a single string

values = sequence.split()                           # split sequence of integers into a list of string values using split()

num_odd = 0                                         # initialize odd count to 0
num_even = 0                                        # initialize even count to 0

for value in values:                                # for each value in values list
    int_value = int(value)                          # convert each value into integer using int()
    if int_value%2 == 0:                            # check if the integer value is even or odd using modulus operator (%) 
        num_even += 1                               # increment even count if the integer value is even
    else:
        num_odd += 1                                # increment odd count if the integer value is odd
        
print("Number of even inputs : ", num_even)         # print even count
print("Number of odd inputs : ", num_odd)           # print odd count
    

Screenshot:

Output1:

Output2:

Please don't forget to give a Thumbs Up.


Related Solutions

C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
Write a Python program that reads an integer and prints how many digits the number has,...
Write a Python program that reads an integer and prints how many digits the number has, by checking whether the number is ≥10,≥100,≥1000, and so on (up to 1,000,000). Your program should also identify if a number is negative.
Write a C program that reads an integer value. Assume it is the number of a...
Write a C program that reads an integer value. Assume it is the number of a month of the year; print out the name of that month (Hint: months need to be captured in an array).
1. Write a program that read a sequence of positive integer inputs and print the sum...
1. Write a program that read a sequence of positive integer inputs and print the sum and average of the inputs. Assume that the user always enters a positive number (integer) as an input value or an empty space as a sentinel value. Please use the below to test your code [45 points]: Enter an int value or empty string to end: 98 Enter an int value or empty string to end: 78 Enter an int value or empty string...
write a python program that inputs 10 integer values from the keyboard and then displays their...
write a python program that inputs 10 integer values from the keyboard and then displays their sum. use for loop
PYTHON WHILE Write a program that prompts for and reads the number ? of spheres to...
PYTHON WHILE Write a program that prompts for and reads the number ? of spheres to be processed. If ?≤0 your program must display an error message and terminate; otherwise it does the following for ? times: Write a program that prompts for and reads the number ?n of spheres to be processed. If ?≤0n≤0 your program must display an error message and terminate; otherwise it does the following for ?n times: Prompts for and reads the volume of a...
PYTHON: Turtle Command Interpreter You will write a program that reads a sequence of codes, converts...
PYTHON: Turtle Command Interpreter You will write a program that reads a sequence of codes, converts them into Turtle commands, and then executes them. The codes are in a Python list, passed as an argument to your function. turtleRunner(t, x) t is a Turtle x is a list of Turtle commands Code List (f, d)    Move forward by d pixels u Lift pen up d Put pen down (l, d) Turn left by d degrees (r, d) Turn right...
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
Write a program that reads an integer, a list of words, and a character.
13.14 LAB: Contains the characterWrite a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character.Ex: If the input is:4 hello zoo sleep drizzle zthen the output is:zoo drizzleIn c++ 
Program must be in Python Write a program in Python whose inputs are three integers, and...
Program must be in Python Write a program in Python whose inputs are three integers, and whose output is the smallest of the three values. Input is 7 15 3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT