0

Loop through code with different output

I have a python code which runs through 4 different ranges and provide 4 different output, how do I optimize the code to provide 4 different output (not related) looping through same set of code? E.g. tried code below for workbook in workbooks: wb = load_workbook (os.path.join(path, workbook)) perf_sheet = wb.active rowmax = perf_sheet.max_row y = 22 z = 23 for rows in range (2, rowmax + 1): for col in range(y, z): char = get_column_letter(col) x = (perf_sheet.cell(int(rows), 17)).value if x is not None: if z < 24: (perf_sheet(char + str(rows)]) = len((perf_sheet.cell(int(rows), 17)).value) elif z < 25: (perf_sheet(char + str(rows)]) = x[:1].isnumeric() if z < 26: (perf_sheet [char + str(rows)]) = x[-1:].isnumeric() if z >= 27: break else: (perf_sheet[char+ str(rows)]) = 0

2nd May 2023, 2:06 PM
Rajiv K V
Rajiv K V - avatar
10 Respostas
+ 6
Where is your attempt?
2nd May 2023, 2:11 PM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar
+ 6
Rajiv K V , > to get helpful hints, we need to see your code. at the moment we can only guess. > also provide a clear task description what should your code achieve, what input / output is expected ?
2nd May 2023, 5:53 PM
Lothar
Lothar - avatar
+ 5
Learn from the lesson *loop* again. Understand the given example of how to structure the loop in code. The e.g. already tell you how to write loop. Try to yourself first, if you get error share your code, so we can help you from there.
2nd May 2023, 2:20 PM
D Shah đŸŽŻâłïž
D Shah đŸŽŻâłïž - avatar
+ 4
Rajiv K V compare the loops you have written and observe which parts of the loops are changing from one loop to the next. The parts that change could be made into variables that you assign before entering the loop. E.g., limit = 1 value = a for x in range(limit): print(value) Now that you have the loop code defined by variables, the same loop code can be used for any limit and value. You may put the loop into a nested loop where the limit and value are changing: #main code value = 99 for limit in range(4): value += 1 for x in range(limit): print(value) You may also put the loop into a function: def repeatPrint(limit, value): for x in range(limit): print(value) return #main code y = 99 for x in range(4): y += 1 repeatPrint(x, y)
2nd May 2023, 4:03 PM
Brian
Brian - avatar
+ 4
Rajiv K V you changed your question and now you give us this unindented code with unknown variable workbook and inconsistent if else statements. How can we know what this is supposed to do? How are you actually nesting your if-else or if-elif? It is hard to tell from your non-indented copy-paste. As others said before, put it in a python code bit and post it. Looks like openpyxl code... some kind of table or Excel file?
3rd May 2023, 2:29 AM
Bob_Li
Bob_Li - avatar
+ 2
print() not Print range(n) not range n carelessness is a bad habit you have to break. you can use two nested for-range, or a while with a for-range inside.
2nd May 2023, 4:04 PM
Bob_Li
Bob_Li - avatar
+ 2
Rajiv K V Are you able to open the files in the first place? did you add this at the top? from openpyxl import load_workbook import os also, you need workbooks list and path to make the for loop work. (example) workbooks = ["wb1.xlsx", "wb2.xlsx"] path = "C:\\My files\\test_files\\" we can give generic advise. You have to modify it to suit your specific use case.
3rd May 2023, 7:40 AM
Bob_Li
Bob_Li - avatar
+ 1
You can do it more simple: if you want to split a string to a list of character just put: split_input = list(input()) And done!
4th May 2023, 10:15 AM
Ugulberto SĂĄnchez
Ugulberto SĂĄnchez - avatar
0
Tried code added as requested, which is not working....
2nd May 2023, 6:46 PM
Rajiv K V
Rajiv K V - avatar
0
Xhj
4th May 2023, 5:51 AM
Sonu Kumar
Sonu Kumar - avatar