-1

this is my current code, however the the input doesn't change to an integer can someone help me pls

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

def validate_credits(input_credits):
    try:
        input_credits = int(input_credits)
    except:
        raise ValueError('integer required')
        
    if input_credits not in score:
        raise ValueError('out of range')
        
while True: 
    try: 
        mark1 = input('Enter your total PASS marks: ')
        validate_credits(mark1)
    
        mark2 = input('Enter your total DEFER marks: ')
        validate_credits(mark2)
    
        mark3 = input('Enter your total FAIL marks: ')
        validate_credits(mark3)
        
    except ValueError as e: 
           print(e)
ruki nish
  • 3
  • 2

1 Answers1

0

This is a scoping issue. Consider:

def the_function(_the_input):
    _the_input = int('2')

if __name__ == "__main__":
    the_input = '1'
    the_function(the_input)
    print(the_input)

What do you think the_input will be? '1' or 2?

Output is '1'

How about a list?

def the_function(_the_input):
    _the_input.append('2')

if __name__ == "__main__":
    the_input = ['1']
    the_function(the_input)
    print(the_input)

Output is ['1', '2']

At the risk of not explaining this correctly, I'd instead refer you to this thread on the topic

To get your input as an integer, you need to return the value:

def the_function(_the_input):
    # + other function logic...
    return int(_the_input)

if __name__ == "__main__":
    the_input = '1'
    the_input = the_function(the_input)
    print(the_input)
PeptideWitch
  • 2,239
  • 14
  • 30