Question

Please use Python to solve this problem: Write a loop that reads positive integers from standard...

Please use Python to solve this problem:

Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out, on a single line and separated by a single space, the sum of all the even integers read and the sum of all the odd integers read.

1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

# Initialize the variables oddSum and evenSum to 0.


oddSum = 0


evenSum = 0


# Start the while loop till the condition is True


# Prompt the user to enter the positive number till


# a negative number is read.


while(True):

  

n = int(input("Enter positive number: "))


# If the user input is less than 0, that is


# negative, break.


if(n<0):

  

  break


else:

  

  # If the number entered by user is positive and it

  

  # is divisible by 2 completely, it means the number

  

  # is even, so add the positive even numbers and

  

  # store the result in variable evenSum.

  

  if(n%2==0):

    

    evenSum += n

    

  else:

    

    # If the number entered by user is positive and it

    

    # is not divisible by 2 completely, it means the

    

    # number is odd so add the positive odd numbers

    

    # and store the result in variable oddSum.

    

    oddSum += n

    

    # Print the sum of even numbers.  

    

print("Sum of evens:",evenSum)


# Print the sum of odd numbers.


print("Sum of odds:",oddSum)


Add a comment
Know the answer?
Add Answer to:
Please use Python to solve this problem: Write a loop that reads positive integers from standard...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • C++ Write a loop that reads positive integers from standard input and that terminates when it...

    C++ Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out the sum of all the even integers read and the sum of all the odd integers read(The two sums are separated by a space). Declare any variables that are needed.

  • python 3 please, for these questions you do not need to write the whole code just...

    python 3 please, for these questions you do not need to write the whole code just the expression 1)Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, each on a separate line, The loop terminates when it reads an integer that is not positive. 2) Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop...

  • Python My ITlab: 1. Write some code that repeatedly reads a value into the variable n...

    Python My ITlab: 1. Write some code that repeatedly reads a value into the variable n until a number between 1 and 10 (inclusive) has been entered. 2. Write some code that repeatedly reads a value from standard input into the variable response until at last a Y or y or N or n has been entered. 3.Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, each on a separate...

  • 1. Write a for loop that prints the sum of all positive even integers less than...

    1. Write a for loop that prints the sum of all positive even integers less than 200. Assume that variables i and sum are already declared. 2. Write a while loop that prints the sum of all positive odd integers less than 100. Assume that variables i and sum are already declared. 3. Write a do-while loop that prints the sum of all positive multiples of 3 less than or equal to 150. Assume that variables i and sum are...

  • Write a C program that reads a list of positive integers from a file named "input.txt."...

    Write a C program that reads a list of positive integers from a file named "input.txt." and count number of odd integers and even integers and prints the results to another file called "results.txt". Please submit both the input and results files along with the c program.

  • Write a program in python using lists or tuples that asks for two positive integers n1...

    Write a program in python using lists or tuples that asks for two positive integers n1 and n2 and prints out all the numbers that are divisible by 7 but are not a multiple of 5 within the range [n1,n2] (both included). The numbers obtained should be printed in a comma-separated sequence on a single line.

  • In Python 3 - Write a program that reads a sequence of integer inputs from the...

    In Python 3 - Write a program that reads a sequence of integer inputs from the user. When the user is finished entering the integers, they will enter a 'q'. There is no need to check if the entry is a valid integer. All integers in the test data (input) will be between -255 and 255. The program should then print: The smallest and largest of the inputs. The number of even and odd inputs (0 should be considered even)...

  • python la ab X, X Lab 7 Design and implement a Python program that uses a...

    python la ab X, X Lab 7 Design and implement a Python program that uses a while loop and selection statements to print the sum of the even numbers and the sum of the odd numbers (all integers) read from the keyboard. The sequence of numbers ends with a negative number: So, all numbers considered for printing the two sums are greater than or equal to 0. The sequence may be empty. Here is a possible interaction. Enter an integer:...

  • Write a C program named space_to_line.c that features a while loop that continuously reads input from...

    Write a C program named space_to_line.c that features a while loop that continuously reads input from the user one character at a time and then prints that character out. The exception is that if the user inputs a space character (‘ ‘), then a newline character (‘\n’) should be printed instead. This will format the output such that every word the user inputs is on its own line. Other than changing the spaces to newlines, the output should exactly match...

  • python language Primes Write a program primes.py that reads a positive integer from standard input, and...

    python language Primes Write a program primes.py that reads a positive integer from standard input, and determines whether or not the number is prime A prime number is a positive integer that is greater than 1, an e can be divided exactly (without leaving a rerm or itself 1 $ python3 primes. py 2 one positive integer please: 21 3 21 is not prime! 5 S python3 primes. py 6 one positive integer please 7 23 is prime

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT