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()
'$'
-
Oh and to the moderator, sorry for the non indented code, still trying to figure out how to work with this.Sammy West– Sammy West2016年02月27日 00:47:21 +00:00Commented Feb 27, 2016 at 0:47
-
1@cat I make that typo all the time, too.Barmar– Barmar2016年02月27日 01:04:40 +00:00Commented Feb 27, 2016 at 1:04
-
It's not clear to me what you mean by "I'm also having." Do you mean that's your code? Please also provide the exact error message and preferably the stack trace. Lastly, camel case method names is extremely uncommon in Python. I see all lower case with underscores most commonly, and you sometimes see Pascal case.jpmc26– jpmc262016年02月27日 01:06:31 +00:00Commented Feb 27, 2016 at 1:06
-
1lol cat, yeah didn't realize that until i posted :DSammy West– Sammy West2016年02月27日 01:20:46 +00:00Commented Feb 27, 2016 at 1:20
-
jpmc, the code at the bottom is mine, the code at the top is the example of the book that I'm trying to hack around with and understand the nuances.Sammy West– Sammy West2016年02月27日 01:22:04 +00:00Commented Feb 27, 2016 at 1:22
5 Answers 5
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)
4 Comments
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
6 Comments
global inside the function to manipulate module level variables inside a function, but that is usually a bad idea.global, I recommend just searching around for global. A Google search readily turns up some very basic examples. Your second question is unclear; I'm not sure what you're asking. You need to flush out what you mean and do some research. If after looking around for an hour or so, you can't find something on it, then post a question describing the feature you want. If it comes from another language, just searching for Python's "equivalent" is usually a good way to turn up results that will help.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()
3 Comments
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().
7 Comments
c = 0. You are not modifying c; you are redefining it. You can't modify an integer; it is immutable.c is not exactly just whitespace. You could use it before you said c = 0, and it would be the same value as that given to the function, but when you say c = 0, you are overwriting the old definition.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
3 Comments
TypeError: Character() takes exactly 1 argument (0 given)