14 Replies - 779 Views - Last Post: 10 July 2012 - 08:57 PM Rate Topic: -----

#1 BayseVT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-July 12

Creating Function

Posted 06 July 2012 - 10:59 AM

I am supposed to take the quiz sum of four quiz grades, the average of four homework grades, and the sum of two test grades and add them all together. I am then supposed to output the gradeTotal and letter grade to a user. I haven't had any trouble with the previous modules, but the gradeTotal module is confusing me.

 def main():
    quizOne=input("What is the score on the first quiz? ")
    quizTwo=input("What is the score on the second quiz? ")
    quizThree=input("What is the score on the third quiz? ")
    quizFour=input("What is the score on the fourth quiz? ")
    total=addFourQuizzes(quizOne, quizTwo, quizThree, quizFour)
    print "The four quizzes add up to ",total

    homeworkOne=input("What is the score on the first homework? ")
    homeworkTwo=input("What is the score on the second homework? ")
    homeworkThree=input("What is the score on the third homework? ")
    homeworkFour=input("What is the score on the fourth homework? ")
    total=getHomeworkAvg(homeworkOne, homeworkTwo, homeworkThree, homeworkFour)
    print "The homework average is ",total

    testOne=input("What is the score on the first test? ")
    testTwo=input("What is the score on the second test? ")
    total=addTwoTests(testOne, testTwo)
    print "The sum of the two tests is ",total

    quizSum=quizSum
    homeworkAvg=homeworkAvg
    testSum=testSum
    total=gradeTotal(quizSum, homeworkAvg, testSum)
    
    
    
def addFourQuizzes(quiz1, quiz2, quiz3, quiz4):
    quizSum=quiz1+quiz2+quiz3+quiz4
    return quizSum

def getHomeworkAvg(homework1, homework2, homework3, homework4):
    homeworkAvg=(homework1+homework2+homework3+homework4)/4
    return homeworkAvg

def addTwoTests(test1, test2):
    testSum=test1+test2
    return testSum

def gradeTotal():
    gradeTotal=quizSum1+homeworkAvg1+testSum1
    return gradeTotal

main() 


I am getting the error message saying global name quizSum1 is not defined. Any help is appreciated, I just need pointed in the right direction.

Is This A Good Question/Topic? 0
  • +

Replies To: Creating Function

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,236
  • Joined: 21-June 11

Re: Creating Function

Posted 06 July 2012 - 11:10 AM

On line 41 you're using the variables quizSum1, homeworkAvg1 and testSum1. None of those variables appear anywhere else in your program, so they don't have a value and can't be used.

They should probably be parameters to the gradeTotal function.
Was This Post Helpful? 0
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Creating Function

Posted 06 July 2012 - 11:58 AM

damn sepp2k is fast. I thought this one would be all mine when I saw that there were 0 views and 0 replies... now it says there are 0 views and 1 reply!
Was This Post Helpful? 0
  • +
  • -

#4 RCR  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 33
  • Joined: 04-July 12

Re: Creating Function

Posted 06 July 2012 - 01:34 PM

View Postatraub, on 06 July 2012 - 11:58 AM, said:

damn sepp2k is fast. I thought this one would be all mine when I saw that there were 0 views and 0 replies... now it says there are 0 views and 1 reply!

Hey atraub! Finally a fellow python user! Lol, seems like there are not many people who know python over here!
Was This Post Helpful? 0
  • +
  • -

#5 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Creating Function

Posted 06 July 2012 - 01:42 PM

I think you'll find there a quite a few lurking about, especially in the "Python" forum.

We just can't all be as quick at replying ;)
Was This Post Helpful? 0
  • +
  • -

#6 RCR  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 33
  • Joined: 04-July 12

Re: Creating Function

Posted 06 July 2012 - 01:47 PM

View PostSimown, on 06 July 2012 - 01:42 PM, said:

I think you'll find there a quite a few lurking about, especially in the "Python" forum.

We just can't all be as quick at replying ;)

Lol. We should all have a message gathering and just talk about python :)
Was This Post Helpful? 0
  • +
  • -

#7 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Creating Function

Posted 06 July 2012 - 01:54 PM

Head up to the top of the forum for a sub-forum "Advanced Python Discussion" where we can discuss non-help topics, anything goes as long as there is discussion value, and it's Python of course! There are a few discussions there already :)
Was This Post Helpful? 0
  • +
  • -

#8 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Creating Function

Posted 07 July 2012 - 05:45 PM

View PostRCR, on 06 July 2012 - 04:34 PM, said:

Hey atraub! Finally a fellow python user! Lol, seems like there are not many people who know python over here!


There are plenty of great python guys here. Simown is fantastic, sepp2k has been really active here lately, and .... well, he has to grow on you, but he is knowledgeable! :) Also, I haven't seen him in a while, but Motoma is one of the best damn pythoneers I've ever met.
Was This Post Helpful? 0
  • +
  • -

#9 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Creating Function

Posted 10 July 2012 - 07:21 PM

I have been messing with your code for a couple of minutes and don't know what happened.
def main():
    q1 = float(input("What is the score on the first quiz? "))
    q2 = float(input("What is the score on the second quiz? "))
    q3 = float(input("What is the score on the third quiz? "))
    q4 = float(input("What is the score on the fourth quiz? "))
    total1 = add(q1, q2, q3, q4)
    
    h1 = float(input("What is the score on the first homework? "))
    h2 = float(input("What is the score on the second homework? "))
    h3 = float(input("What is the score on the third homework? "))
    h4 = float(input("What is the score on the fourth homework? "))
    total2 = HomeworkAvg(h1, h2, h3, h4)

    t1 = float(input("What is the score on the first test? "))
    t2 = float(input("What is the score on the second test? "))
    total3 = addT(t1, t2)

'''
This section I dont know what happened, I keep recieving an assignment error (maybe use 2 classes)
'''


def add(w, x, y, z):
    quizSum = str(w + x + y + z)
    print (quizSum)
    
def HomeworkAvg(w, x, y, z):
    homeworkAvg = w + x + y + z / 4
    print (homeworkAvg)

def addT(w, x):
    testSum = w + x
    print (testSum)

def gradeTotal(x, y, z):
    gradeTotal = (x + y + z)
    print (gradeTotal)

main()


Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Creating Function

Posted 10 July 2012 - 08:02 PM

Well, I recieve no errors that you refer to, but I do see this problem:
homeworkAvg = w + x + y + z / 4



Needs to be:
homeworkAvg = (w + x + y + z) / 4


Was This Post Helpful? 0
  • +
  • -

#11 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Creating Function

Posted 10 July 2012 - 08:14 PM

View PostDogstopper, on 10 July 2012 - 10:02 PM, said:

Well, I recieve no errors that you refer to, but I do see this problem:
homeworkAvg = w + x + y + z / 4



Needs to be:
homeworkAvg = (w + x + y + z) / 4



Thank you; however I was confused about how the grand total worked because you may have noticed I didn't even use it. The reason being I recieved to many assignment errors
Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Creating Function

Posted 10 July 2012 - 08:35 PM

I do not know how you got those assignment errors. I ran that code with no problem (other than the average being wrong):
>>> 
What is the score on the first quiz? 100
What is the score on the second quiz? 100
What is the score on the third quiz? 50
What is the score on the fourth quiz? 75
325.0
What is the score on the first homework? 100
What is the score on the second homework? 100
What is the score on the third homework? 50
What is the score on the fourth homework? 75
268.75
What is the score on the first test? 40
What is the score on the second test? 40
80.0


Was This Post Helpful? 0
  • +
  • -

#13 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Creating Function

Posted 10 July 2012 - 08:42 PM

View PostDogstopper, on 10 July 2012 - 10:35 PM, said:

I do not know how you got those assignment errors. I ran that code with no problem (other than the average being wrong):
>>> 
What is the score on the first quiz? 100
What is the score on the second quiz? 100
What is the score on the third quiz? 50
What is the score on the fourth quiz? 75
325.0
What is the score on the first homework? 100
What is the score on the second homework? 100
What is the score on the third homework? 50
What is the score on the fourth homework? 75
268.75
What is the score on the first test? 40
What is the score on the second test? 40
80.0



    quizSum=quizSum
    homeworkAvg=homeworkAvg
    testSum=testSum
    total=gradeTotal(quizSum, homeworkAvg, testSum)


When I pass the arguments to the function gradeTotal(w, x, y) . It says that the variables are being referenced before being assigned.

Was This Post Helpful? 0
  • +
  • -

#14 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Creating Function

Posted 10 July 2012 - 08:53 PM

? Where are you getting this from?
quizSum=quizSum
homeworkAvg=homeworkAvg
testSum=testSum



Think about it...how does that even make sense? How can something equal itself if it doesn't exist yet? The exact code you posted here works great (with the exception of the average problem).
Was This Post Helpful? 1
  • +
  • -

#15 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Creating Function

Posted 10 July 2012 - 08:57 PM

View PostDogstopper, on 10 July 2012 - 10:53 PM, said:

? Where are you getting this from?
quizSum=quizSum
homeworkAvg=homeworkAvg
testSum=testSum



Think about it...how does that even make sense? How can something equal itself if it doesn't exist yet? The exact code you posted here works great (with the exception of the average problem).


def gradeTotal(x, y, z):
    gradeTotal = (x + y + z)
    print (gradeTotal)


The point of that function is to add up all of the sums and get a grand total. However I am unsure of where to place the varaiables (so I left them out of my script). Would you be able to show me the right place
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1