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") 
9
  • 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? Commented May 15, 2021 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? Commented May 15, 2021 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. Commented May 15, 2021 at 20:37
  • How to continue in nested loops in Python Commented May 15, 2021 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. Commented May 15, 2021 at 20:57

1 Answer 1

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") 
Sign up to request clarification or add additional context in comments.

2 Comments

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 No worries, Happy to help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.