2

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") 
san22bb
  • 23
  • 4
  • What do you mean by "want the loop to start back over"? By "the loop", do you mean the `while` loop or the `for` loop? – j1-lee May 15 '21 at 20:36
  • Should the exising values be stored? Suppose if I enter 2, 3, 70; should 2,3 be stored or will it be a fresh start? – Ashok Arora May 15 '21 at 20:36
  • 2
    No matter what you do, the `break` statement ends the `while` loop. Also, the loop condition will never be false, because that variable is *always* a list. – chepner May 15 '21 at 20:37
  • [How to continue in nested loops in Python](https://stackoverflow.com/q/14829640/3890632) – khelwood May 15 '21 at 20:46
  • @j1-lee The while loop, I want it to ask the user again to enter the numbers back in if any of the number they entered were bigger than 60. – san22bb May 15 '21 at 20:57
  • @AshokArora - No exit values should not be stored just asking them to re-enter. – san22bb May 15 '21 at 20:58
  • @chepner - So do you mean the variable the user_lottery_numbers as this does get triggered as an error if you were to enter a sting instead of an integer. I have other if statements inside this while loop that do get triggered with their continue and then it iterates back over the while loop. Let me add to my code so you can see the entire block. – san22bb May 15 '21 at 20:58
  • If `int(x)` raises a `ValueError`, the assignment to `user_lotter_numbers` never happens; it keeps its previous value. – chepner May 15 '21 at 21:00
  • @chepner - Yep thats actually correct what you said, I missed that, this is something I need to address. Thank you for having eagle eyes. P.S - how do I say that this issue is solved as I have my answer now. This is the first time I have ever posted on stack... – san22bb May 15 '21 at 21:09

1 Answers1

2

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") 
  • 1
    OMG - Thanks so much make so much sense, I have been trying to work this out for the last hour, thinking what have I done wrong. Now that you have said that its so obvious. Thanks !!!! Really appreciate it. – san22bb May 15 '21 at 21:04
  • @san22bb No worries, Happy to help :) –  May 15 '21 at 21:05