samedi 19 septembre 2015

How to get python to go back to start of loop and not use random?

I'm a bit new to python and was giving myself a task, I wanted a number guessing game that gets you to guess four numbers and the program will keep telling you how many numbers you guess right till you guess the full list of numbers.

running = True
import random
def compareLists(a, b):
    return list(set(a) & set(b))
def start():
    rNums =  random.sample(range(10), 4)
    gNums = []

    print("Get ready to guess 4 numbers!")
    for i in range(0, 4):
        x = int(input("Guess: "))
        gNums.append(x)

    print("You guessed: ", gNums)
    comparison = len(compareLists(rNums, gNums))

    while gNums and rNums:
        if gNums != rNums:
            print("You guessed " + str(comparison) + " numbers right, keep guessing!")

        elif gNums == rNums:
            print("What amazing luck!")
while running:
    start()

The problem is that when I use a break a new list of 4 numbers is created, I want python to just go back to the start of the loop and not make a whole new list!




Aucun commentaire:

Enregistrer un commentaire