san22bb
san22bb

Reputation: 23

Python While loop - nesting if statement in for loop to check numbers in array

Really struggling with this at the moment and need some help as to where I am going wrong with this all.

So currently just doing some practice work and running into an error. Seeing if numbers that a user enters into 'user input' within an array are bigger than a set integer. I want to iterate over the number in the array then have an if statement print out a message saying the number they entered is too high. I then want the loop to start back over. The error I am getting is that the loop prints the message but does not iterate back over the while loop and instead just exits. I do not know why this is happening and can't figure it out. Any help would be much appreciated, Thank you in advance.

user_lottery_numbers = []
while type(user_lottery_numbers) is not int:
    try:
        user_lottery_numbers = [int(x) for x in input("Please enter your numbers followed by the enter key:").strip(",").split(",")]
        for x in user_lottery_numbers:
            if (x) > 60:
                print ("The number %s you have entered is too high please stay within the range" % (x))
                continue
        if len(user_lottery_numbers) <6 or 0 in user_lottery_numbers:
            print ("Not enough numbers and or you have made one number zero by mistake")
            continue
        if len(user_lottery_numbers) >6:
            print ("Too many numbers")
            continue
        print (user_lottery_numbers)
        print ("Thank you for the correct format of numbers")
        break
    except ValueError:
        print ("You must only enter 6 integers followed by the enter key") 

Upvotes: 2

Views: 485

Answers (1)

user3980558
user3980558

Reputation:

First of all, A break statement terminates the loop containing it, therefore the break statement at the end of your while loop causes your loop to terminate right after the for loop ends. So, if you remove the break statement you will get the desired outcome:

Please enter your numbers followed by the enter key:5,4,61,77
The number 61 you have entered is too high please stay within the range
The number 77 you have entered is too high please stay within the range
Please enter your numbers followed by the enter key:

Secondly, looking at your code, I realize you thought the continue statement will cause the break statement to be skipped, however, the continue statement and the break statement are not in the same loop. You can remove the continue as it doesn't have any effect here.

If you want the while loop to finish if the user enters correct input, I would change the code to:

user_lottery_numbers = []
continue_running = True
bad_input = False
while continue_running:
    try:
        user_lottery_numbers = [int(x) for x in input("Please enter your 
        numbers followed by the enter key:").strip(",").split(",")]
        for x in user_lottery_numbers:
            if x > 60:
                print ("The number %s you have entered is too high please 
                        stay within the range" % (x))
                bad_input = True
        if bad_input:
            continue_running = True
        else:
            continue_running = False
    except ValueError:
        print ("You must only enter 6 integers followed by the enter key") 

Upvotes: 2

Related Questions