pythonprintingsumintegernumbers

Printing pair of numbers using for loop - Python


I am a beginner in python and I need some help with a case: I need to print pairs of numbers which are input from a for loop - for example

count_of_numbers = int(input())

for numbers in range(2*count_of_numbers):
    number = int(input())

Let's say we enter 3 2 1 4 5 0 4, I need to print the sum of the paired numbers - 3 + 2, 1 + 4 etc. Could anybody brighten me up with an idea on how exactly this is done?


Solution

  • I tried to keep much of your code:

    count_of_numbers = int(input("Please enter the number of pairs: "))
      
    for i in range(count_of_numbers):
        number1 = int(input("Number 1 = "))
        number2 = int(input("Number 2 = "))
        print ("Sum of " + str(number1) + " and " + str(number2) + " = " + \ 
                str(number1 + number2))
    

    I made a few changes:

    1. since the loop requests 2 inputs instead of one, the loop only runs up to count_of_numbers. I could have used "+ 1", but preferred to start at zero, as the variable i (formerly numbers) isn't used.
    2. In order to guide the user running this program, I've added some text to the input() calls