Home > Enterprise >  how can I loop this when the input entered y
how can I loop this when the input entered y

Time:12-18

def interact():
    while True: 
        try:
            num = int(input("Please input an integer: "))
            if (num % 2) == 0:
                print ("{0} is even".format(num))
            else:
                print("{0} is odd".format(num))
            num_two = int(input('Do you want to play again n/Y:'))
       
        except:
            if num_input == "y":
                continue
        finally:
            print("Goodbye")
        print(num_two)

CodePudding user response:

I don't fully understand what you are after, but maybe try something like this:

def interact():
    while True: 
        try:
            num = int(input("Please input an integer: "))
            if (num % 2) == 0:
                print ("{0} is even".format(num))
            else:
                print("{0} is odd".format(num))
        except:
            continue
        if input("Would you like to play again? Y/N: ").lower() == "y":
            continue
        else:
            print("goodbye")
            break

I believe this will give the effect your are after.

CodePudding user response:

The play-again prompt expects a string, not a number, to be entered. Don't try to treat it as a number. Restrict the try statement to testing the number input: if you get a ValueError, restart the loop immediately, instead of trying to test if it is even or odd.

When you get the response to play again, use break to exit the loop if the response isn't y.

Print "Good bye" after the loop exits.

def interact():
    while True:
        try:
            num = int(input("Please input an integer: "))
        except ValueError:
            continue  # Ask for a number again

        if num % 2 == 0:
            print("{0} is even".format(num))
        else:
            print("{0} is odd".format(num))

        response = input("Play again? (n/Y) ")
        if response != "y":
            break
    print("Good bye")

CodePudding user response:

A more compact approach:

def interact():
    want_continue = "Y"
    while want_continue == "Y":
        try:
            num = int(input("Please input an integer: "))
            print ("{0} is {1}".format(num, "even" if num % 2 == 0 else "odd"))
            want_continue = input('Do you want to play again (n/Y):').upper()
        except ValueError:
            print("Please enter a number")
    print("Goodbye")

CodePudding user response:

Use a break statment after your Goodbye:

def interact():
    while True: 
        try:
            num = int(input("Please input an integer: "))
            if (num % 2) == 0:
                print ("{0} is even".format(num))
            else:
                print("{0} is odd".format(num))
            num_two = int(input('Do you want to play again n/Y:'))
       
        except:
            if num_input == "y":
                continue
        finally:
            print("Goodbye")
            break
        print(num_two)

CodePudding user response:

I think the code should go like that:

def interact():
  try:
      while True:
          num = int(input("Please input an integer: "))
          if (num % 2) == 0:
              print ("{0} is even".format(num))
          else:
              print("{0} is odd".format(num))
          num_two = str(input('Do you want to play again n/Y:'))

          if num_two != "y":
              raise
  except:
      print("Goodbye")
  • Related