Click here to Skip to main content
15,884,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys, I'm new to programming. I have tried my assignment and when I enter an input which satisfies my user defined functions, total_check and range_check, those functions get executed and the rest of the programme gets executed too. I want my program to loop till a proper input is entered. I was successful by replacing the user defined functions with plain conditional statements with parameters and everything. But it's a must that use user defined functions for checking the range and total. I have highlighted the part which needs to be looked into. Also if you find anymore errors I'll be glad! Thank you in advance!

What I have tried:

Python
#progress tracker

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

def total_check(p,d,f):
     if (p+d+f) != 120:
        print("Incorrect total")

def range_check(p,d,f):
    if (p or d or f) not in range (0,121,20):
        print("Out of range")

while True:
    while True:
        try:
            #get inputs from user
            pass_credits = int(input("Please 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 TypeError:
            print("Integer required")
        if range_check(pass_credits,defer_credits,fail_credits):
            continue
        elif total_check(pass_credits,defer_credits,fail_credits):
            continue
        else:
            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 11:05am
v2

If fear you need to learn Python syntax and stop inventing.
This is wrong
Python
if (p or d or f) not in range (0,121,20):
    print("Out of range")

if you want to check if 3 variables are each in given range, you need to rewrite as
Python
if (p not in range (0,121,20)) or (d not in range (0,121,20)) or (f not in range (0,121,20)):
    print("Out of range")
 
Share this answer
 
Comments
CPallini 3-Dec-20 2:51am    
5.Nice spotted, that fixes also my code... :-)
Patrice T 3-Dec-20 3:01am    
thank you
Getting your program to run does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
Python
def Double(value):
   return value * value

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
You have two functions as follows:
Python
def total_check(p,d,f):
     if (p+d+f) != 120:
        print("Incorrect total")

def range_check(p,d,f):
    if (p or d or f) not in range (0,121,20):
        print("Out of range")

which you then try to use in if statements:
Python
if range_check(pass_credits,defer_credits,fail_credits):
    continue
elif total_check(pass_credits,defer_credits,fail_credits):
    continue

but neither function returns a true/false value.

You need to change them as follows:
Python
def total_check(p,d,f):
     if (p+d+f) != 120:
        print("Incorrect total")
        return False
     return True

def range_check(p,d,f):
    if (p or d or f) not in range (0,121,20):
        print("Out of range")
        return False
    return True
 
Share this answer
 

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