Safiya Hamid
Safiya Hamid

Reputation: 53

Python output stream

I'm trying to solve the Philaland coin problem (here is the link for better understanding of the question - https://discuss.codechef.com/t/tcs-codevita-problem/30018 ) in Python and am even getting the right output. However, I want to be able to give all the inputs together and get all the output together. What I am getting is - I input a number and get its output in the next line before I can give my second input. Here's my code.

import math
t=int(input())
for _ in range(t):
    n=int(input())
    coinsrequired=0
    if n>1:
        a=int(math.sqrt(n))+1
        print(a)

Here is the output I'm getting. 2 is the input for t. 10 and 5 are the inputs for n. 4 is the output for 10 and 3 is the output for 5.

2                                                                                                                             
10                                                                                                                            
4                                                                                                                             
5                                                                                                                             
3

What I want is the inputs (10 and 5) and outputs (4 and 3) to be together.

2
10
5
4
3

Thanks in advance. Python rookie here, a simple solution will be much appreciated.

Upvotes: 0

Views: 471

Answers (2)

Peaceful James
Peaceful James

Reputation: 2233

Save your inputs and outputs in separate lists and print them when your loop is finished:

import math

t=int(input())

inputs = []
outputs = []

for _ in range(t):
    n=int(input())
    inputs.append(n)
    coinsrequired=0
    if n>1:
        a=int(math.sqrt(n))+1
        outputs.append(a)

print("""
Here are the inputs:
""")
for i in inputs:
    print(i)

print("""
Here are the outputs:
""")
for o in outputs:
    print(o)

Upvotes: 0

Barmar
Barmar

Reputation: 781503

Put all the inputs in a list. Then loop through the list calculating the result for that input.

import math

t=int(input())
inputs = [int(input()) for _ in range(t)]
for n in inputs:
    coinsrequired=0
    if n>1:
        a=int(math.sqrt(n))+1
        print(a)

Upvotes: 1

Related Questions