samedi 24 juillet 2021

How to increment a value in a loop

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.




Aucun commentaire:

Enregistrer un commentaire