Can anyone help me understand Python functions?

1    23 Feb 2018 21:58 by u/carlip

I'm having trouble with an assignment in an Intro to Programming class for Python.

I cannot grasp the concept of how I am supposed to shuffle around variables, into and out of functions. I keep getting "positional errors", then when I think I fixed it, my program doesn't work correctly, but at least no errors.

8 comments

0

I'm not familiar with Python nor that specific Python error so perhaps you should post some example. I don't understand what you mean by shuffling either. Are you new to programming in general?

0

i've never gotten too indepth into any particular language.

What i mean is that i have some variables that i have to do 2 things with, the requirement is that i use 2 separate functions and returns, but i am having trouble splitting the data then reassembling it correctly.

0

I might have to be creative here and not use python language but...

function one(arg)
     return arg + 1
function two(arg)
    return arg + 2
a = 99
b = 101
c = one(a)
d = two(b)
print(c,d)

I would expect 100 and 103 to be printed. Now your case won't be nearly as simple as that. a is still 99, b is still 101 and arg is local to each function. When you say splitting does that mean your data in a string somewhere and you need to get it out?

0
def calcAverage(score1, score2, score3, score4, score5):
    pass
def determineGrade(score):
    if score >=90:
        return 'A'
    elif score >= 80:
        return 'B'
    elif score >= 70:
        return 'C'
    elif score >=60:
        return 'D'
    else:
        return 'F'
def main():
    loopCount=0
    while loopCount !=5:
        grade=int(input('Enter Score: '))
        print(determineGrade(grade))
        loopCount+=1
main()

I have to get 5 inputs as "test scores", then assign a letter grade A-F as well as print the average score and letter grade. All of this has to be formatted into a chart, which i already have worked that out, i just dont get the back end stuff.

0

I think I see. You either need to store the individual scores in some structure--Python has a great many types but I think the basic one for this is just called a List--or just keep track of a sum and and a count from there an average (the mean) is simple.

0

What kind of chart?

Formatted command-line outputs, or some kind of text or graphic file?

0
def determineGrade(letGrade):
    score=letGrade
    if score >=90:
        return 'A'
    elif score >=80:
        return 'B'
    elif score >=70:
        return 'C'
    elif score >=60:
        return 'D'
    else:
        return 'F'
def avgGrade(grade1,grade2,grade3,grade4,grade5):
    avg=(grade1+grade2+grade3+grade4+grade5)/5
    return avg
def printOut(grade1,grade2,grade3,grade4,grade5):
    avg=avgGrade(grade1,grade2,grade3,grade4,grade5)
    print()
    print('Score\tGrade\tLetter Grade')
    print('----------------------------')
    print(' 1:\t',grade1,'\t  ',determineGrade(grade1),'\n'+\
          ' 2:\t',grade2,'\t  ',determineGrade(grade2),'\n'+\
          ' 3:\t',grade3,'\t  ',determineGrade(grade3),'\n'+\
          ' 4:\t',grade4,'\t  ',determineGrade(grade4),'\n'+\
          ' 5:\t',grade5,'\t  ',determineGrade(grade5))
    print('----------------------------')
    print('Average Score:',avgGrade(grade1,grade2,grade3,grade4,grade5),'\t',\
          determineGrade(avg))
def askGrade():
    grade1=float(input('Score 1: '))
    grade2=float(input('Score 2: '))
    grade3=float(input('Score 3: '))
    grade4=float(input('Score 4: '))
    grade5=float(input('Score 5: '))
    return grade1,grade2,grade3,grade4,grade5
def main():
    grade1,grade2,grade3,grade4,grade5=askGrade()
    printOut(grade1,grade2,grade3,grade4,grade5)
main()

OUTPUT

>>> 
====== RESTART fasdasd.py ======
Score 1: 84
Score 2: 93
Score 3: 97
Score 4: 88
Score 5: 79
Score   Grade   Letter Grade
 1:  84.0      B 
 2:  93.0      A 
 3:  97.0      A 
 4:  88.0      B 
 5:  79.0      C
Average Score: 88.2      B
>>> 

apparently i was passing the variables to the wrong places, all fucking day...

0

Read about "variable scope".

You can use the same variable names in different functions and objects, and most of the time they are not shared across those functions or objects.

In other words, a variable may not be available to a function the way you'd expect unless you learn the ins and outs.