Europa14
Europa14

Reputation: 39

Keep original input as part of print statement

I have two inputs, a and b, and I am trying to perform an operation. My code is as follows:

a = int(input("Enter an integer A to be multiplied: "))
b = int(input("Enter an integer B to be multiplied: "))

while b > 1:
    b //= 2
    a *= 2
    sequence = a, b
    print(sequence)

If I enter 34 and 19, this is the output

Enter an integer A to be multiplied: 34
Enter an integer B to be multiplied: 19
(68, 9)
(136, 4)
(272, 2)
(544, 1)

However, I would like to include my original input 34 and 19 in the output. What do I need to change/add in order to make this happen?

Upvotes: 0

Views: 55

Answers (2)

amanb
amanb

Reputation: 5473

You can store the original values and then print them as tuples:

In [59]: a = int(input("Enter an integer A to be multiplied: "))
...: b = int(input("Enter an integer B to be multiplied: "))
...: orig_a = a
...: orig_b = b
...: while b > 1:
...:     b //= 2
...:     a *= 2
...:     sequence = a, b
...:     print((orig_a, orig_b),sequence)
#Output:
Enter an integer A to be multiplied: 6
Enter an integer B to be multiplied: 7
(6, 7) (12, 3)
(6, 7) (24, 1)

Or if you want all values in one tuple just change:

sequence = orig_a, orig_b,a, b
print(sequence)
#Output:
Enter an integer A to be multiplied: 34
Enter an integer B to be multiplied: 19
(34, 19, 68, 9)
(34, 19, 136, 4)
(34, 19, 272, 2)
(34, 19, 544, 1)

Upvotes: 1

xnx
xnx

Reputation: 25548

This would probably work, since when b falls below 2 the integer division will round down to zero:

a = int(input("Enter an integer A to be multiplied: "))
b = int(input("Enter an integer B to be multiplied: "))

while True:
    print(a, b)
    b //= 2
    a *= 2
    if b < 1:
        break

Upvotes: 0

Related Questions