0

I need this to rerun the initial inpput question if it gets a value that is unacceptable.

     try:
    hours = float(input("Please enter your hours worked: "))
    if hours < 1 :
        print("Please enter a value greater than 0 ")
        #continue isn't working, figure out how to loop back to top instead
                    #temporary placeholder fix  
        hours= int(input("Please enter your hours worked: "))
#if a non-numerical value is entered
except:
    print("Please enter a numerical value greater than 0")
       #temporary placeholder fix  
            hours= int(input("Please enter your hours worked: "))

I tried usisng a continue statement, but it doesn't work with this code. How can I get the code to ask the user for input again if the input fails the if statement or triggers the except statement? I'm really new at this.

1
  • Please format your code correctly. Indentation is important in Python. Commented Jul 18, 2024 at 1:52

1 Answer 1

-1

can you try it this way. While loop will continue until its greater than 0

try:
    hours = float(input("Please enter your hours worked: "))
    while hours< 1 :
        print("Please enter a value greater than 0 ")
        #continue isn't working, figure out how to loop back to top instead
        #temporary placeholder fix  
        hours= int(input("Please enter your hours worked: "))
        #if a non-numerical value is entered
except:
    print("error occured")
    
Sign up to request clarification or add additional context in comments.

1 Comment

This will stop as soon as the user inputs a non-integer which moots the point of the try-except.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.