A question about defining functions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

A question about defining functions

from time import sleep as slp def print_with_slp(word): print(word + "1") slp(1) print(word + "2") slp(1) print(word + "3") slp(1) print(word + "4") slp(1) print(word + "5") spider1 = " |" spider2 = " |" spider3 = " _|" spider4 = '///\(o_o)/111' spider5 = "||| ` ' |||" print("Welcome to the drawer 0.6!") slp(2) print("Please tell me what would you like me to draw.") slp(2) while True: x = input("What do you want me to draw? =").lower() if x == "a spider": print_with_slp(spider) else: break slp(2) as you can see I defined a function. it would take the word I put in and will write it as print(word1), print(word2) etc. so I wrote spider in there so it will print(spider1) and print(spider2) etc so there will be a spider figure drawn. but it says spider is not defined?

23rd Feb 2017, 5:37 PM
Korhan Özdemir
Korhan Özdemir - avatar
10 Answers
+ 3
# use a list and a loop: from time import sleep as slp def print_with_slp(word): for i in range(len(word)): slp(1) print(word[i]) spider = [ " |", " |" , " _|" , '///\(o_o)/111', "||| ` ' |||" ] print("Welcome to the drawer 0.6!") slp(2) print("Please tell me what would you like me to draw.") slp(2) while True: x = input("What do you want me to draw? =").lower() if x == "a spider": print_with_slp(spider) else: break slp(2)
24th Feb 2017, 11:03 PM
visph
visph - avatar
+ 1
I could be wrong as I have not touched python in about 5 years.
23rd Feb 2017, 4:52 PM
Liam Keegan
Liam Keegan - avatar
+ 1
Looks you need to print spder1 -5 and you are trying to print a string called spider. Maybe you cannot add to strings together in that way (print(word + "1")), I think the program is trying to print spider + 1 as in numerical values.
23rd Feb 2017, 4:55 PM
Liam Keegan
Liam Keegan - avatar
+ 1
from time import sleep as slp # if you are determined to stick with your current format # you can use this to literally evaluate the code def print_with_slp(word): print(eval(word + "1")) slp(1) print(eval(word + "2")) slp(1) print(eval(word + "3")) slp(1) print(eval(word + "4")) slp(1) print(eval(word + "5")) spider1 = " |" spider2 = " |" spider3 = " _|" spider4 = '///\(o_o)/111' spider5 = "||| ` ' |||" print("Welcome to the drawer 0.6!") slp(2) print("Please tell me what would you like me to draw.") slp(2) while True: x = input("What do you want me to draw? =").lower() if x == "a spider": print_with_slp("spider") else: break slp(2) # However here is an alternative option, # pass any drawing in as a list. It may # save you a lot of headaches. # By the way , Great idea and Picture ! def print_with_slp(word): for item in word: print(item) slp(1) spider =[ " |"," |"," _|",'///\(o_o)/111',"||| ` ' |||"] print("Welcome to the drawer 0.6!") slp(1) print("Please tell me what would you like me to draw.") slp(1) while True: x = input("What do you want me to draw? =").lower() if x == "a spider": print_with_slp(spider) else: break slp(1)
23rd Feb 2017, 8:00 PM
richard
0
no it doesn't work like that either. Says "spider is not defined."
23rd Feb 2017, 5:38 PM
Korhan Özdemir
Korhan Özdemir - avatar
0
the version before this one was exactly that. but it is long and not pratical. My question still remains unanswered.
23rd Feb 2017, 5:46 PM
Korhan Özdemir
Korhan Özdemir - avatar
0
you just said the same thing... I said it is not what I would like to do. I wan it to be competible with every thing I want to draw afterwards
23rd Feb 2017, 5:55 PM
Korhan Özdemir
Korhan Özdemir - avatar
0
what is sleep?
23rd Feb 2017, 7:16 PM
Mezo Drogba
Mezo Drogba - avatar
0
it makes it wait for how many seconds you put in before executing the code after it mezo
23rd Feb 2017, 7:18 PM
Korhan Özdemir
Korhan Özdemir - avatar
0
making a list is a perfect idea richard thanks a lot
23rd Feb 2017, 8:06 PM
Korhan Özdemir
Korhan Özdemir - avatar