i wanna ask about how to call a function with two arguments in python. for example,
code below is an example i want to call color function.
def color(objec
The red
variable is defined inside of color
, so you can't use it outside of color
.
Instead, you have the variables tes
and tes_2
defined, so the call to color
should look like print color(tes, tes_2)
.
The small but fundamental errors in your second block:
object
and arg2
. object
is a reserved python word, both words are not so explanatory and (the real mistake) you never use arg2
in your function.return
value in the function.color(tes,red)
when it should be color(tes,tes_2)
.I have rewritten the block, take a look (with some modifications you can fine-tune later)
def color(color1,color2):
blue = '\033[1;34m'+color1+'\033[1;m'
red = '\033[1;31m'+color2+'\033[1;m'
return blue, red
tes = 'this must be blue'
tes_2 = 'i wanna this string into red!!'
for c in color(tes,tes_2):
print c
An alternate suggestion to achieve what you want would be:
def to_blue(color):
return '\033[1;34m'+color+'\033[1;m'
def to_red(color):
return '\033[1;31m'+color+'\033[1;m'
print to_blue('this is blue')
print to_red('now this is red')
EDIT: as requested (this is just the beginning ;oP . For example, you could use a dictionary of color names and color codes to call the function)
def to_color(string, color):
if color == "blue":
return '\033[1;34m'+color+'\033[1;m'
elif color == "red":
return '\033[1;31m'+color+'\033[1;m'
else:
return "Are you kidding?"
#should be 'raise some error etc etc.'
print to_color("this blue", "blue")
print to_color("this red", "red")
print to_color("his yellow", "yellow")
def color(object,arg2):
blue = '\033[1;34m'+object+'\033[1;m'
red = '\033[1;31m'+arg2+'\033[1;m'
return blue + red
tes = 'this must be blue'
tes_2 = 'i wanna this string into red!!'
print color(tes,tes_2)
I think you should visit Python2.7 Tutorial