Click here to Skip to main content
14,775,081 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to replace the highlighted block of conditional statements with user defined functions. I tried various methods but they were messing up the loop. I'll be glad if anyone helps me with this problem. Thank you.

What I have tried:

progress_count = 0
trailer_count = 0
exclude_count = 0
retriever_count = 0

while True:
    while True:
        try:
            #get inputs from user
            pass_credits = int(input("\nPlease enter your credits at pass: "))               
            defer_credits = int(input("Please enter your credits at defer: "))     
            fail_credits = int(input("Please enter your credits at fail: "))
        except ValueError:
            print("Integer Required \n")
            continue
        if pass_credits + defer_credits + fail_credits != 120:
            print("Total incorrect \n") #sum of inputs entered should be 120
            continue
        elif (pass_credits or defer_credits or fail_credits) not in range (0,121,20):
            print("Out of range")  #range of entered inputs should be 0,20,40,60...120
            continue
        else:
            #type, total and range of inputs entered are correct
            break 

    #conditions to assess student progess                                       
    if pass_credits==120:                                                   
        print("Progress\n")
        progress_count+=1
    elif pass_credits==100:                                            
        print("Progress (module trailer)\n")
        trailer_count+=1
    elif fail_credits>=80:                                                  
        print("Exclude\n")
        exclude_count+=1
    else:
        print("Do not progress - module retriever\n")
        retriever_count+=1

    #prompting for confirmation to enter more students' data on their progression or quit and view as horizontal histogram
    rerun_program = str(input("Would you like to enter another set of data? \nEnter 'y' for yes or 'q' to quit and view results: "))
    if rerun_program == 'y':
        #loops back to try block to get new data
        continue
    elif rerun_program == 'q':
        print("\nHorizontal Histogram")
        print(f"Progress {progress_count} : {progress_count*'*'}")
        print(f"Trailer {trailer_count} : {trailer_count*'*'}")
        print(f"Retriever {retriever_count} : {retriever_count*'*'}")
        print(f"Exclude {exclude_count} : {exclude_count*'*'}")
        print(f"{progress_count + trailer_count + retriever_count + exclude_count} outcomes in total.")
        #displays the entered data as a horizontal histogram and kills program
        break
Posted
Updated 2-Dec-20 21:45pm
v2

Why have you reposted this question? I already gave you the answer at While loop malfunctioning?[^]
   
Comments
Member 15009895 2-Dec-20 12:01pm
   
I'm sorry, I tried it but the prompting user input was looping unlimitedly. Is there any way to rectify that?
Richard MacCutchan 2-Dec-20 12:02pm
   
Then you should have replied to my suggested solution and explained the problem. There is no need to repost the entire question.
You might try something like
def validate_user_input():
  success = True
  try:
    #get inputs from user
    pass_credits = int(input("\nPlease enter your credits at pass: "))
    defer_credits = int(input("Please enter your credits at defer: "))
    fail_credits = int(input("Please enter your credits at fail: "))
  except ValueError:
    print("Integer Required \n")
    success = False
  else:
    if pass_credits + defer_credits + fail_credits != 120:
      print("Total incorrect \n") #sum of inputs entered should be 120
      success = False
    elif (pass_credits not in range(0,121,20)) or (defer_credits not in range(0,121,20)) or (fail_credits not in range (0,121,20)):
      print("Out of range")  #range of entered inputs should be 0,20,40,60...120
      success = False
  return success, pass_credits, defer_credits, fail_credits


success = False
while not success:
  success, pass_credits, defer_credits, fail_credits = validate_user_input()
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900