How To Call A Function With Two Arguments In Python
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(object): return '\033[1;
Solution 1:
The small but fundamental errors in your second block:
- Your arguments are
object
andarg2
.object
is a reserved python word, both words are not so explanatory and (the real mistake) you never usearg2
in your function. - You don't used any
return
value in the function. - When you call the function, you use
color(tes,red)
when it should becolor(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, redtes='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:
defto_blue(color):
return'\033[1;34m'+color+'\033[1;m'defto_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)
defto_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")
Solution 2:
defcolor(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
Solution 3:
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)
.
Post a Comment for "How To Call A Function With Two Arguments In Python"