Reputation: 91
I'm not sure how to properly write the question. So, the idea I have is to print the original random values I get from a list. After the user inputs a 1
inside a while loop it creates one new random number from the list. Every time the user enters 1
as the input, it keeps creating a new random number and prints the previous random numbers together.
import random
numbers = [1, 2, 3, 4, 5]
random_number = [random.choice(numbers) for _ in range(2)]
print(random_number)
while True:
choice = int(input("Press [1] to continue [2] to exit: "))
if choice == 2:
break
elif choice == 1:
extra_number = [random.choice(numbers) for _ in range(1)]
for i in extra_number:
print("\nFirst Numbers:", random_number, "Extra Numbers:", i)
This loop will return a random number from the list. However, every time the user enters 1
it replaces the old one with a new one. I want to still return those old values and also print a new one every time the user enters 1
. So, I thought that maybe increment will work, but I don't know how to make it work. I also tried enumerate
and range(len())
and found it a bit confusing to understand. I would appreciate it if someone could help me or if there is an easier way of doing this.
Upvotes: 2
Views: 335
Reputation: 1022
Does this do what you want?
import random
numbers = [1, 2, 3, 4, 5]
random_number = [random.choice(numbers) for _ in range(2)]
extra_numbers = []
print(random_number)
while True:
choice = int(input("Press [1] to continue [2] to exit: "))
if choice == 2:
break
elif choice == 1:
extra_number = random.choice(numbers)
extra_numbers.append(extra_number)
print("\nFirst Numbers:", random_number, "Extra Numbers:", extra_numbers)
Upvotes: 2
Reputation: 436
I am not sure if I understood the question. Here I added the list old_numbers where you save the pervious values.
import random
numbers = [1, 2, 3, 4, 5]
old_numbers=[]
while True:
choice = int(input("Press [1] to continue [2] to exit: "))
if choice == 2:
break
elif choice == 1:
extra_number = random.choice(numbers)
old_numbers.append(extra_number)
print("\nFirst Numbers:", old_numbers, "Extra Numbers:", extra_number)
The result:
Press [1] to continue [2] to exit: 1
First Numbers: [3] Extra Numbers: 3
Press [1] to continue [2] to exit: 1
First Numbers: [3, 4] Extra Numbers: 4
Press [1] to continue [2] to exit: 1
First Numbers: [3, 4, 4] Extra Numbers: 4
Press [1] to continue [2] to exit: 1
First Numbers: [3, 4, 4, 1] Extra Numbers: 1
Press [1] to continue [2] to exit: 1
First Numbers: [3, 4, 4, 1, 2] Extra Numbers: 2
Press [1] to continue [2] to exit: 1
First Numbers: [3, 4, 4, 1, 2, 3] Extra Numbers: 3
Press [1] to continue [2] to exit: 1
First Numbers: [3, 4, 4, 1, 2, 3, 3] Extra Numbers: 3
Press [1] to continue [2] to exit: 1
First Numbers: [3, 4, 4, 1, 2, 3, 3, 3] Extra Numbers: 3
Press [1] to continue [2] to exit: 1
First Numbers: [3, 4, 4, 1, 2, 3, 3, 3, 4] Extra Numbers: 4
Press [1] to continue [2] to exit: 1
First Numbers: [3, 4, 4, 1, 2, 3, 3, 3, 4, 4] Extra Numbers: 4
Press [1] to continue [2] to exit: 2
Upvotes: 2