# Creating Function

Page 1 of 1

## 14 Replies - 1158 Views - Last Post: 10 July 2012 - 08:57 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=284960&amp;s=4a51b2d865cba03e2322a96aadf1f6ca&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 BayseVT

Reputation: 0
• 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? ")
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? ")
print "The sum of the two tests is ",total

quizSum=quizSum
homeworkAvg=homeworkAvg
testSum=testSum

quizSum=quiz1+quiz2+quiz3+quiz4
return quizSum

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

testSum=test1+test2
return testSum

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

• D.I.C Lover

Reputation: 2307
• Posts: 3,568
• 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.

### #3 atraub

• Pythoneer

Reputation: 827
• Posts: 2,231
• 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!

### #4 RCR

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

## Re: Creating Function

Posted 06 July 2012 - 01:34 PM

atraub, 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!

### #5 Simown

• Blue Sprat

Reputation: 321
• 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

### #6 RCR

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

## Re: Creating Function

Posted 06 July 2012 - 01:47 PM

Simown, 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

### #7 Simown

• Blue Sprat

Reputation: 321
• 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

### #8 atraub

• Pythoneer

Reputation: 827
• Posts: 2,231
• Joined: 23-December 08

## Re: Creating Function

Posted 07 July 2012 - 05:45 PM

RCR, 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.

### #9 k3y

Reputation: 36
• 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? "))

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

quizSum = str(w + x + y + z)
print (quizSum)

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

testSum = w + x
print (testSum)

gradeTotal = (x + y + z)

main()

```

### #10 Dogstopper

Reputation: 2956
• Posts: 11,220
• 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

```

### #11 k3y

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

## Re: Creating Function

Posted 10 July 2012 - 08:14 PM

Dogstopper, 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

### #12 Dogstopper

Reputation: 2956
• Posts: 11,220
• 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

```

### #13 k3y

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

## Re: Creating Function

Posted 10 July 2012 - 08:42 PM

Dogstopper, 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

```

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

### #14 Dogstopper

Reputation: 2956
• Posts: 11,220
• 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).

### #15 k3y

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

## Re: Creating Function

Posted 10 July 2012 - 08:57 PM

Dogstopper, 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)