2

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() 

'$'

asked Feb 27, 2016 at 0:46
6
  • Oh and to the moderator, sorry for the non indented code, still trying to figure out how to work with this. Commented Feb 27, 2016 at 0:47
  • 1
    @cat I make that typo all the time, too. Commented 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. Commented Feb 27, 2016 at 1:06
  • 1
    lol cat, yeah didn't realize that until i posted :D Commented 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. Commented Feb 27, 2016 at 1:22

5 Answers 5

2

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)
answered Feb 27, 2016 at 1:01
Sign up to request clarification or add additional context in comments.

4 Comments

ohhh! So you have to set the value of c in the argument part of the function instead of using it in the nested part. So i think i understand now. it would be Character(c = 0, Name = "Sammy West", Str = 0) and then from there, the function would silently or verbosely depending on how you design it will tell you what its doing with that data thats being sent to those place holders?
ok and c would still return a + b then, which i just pretty much want to add together. let me test it out. thanks!
If you want to just return a+b, then get rid of c altogether. character(): ...return a+b.
I see now!!! if you use c = 0 its basically something that requires a number or string in order to be ran. if theres no data going through c, then the function wont work because it needs that argument. and sense c would be a place holder for the answer, then its not c that needs to be the data center. c just returns it. it would need to be a = input(), b = input() in character arguments in order for it to be able to understand what I'm doing. I think i just had the logic of it backwards lol
1

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
answered Feb 27, 2016 at 0:47

6 Comments

All due respect, that's more of a political statement then a hard rule. I'm trying to have parameters to see if i can use those to manipulate objects from out side of the function.
You can use parantheses, just put nothing in it (see my edit)! The latter is indeed a "political" statement, if you want to call it that (I'd call it a "convention").
@KyleHuffman We can answer that directly for you: no, you can't manipulate function scope variables outside them. (You get this error trying.) For that, you need a module level variable; you can use global inside the function to manipulate module level variables inside a function, but that is usually a bad idea.
I've tried that before but kept getting an error. jpmc, can you please show me a example of that so i can understand how to work with it? also a side question, can you nest expressions in variables? at least about the global variables
@KyleHuffman Those are both separate questions that your original one. For examples of using 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.
|
1

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()
answered Feb 27, 2016 at 1:05

3 Comments

ok i think im starting to understand this. I appreciate the help!
ok so the way you have it, c would have to be a global in order for it to be able to have any affect on something. i don't think that's what i'm looking for on the answer side.
Actually, c does not have to be a global. The code will work when c is in the same scope of the function call or higher. In my case, c is global because you are calling the function from a global scope. Check the new example.
0

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().

answered Feb 27, 2016 at 0:48

7 Comments

Well I'm trying to learn how to manipulate a object inside a function from using an out side source in the long term. In the short term, trying to create objects that i can manipulate from the inside.
In your function, you say c = 0. You are not modifying c; you are redefining it. You can't modify an integer; it is immutable.
It's like drawing a line through a definition in a dictionary, and writing in a new one. You aren't modifying what that definition means; you are creating a new definition.
ohh so the c in the Character(c) is just white space and confusing to the compiler and is pretty much null in the eyes of the compiler? if so, then how would i be able to manipulate something that's inside the scope of a function?
The 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.
|
0

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

answered Feb 27, 2016 at 0:53

3 Comments

He's gonna need more explanation than that. Just calling it with no argument will give TypeError: Character() takes exactly 1 argument (0 given)
ok I think I'm understanding the Character(c) issue, but im curious. is there a way to change c = 0 from out side of the function? or would this be a new question or thread?
C is defined inside function so it becomes local variable accessible only in function. If you want to change it then define it as a global variable. Read more about global variable in python.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.