mercredi 5 octobre 2022

How to append randomly generated values to a list if you're running the function multiple times?

What I want this code (shown below) to do is print out 10 random float values and after the 10th term it prints out the actual random number and adds it to a list. If the user then decides to run the function again I want those two (or more) values added within the list (add_rand_num = []). How would I go about doing this?

import random
import time

def randnum():
    num = random.uniform(.0005, .5)
    add_rand_num = []
    for x in range(10):
        print(random.uniform(.0005, .5))
        time.sleep(.5)
    print("----------------------")
    print(num)
    add_rand_num.append(num)
    while True:
        redo = input("Would you like to generate another num? Enter Y for YES or N for NO: ")
        if redo == "Y":
            randnum()
            break
        elif redo == "N":
            print(f"The sum of the numbers you generated is {add_rand_num}! ")
            break
        else:
            print("Please enter either Y for YES or N for NO")

randnum()
    

Thank you in advance!




Aucun commentaire:

Enregistrer un commentaire