0

I am writing code that will take an integer input from a user. If the integer is greater than 0 then the program will ask the user for a second integer input. If the second integer input is greater than 1 then the program will compute the first number raised to the power of the second number. I am unsure how to correctly implement a while true loop such that when a user enters a wrong input it will reprompt the user for that respective integer i.e. int 1 or 2. Really appreciate some help :)!

while True:
    first_num= int(input("Enter the first integer:"))
    if(first_num>0):
     while True:        
        sec_num= int(input("Enter the second integer:"))
        if(sec_num>1):
            print(first_num**sec_num)
        break     
5
  • Use two separate loops, or write a function that handles the loop and optionally sets additional constraints. See the duplicate for how to write a loop that properly validates, as well as wrap the whole in a function. Commented Mar 13, 2020 at 16:09
  • @MartijnPieters - OP uses two loops. The problem is that the break is in the wrong place. Commented Mar 13, 2020 at 16:15
  • @tdelaney The loops aren't separate, though, they are nested. Commented Mar 13, 2020 at 16:18
  • @MartijnPieters - that's okay if the intent is to keep repeating the process. Commented Mar 13, 2020 at 16:20
  • @tdelaney: so? this doesn't change how you should approach this. Commented Mar 13, 2020 at 20:58

2 Answers 2

1

You have the break in the wrong place. Move it under the last if and it will continue prompting for the second number without going back to the first.

while True:
    first_num= int(input("Enter the first integer:"))
    if(first_num>0):
     while True:        
        sec_num= int(input("Enter the second integer:"))
        if(sec_num>1):
            print(first_num**sec_num)
            break

If you want to end the program after one success, then don't nest the whiles

while True:
    first_num= int(input("Enter the first integer:"))
    if(first_num>0):
        break

while True:        
    sec_num= int(input("Enter the second integer:"))
    if(sec_num>1):
        print(first_num**sec_num)
        break
Sign up to request clarification or add additional context in comments.

Comments

1

Handle each number one at a time. Don't start trying to get the second number until you have a valid first number.

while True:
    first_num = int(input(...))
    if first_num > 0:
        break

while True:
    second_num = int(input(...))
    if second_num > 1:
        break

print(first_num ** second_num)

If you want to repeat the ** operation for multiple numbers, put everything inside a third loop.

while True:
    while True:
        first_num = int(input(...))
        if first_num > 0:
            break

    while True:
        second_num = int(input(...))
        if second_num > 1:
            break

    resp = input("Try again?")
    if not resp.lower().startswith("y"):
        break

2 Comments

It looks to me like OP wants to keep repeating the process. If so, nested loops are reasonable.
The logic for repeating the process should be kept separate from the logic of getting valid inputs.

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.