Zaid Fanek
Zaid Fanek

Reputation: 23

Print previous values

I am looking to print values that were obtained through a previous loop:

for example:

x=10
while i < num_of_guesses:
   y = int(input("enter y: ")
   print(x*y)
   i += 1

My goal is to print every value obtained again, so the output would look something like this

enter y: 1
10
enter y: 2
10
20
enter y: 1.5
10
20
15

The problem I'm having is figuring out a way to print the 10 and 20 (in this example) again. Any solution?

Upvotes: 0

Views: 404

Answers (2)

Ezequiel Celona
Ezequiel Celona

Reputation: 31

You can store the users inputs and append to a list, somethig like this:

x= 10
i = 0
values = []
while i < 10:
   y = int(input("enter y: "))
   values.append(y*x)
   print('\n'.join([str(v) for v in values]))
   i += 1

Output:

enter y: 1
10
enter y: 20
10
200
enter y: 2
10
200
20

Upvotes: 1

QWERTYL
QWERTYL

Reputation: 1373

Try this:

arr = []

while (i < num_of_guesses):
    y = int(input("enter y: "))
    arr.append(x*y)
    for m in arr:
        print(m)
    i += 1

Upvotes: 0

Related Questions