MLJezus
MLJezus

Reputation: 79

How to return to the top of a loop if the else statement is reached

I hope the title is explanation enough. Basically, the user inputs a number and the number has to be between 1 and 147. If it is, the next required input is reached(num_guess). But we need to have the user try again if the number is not within the parameters. But I cannot figure out how to do this. Thanks in advance.

word_correct = False

def length_check(x):
    while (word_correct == False):
        if x >= 1 and x <= 147:
            return word_correct == True
            break
        else:
            print("Try another number")
            # print(input("Please enter a word length: "))  ## ignore me

word_length = input("Please enter a word length: ")
length_check(word_length)

num_guess = raw_input("Please enter an amount of guesses: ")

Upvotes: 0

Views: 3384

Answers (7)

bigbounty
bigbounty

Reputation: 17368

Basically you don't need a break after return.Once the compiler encounters return,it doesn't go through the next statements. What's the error in your code?

  • The implementation is not proper.
  • Use of break or return not in desired place

The code for your requirement can be implemented as follows -

def length_check(x):
     while(1):
           temp = input()
           if temp >= 1 and temp <= 147:
              return
           else:
              print 'Enter proper length'

This should solve your problem.

Upvotes: 0

Paul Rooney
Paul Rooney

Reputation: 21609

I would not try to put the loop inside word_check. Separate the responsibility for checking the length from the control flow & printing messages.

You can use an infinite loop, that is only broken out of when a valid value is input.

Also don't forget to convert to int. I am presuming you are using python 3? There is the possibility for the user to enter something that is not a valid integer (e.g. "abc"), so use an exception to handle that. If you are using python 2, swap input for raw_input.

def length_check(x):
    return x >= 1 and x <= 147

while True:
    try:
        word_length = int(input("Please enter a word length: "))
    except ValueError:
        print('please enter a valid number')
        continue

    if(length_check(word_length)):
        break

    print("Try another number")

Notice that this method involves only a single instance of input. This will make life easier later. For example, if you want to change the message and you don't have to remember to change it in two places or assign it to some string variable.

Upvotes: 1

maxwu
maxwu

Reputation: 432

A while True with break in block works like a do{} until() in other language, so as below program to satisfy the problem statement. Another point is to take care of recursion limit there is no hard limit in code to break out.

hint = "Please enter a word length: "
while True:
    x = int(raw_input(hint).strip())
    if 1 <= x <= 147:
        break
    else:
        hint = "Try another number"
num_guess = int(raw_input("Please enter an amount of guesses: ").strip())

Upvotes: 0

toonarmycaptain
toonarmycaptain

Reputation: 2331

This might work:

else:
    word_length = input("Try another number")
    length_check(word_length)

In the event of a wrong guess (the else) you ask the user for another input to word_length and then run the checking function length_check again. User can go through this as many times as they like (ie keep taking the else branch by entering a wrong value) until they get the number correct, and satisfy the if condition.

You might be able to get away without using break if you use this.

Upvotes: 0

R.A.Munna
R.A.Munna

Reputation: 1709

You may try this

word_correct = False

def length_check(x):
    while (word_correct == False):
        if x >= 1 and x <= 147:
            word_correct == True
            break
        else:
            x = input("Try another number")
            # print(input("Please enter a word length: "))  ## ignore me
    return
word_length = input("Please enter a word length: ")
length_check(word_length)

Upvotes: 0

Exprator
Exprator

Reputation: 27503

try this

word_correct = False

def length_check(x):
    while (word_correct == False):
        if x >= 1 and x <= 147:
            return word_correct == True
        else:
            print("Try another number")
            new_word= input("Please enter a word length: ")
            length_check(new_word)

word_length = input("Please enter a word length: ")
length_check(word_length)

num_guess = raw_input("Please enter an amount of guesses: ")

Upvotes: 1

JacobIRR
JacobIRR

Reputation: 8946

This strategy is much more concise, and utilizes recursion. No while loop is needed for what you are trying to do:

def main(warning=''):
  if warning:
    print(warning)
  word_length = input("Please enter a word length: ")
  if word_length >= 1 and word_length <= 147:
    num_guess = raw_input("Please enter an amount of guesses: ")
  else:
    main(warning="Try another number")
  #... now do something with num_guess

Upvotes: 0

Related Questions