I don’t understand why my yield statement isn’t working or am not using it correctly if I use print it works isn’t yield supposed to return or do I need to return something else sorry I don’t like to bother people but I was confused as to why I need to return something . Thanks
def arithmetic_arranger(arithm_list, cond=None):
if len(arithm_list) > 5:
return 'Error: Too many problems.'
problem_list = []
for (i, problem) in enumerate(arithm_list):
problem_list.append(problem.split())
for i in problem_list:
if [j for j in i if len(j) > 4]:
return 'Error: Numbers cannot be more than four digits.'
while True:
try:
if i[1] == '+':
i.append('-----')
i.append(int(i[0]) + int(i[2]))
elif i[1] == '-':
i.append('-----')
i.append(int(i[0]) - int(i[2]))
else:
return "Error: Operator must be '+' or '-'."
except ValueError:
return 'Error: Problems must only contain digits.'
else:
break
if cond == False:
return
elif cond == None:
return
else:
pass
final_list = []
for (i, characters) in enumerate(problem_list):
problem = \
"""
{0:>10}
{1:>5}{2:>5}
{3:>10}
{4:>10}
""".format(characters[0],
characters[1], characters[2], characters[3],
characters[4]).split('\n')
final_list.append(problem)
for i in zip(*final_list):
yield ' '.join(i)
test program
ints_list = ["32 + 698", "2 - 3801", "45 + 43", "123 + 49"]
print(arithmetic_arranger(ints_list,True))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36.
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
Sorry sir it failed i don't know how to return so I gave up. Yield will not work as the return staments did not exit from the generator. However, this way it works oh well I just give coding
def arithmetic_arranger(arithm_list,cond=None):
if len(arithm_list) > 5: return "Error: Too many problems." #check len of list
problem_list = [] # empty list to append lists
for i, problem in enumerate(arithm_list):
problem_list.append(problem.split())
#appends list to create nested list. example [['23','+','5']['23','-','5']]
for i in problem_list:
if([j for j in i if len(j)>4]): # checks if len of digit sequence is > 4
return "Error: Numbers cannot be more than four digits."
while True:
try:
if(i[1]=="+"): # checks if the second value in i is either - +
i.append("-----")
i.append(int(i[0])+int(i[2])) # if conditon true,try to parse, sum, append ->i
elif(i[1]=="-"):
i.append("-----")
i.append(int(i[0])-int(i[2])) # if conditon true,try to parse,subt, append ->i
else:
return "Error: Operator must be '+' or '-'."
except ValueError: # handles parsing error
return "Error: Problems must only contain digits."
else:
break
if(cond==False):
return
elif(cond==None):
return
else:
pass
final_list = [] # final list will contain our formated strings
for i, characters in enumerate(problem_list):
# for loop iterates problem_list to create a list of formated strings using list indexes
problem = """
{0:>10}
{1:>5}{2:>5}
{3:>10}
{4:>10}
""".format(characters[0], characters[1], characters[2],characters[3],characters[4]).split('\n')
final_list.append(problem)
for i in zip(*final_list): #finally this allows us to zip the list and print by joining
print(" ".join(i))
# never found a way to just return the contents but it does print to console
# tried return[''.join i for i in zip(*final_list)]----> failed, gave up, cried