jbhanot
jbhanot

Reputation: 21

Input in if else not looping back?

I'm creating a program that gets the prime factors of a number, as well as checks if it is prime or not, based on the user's command. I'm running into an issue where the if-else statement does not loop back, meaning that after the output is given, the user is prompted to give another command. After the user gives the command, the program ends.

  command = input("Enter a command (factor, isprime, end): ")
  command = command.lower()

  if command != "factor" and command != "isprime" and command != "end":
    print("Command", command,"not recognized. Try again!")
    command = input("Enter a command (factor, isprime, end): ")
    command = command.lower()

  elif command == "factor":
    num =  int(input("Enter an integer > 1: "))
    primeFactorsOf(num)
    print("")
    command = input("Enter a command (factor, isprime, end): ")
    command = command.lower()

  elif command == "isprime":
    num =  int(input("Enter an integer > 1: "))
    findPrime(num)
    print("")
    command = input("Enter a command (factor, isprime, end): ")
    command = command.lower()

  elif command == "end":
    print("Thanks for using our service! Goodbye.")

Any idea on how I can fix this?

Upvotes: 2

Views: 54

Answers (4)

ppwater
ppwater

Reputation: 2277

You can do this by using while True. and make an else: since the user might give an invalid input. (By the way use f strings). By the way, You added some extra unneeded comparisons so I fixed it:

while True: # this loops forever.
    command = input("Enter a command (factor, isprime, end): ")
    command = command.lower()

    if command == "factor":
        num = int(input("Enter an integer > 1: "))
        primeFactorsOf(num)
        print("")

    elif command == "isprime":
        num =  int(input("Enter an integer > 1: "))
        findPrime(num)
        print("")

    elif command == "end":
        print("Thanks for using our service! Goodbye.")
        break # Explicitly "break" out of the loop. if you don't it'll keep looping.

    else:
        print(f"Command {command} not recognized. Try again!")

Upvotes: 0

RufusVS
RufusVS

Reputation: 4127

You added some extra unneeded comparisons. The following accomplishes the same logic, and would actually make adding additional commands easier:

while True:
    command = input("Enter a command (factor, isprime, end): ")
    command = command.lower()

    if command == "factor":
        num = int(input("Enter an integer > 1: "))
        primeFactorsOf(num)
        print("")

    elif command == "isprime":
        num =  int(input("Enter an integer > 1: "))
        findPrime(num)
        print("")

    elif command == "end":
        print("Thanks for using our service! Goodbye.")
        break # Explicitly "break" out of the loop.

    else:
        print("Command", command,"not recognized. Try again!")

Upvotes: 1

Dav_Did
Dav_Did

Reputation: 313

Oh, right now your program don't loop back to the begining after you asked for another command. Using a while loop can easily solve the problem.

while True:
    command = input("Enter a command (factor, isprime, end): ")
    command = command.lower()

    if command != "factor" and command != "isprime" and command != "end":
        print("Command", command,"not recognized. Try again!")

    elif command == "factor":
        num =  int(input("Enter an integer > 1: "))
        # primeFactorsOf(num)
        print("")


    elif command == "isprime":
        num =  int(input("Enter an integer > 1: "))
        # findPrime(num)
        print("")

    elif command == "end":
        print("Thanks for using our service! Goodbye.")
        break

Upvotes: 0

Bill Lynch
Bill Lynch

Reputation: 81926

Welcome to the world of loops!

while True:
  command = input("Enter a command (factor, isprime, end): ")
  command = command.lower()

  if command != "factor" and command != "isprime" and command != "end":
    print("Command", command,"not recognized. Try again!")

  elif command == "factor":
    num = int(input("Enter an integer > 1: "))
    primeFactorsOf(num)
    print("")

  elif command == "isprime":
    num =  int(input("Enter an integer > 1: "))
    findPrime(num)
    print("")

  elif command == "end":
    print("Thanks for using our service! Goodbye.")
    break # Explicitly "break" out of the loop.

Upvotes: 0

Related Questions