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 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
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
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
Lab 5 a) Write a program that reads in an unsigned integer K and sums the...
Lab 5 a) Write a program that reads in an unsigned integer K and sums the first K many integers that are divisible by 7. You should output the sum on a formatted manner b)Consider the following diamond it is an 11 by 11 diamond made with * signs. Write a program that takes as input positive odd integer K (greater than or equal to three and outputs a K by K diamond made with * signs * *** *...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
Input the available amount of Rice in kg. Write a program that reads a sequence of...
Input the available amount of Rice in kg. Write a program that reads a sequence of Rice donations in kg and add them to the initially available amount of Rice. The loop should stop once the accumulated amount of Rice exceed 50 kg. Then, the program should print the final amount of Rice and the number of considered donations.
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT