Reputation: 23
I have been studying beginning game development with pygame and python, and I ran into a snag with defining functions that have arguments. The first one works, and I'm trying to make a more simple version with only one argument. It keeps saying c
is not defined when it clearly is. I don't understand why. Any suggestions or ideas on this? I'm also having
def fugu_tip(price, num_plates, tip):
total = price * num_plates
tip = total * (tip / 100.)
return tip
def Character(c):
a = input("Enter a number 1 - 100")
b = input("Enter A Number 1 - 100")
c = 0
c = a + b
return c
Character(c)
I appreciate all the help guys! This is the revised code for my project: '$' import pygame import random global Cash global PName global PHealth global PHunger global PJob global PEmployeed
def Character1():
Cash = 0
PName = raw_input("Please Enter Name: ")
PHealth = 100
PHunger = 100
PJob = ""
PEmployeed = False
print PName, Cash, PHealth, PHunger, PJob, PEmployeed
Character1()
'$'
Upvotes: 2
Views: 6759
Reputation: 3626
Well, the problem is c is not defined at current scope. In your case, c
is only visible from inside the function Character
but not from outside. So, the place from where you are calling the function has no idea what c
is. As soon as you define c
your code works just fine.
def Character(c):
a = input("Enter a number 1 - 100")
b = input("Enter A Number 1 - 100")
c = 0
c = a + b
return c
c = 0
Character(c)
Or maybe something like this (edit)
def Character(c):
a = input("Enter a number 1 - 100")
b = input("Enter A Number 1 - 100")
c = 0
c = a + b
return c
def call_character():
c = 0
Character(c)
call_character()
Upvotes: 1
Reputation: 27273
c
is defined inside your function — but not where you call Character
.
You seem to set c
to 0
in your function anyways — why have any parameter at all then?
Finally, you should not give your functions names that start with capital letters, as by convention that is reserved for classes.
edit:
def get_sum():
a = input("Enter a number 1 - 100")
b = input("Enter A Number 1 - 100")
c = a + b
return c
Upvotes: 1
Reputation: 2662
I'm going to rework some of the code you have rather than rewriting entirely. The thing that you are missing is scope. Inside, your function, c
is defined. However, outside of your function you are trying to pass in a variable called c
that is not defined.
Here's your code, fixed up.
#it's true that by convention, functions generally start with lowercase
# and Classes being with uppercase characters
def character(c = 0):
a = input("Enter a number 1 - 100")
b = input("Enter A Number 1 - 100")
return c * (a + b)
myValue = 3 #note that the variable that you pass in
# to your function does not have to have the same name as the parameter
character(myValue)
Note, I modified the behavior of the function so that i makes use of the parameter c. Now, c, the input parameter, is used to multiply the sum of the two user inputs. When I call the function the value of c
becomes 3 and so what ever the user enters is added then multiplied by 3.
Also, there is a difference between def character(c):
and def character(c=0):
. In the first case, a value must be passed into the function when calling it. In the second case, you can skip passing in a value to the function as we have the defined the function with a default argument value. So the second function could be called directly with:
character(3)
character()
But the first could only be called correctly with
character(3)
Upvotes: 2
Reputation: 965
C is being passed as a argument to the function Character
so it should be defined before you call then function.
You don't need to pass any argument to
Character
as it is not required for given behavior. Simply do
Character()
. And also remove C from your function definition.
def Character():
a = input("Enter a number 1 - 100")
b = input("Enter A Number 1 - 100")
c = 0
c = a + b
return c
Character()
Edited : based on user comment
Upvotes: 0
Reputation: 20336
The problem is where you call Character(c)
. That c
has not been defined. I can't suggest what you should do instead because I don't know what you are trying to do, but that is your problem. What you use instead depends on what argument you want to give to Character()
.
Upvotes: 0