Diako Kadir
Diako Kadir

Reputation: 11

How can I make it loop properly?

It won't work when trying to loop so it can restart at the end when asked.

def inputPass(message):
  while True:
    try:
       userInput = int(input(message))
    except ValueError:
       print("Not an integer! Try again.")
       continue
    else:
       return userInput

def inputDefer(message):
  while True:
    try:
       userInput = int(input(message))
    except ValueError:
       print("Not an integer! Try again.")
       continue
    else:
       return userInput

def inputFail(message):
  while True:
    try:
       userInput = int(input(message))
    except ValueError:
       print("Not an integer! Try again.")
       continue
    else:
       return userInput

Pass = inputPass("Enter how many passes you got")
if((Pass) != 0 and (Pass != 20) and (Pass != 40) and (Pass != 60) and (Pass != 80) and (Pass != 100) and ( Pass != 120)):
        print("Out of range")
        exit()
Defer = inputDefer("Enter how many defers you got")
if ((Defer != 0) and (Defer != 20) and (Defer != 40) and (Defer != 60) and (Defer != 80) and (Defer != 100) and (Defer != 120)):
        print("out of range")
        exit()
Fail = inputFail("Enter how many fails you got")
if ((Fail != 0) and (Fail != 20) and (Fail != 40) and (Fail != 60) and (Fail != 80) and (Fail != 100) and (Fail != 120)):
        print("out of range")
        exit()

PDF = (Pass + Defer + Fail)

if PDF != 120:
    print("Total incorrect")
    exit()

if Pass == 120:
    print ("Progress")
elif Pass == 100:
    print("Progress (module trailer)")
elif Fail >= 80:
    print("Exclude")
else:
    print("Do not progress – module retriever")

I tried using a while loop and a function so I can recall back to it but it won't do so and I don't know why.

Upvotes: 1

Views: 51

Answers (1)

Chris Doyle
Chris Doyle

Reputation: 12209

Its kind of difficult to answer since your question is very unclear. You have a lot of repeated code which can be tided up. If the code below doesnt answer your question hopefully it puts you on the right track to solve your problem. If your still struggling please update the question with more clear details.

VAL_RANGES = [0, 20, 40, 60, 80, 100, 120]


def get_input(result: str) -> int:
    while True:
        try:
            val = int(input(f"Enter how many {result} you got? {VAL_RANGES}:"))
            if val in VAL_RANGES:
                return val
            print("out of range")
        except ValueError:
            print("Not an integer! Try again.")


while True:
    Pass = get_input("passes")
    Defer = get_input("defers")
    Fail = get_input("fails")
    PDF = Pass + Defer + Fail

    if Pass == 120:
        print("Progress")
    elif Pass == 100:
        print("Progress (module trailer)")
    elif Fail >= 80:
        print("Exclude")
    else:
        print("Do not progress – module retriever")

    if input("Finished? (Y/N):").upper() == "Y":
        break

OUTPUT

Enter how many passes you got? [0, 20, 40, 60, 80, 100, 120]:5
out of range
Enter how many passes you got? [0, 20, 40, 60, 80, 100, 120]:120
Enter how many defers you got? [0, 20, 40, 60, 80, 100, 120]:40
Enter how many fails you got? [0, 20, 40, 60, 80, 100, 120]:20
Progress
Finished? (Y/N):n
Enter how many passes you got? [0, 20, 40, 60, 80, 100, 120]:120
Enter how many defers you got? [0, 20, 40, 60, 80, 100, 120]:0
Enter how many fails you got? [0, 20, 40, 60, 80, 100, 120]:0
Progress
Finished? (Y/N):y

Process finished with exit code 0

Upvotes: 1

Related Questions