+ 2

Please what's wrong with my code?[solved]

all_items = input() spec_item = input() def find_time(boxes, item): mod_boxes = boxes.replace(',', ' ') box_list = mod_boxes.split() print(box_list) time = 5 for i in box_list: while i != item: time = time + 5 return time print(find_time(all_items, spec_item))

29th Dec 2023, 9:51 AM
Franklyn Etu
Franklyn Etu - avatar
18 Answers
+ 4
Franklyn Etu , it is always a good idea to provide a task description. it makes it easier to understand the code and is saving time and effort for the helpers... btw: what should be done / output if the item is not found during iteration ?
29th Dec 2023, 4:19 PM
Lothar
Lothar - avatar
+ 3
Please describe why you think your code is wrong. Is it because you got a EOF kind of error message from the playground, or it produce "no output"?
29th Dec 2023, 10:40 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
Line 4 check it once again
31st Dec 2023, 4:56 AM
Rutvik Muniraja
Rutvik Muniraja - avatar
+ 1
Take a look: all_items = input() spec_item = input() def find_time(boxes, item): mod_boxes = boxes.replace(',', ' ') box_list = mod_boxes.split() time = 5 for i in box_list: if i != item: time = time + 5 break return time print(find_time(all_items, spec_item))
29th Dec 2023, 10:54 AM
Franklyn Etu
Franklyn Etu - avatar
+ 1
Glad you found the solution yourself. You can simplify your code like this def find_time(boxes, item): # it turn the string into a list in place, separated by a comma boxes = boxes.split(',') time = 5 for i in boxes: if i == item: break else: time += 5 # it is the same as time = time + 5, but shorter return time
29th Dec 2023, 12:32 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Thanks you guys for your help and suggestions
30th Dec 2023, 8:14 PM
Franklyn Etu
Franklyn Etu - avatar
0
No output
29th Dec 2023, 10:52 AM
Franklyn Etu
Franklyn Etu - avatar
0
I just noticed that I shouldn't have used the while loop , so I used a conditional. But it only passes a few test cases
29th Dec 2023, 10:53 AM
Franklyn Etu
Franklyn Etu - avatar
0
With your new code, what is your expected result. I try inputs 'a,b,c,d,e,f,g,h' and 'e', it returns 10 as I expected.
29th Dec 2023, 11:18 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
Sorry I should have explained it better. It is supposed to return the time taken to find the item. Each item takes 5mins. So in this case it's supposed to be 25mins
29th Dec 2023, 11:22 AM
Franklyn Etu
Franklyn Etu - avatar
0
Ok finally found the solution
29th Dec 2023, 11:29 AM
Franklyn Etu
Franklyn Etu - avatar
0
If you noticed I put the break statement where in place of the else statement expecting it would work the same way. But it didn't. It supposed to tho
29th Dec 2023, 11:31 AM
Franklyn Etu
Franklyn Etu - avatar
0
Thanks for trying
29th Dec 2023, 11:31 AM
Franklyn Etu
Franklyn Etu - avatar
0
Franklyn Etu , Please add [Solved] to the title if you're done.
29th Dec 2023, 12:54 PM
Rain
Rain - avatar
0
Take a look: all_items = input() spec_item = input() def find_time(boxes, item): mod_boxes = boxes.replace(',', ' ') box_list = mod_boxes.split() time = 5 for i in box_list: if i != item: time = time + 5 break return time print(find_time(all_items, spec_item))
29th Dec 2023, 7:05 PM
Sayfdin Sya3li
Sayfdin Sya3li - avatar
0
While I don’t exactly know what is wrong since i am pretty new and didnt test the code it would if you said which row the error says it is and i think you should ad a try and exept command in some spots
29th Dec 2023, 7:20 PM
Connor Whitman
Connor Whitman - avatar
0
Try this one: all_items = input("1 ") spec_item = input("2 ") def find_time(boxes, item): mod_boxes = boxes.replace(',', ' ') box_list = mod_boxes.split(" ") print(box_list) time = 5 for i, n in enumerate(box_list): if i == item: return time time += 5 print(find_time(all_items, int(spec_item)))
30th Dec 2023, 6:13 PM
omer kemal
omer kemal - avatar
0
all_items = input() spec_item = input() def find_time(boxes, item): mod_boxes = boxes.replace(',', ' ') box_list = mod_boxes.split() time = 5 for i in box_list: if i == item: return time time += 5 # If the item is not found in any box return -1 # Or any other value to indicate the item wasn't found print(find_time(all_items, spec_item))
31st Dec 2023, 4:07 AM
Мухаммадчон Мусаев
Мухаммадчон Мусаев - avatar