+ 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))
18 Antworten
+ 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 ?
+ 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"?
+ 2
Line 4 check it once again
+ 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))
+ 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
+ 1
Thanks you guys for your help and suggestions
0
No output
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
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.
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
0
Ok finally found the solution
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
0
Thanks for trying
0
Franklyn Etu ,
Please add [Solved] to the title if you're done.
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))
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
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)))
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))