Safety deposit boxes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Safety deposit boxes

boxes=input().split(",") key=input() time=0 listlen = len(boxes) for i in range(0,listlen): if boxes[i]!=key: time += 5 else: time += 5 return time print(time) When i run this code, 'return outisde function' pops up. I dont know what part is wrong....

13th Mar 2021, 11:49 AM
Seung won LEE
Seung won LEE - avatar
18 Answers
+ 3
#my solution sth=input().split(',') target=input() print((sth.index(target)+1)*5)
12th May 2022, 1:26 AM
MING ZI CHEN
MING ZI CHEN - avatar
+ 2
Here is a possible solution. BoxContent = input().split(',') item=input() time=1 for n in BoxContent: if item==n: break else: time+=1 time=time*5 print(time)
5th Feb 2023, 1:16 PM
Michael Nazar Bogdan
Michael Nazar Bogdan - avatar
+ 1
all=input().split(",") t=input() print((all.index(t) + 1)*5)
20th Jul 2023, 4:10 AM
Мартин Симонян
0
you are incrementing time, not returning it from inside a function. delete the "return time" line and the code will run
13th Mar 2021, 1:31 PM
Slick
Slick - avatar
0
Thanks Slick. Then how to terminate the for loop when else statement is fulfilled and get the time value. I want the for loop keep running in if statement before else statement runs.
13th Mar 2021, 11:53 PM
Seung won LEE
Seung won LEE - avatar
0
The if-else statement should be a "one or the other" situation. I'm assuming "key" is the final input (or just the value at which you want) to stop the time. is this correct?
13th Mar 2021, 11:58 PM
Slick
Slick - avatar
0
Yes it is.
14th Mar 2021, 2:36 AM
Seung won LEE
Seung won LEE - avatar
0
Okay cool, then use a break statement after you add 5 to time to jump out of the for loop. it should read: if the value of boxes[i] isn't equal to the value of "key", add 5 to time. If not, add 5 to time and break out of the loop. Im not 100% sure of the purpose of your program so this is how i guess it should go.
14th Mar 2021, 10:20 AM
Slick
Slick - avatar
0
I think it'll work boxes=input().split(",") key=input() time=0 listlen = len(boxes) for i in range(0,listlen): if boxes[i]!=key: time += 5 else: time += 5 break print(time)
11th May 2022, 6:33 PM
MYusuf
MYusuf - avatar
0
Try with enumerate (<iterable>, <start>). The enumerate function is very useful here. It returns some separated tuples with an index number and a list value in each separately like >> (0, item1), (1, item2) etc.
17th May 2022, 8:42 PM
Róbert Erdő
Róbert Erdő - avatar
0
My solution is: i = input().split(",") x = input() c = 0 for t in i: if not t == x: c += 5 else: c += 5 break print(c) But with what MING ZI CHEN wrote, I changed it to: print((i.index(x)+1)*5) And that is way better 😊🙌
24th Jun 2022, 5:03 PM
Martin Valdés Mallaug
0
My solution: boxes = input().split(",") item = input() x = boxes.index(item) wait = (x+1)*5 print(wait)
17th Sep 2022, 7:12 PM
Oleh Khoptii
Oleh Khoptii - avatar
0
This is my solution using c++ #include <iostream> #include <string> #include <sstream> #include <vector> using namespace std; int main() { string items_string; getline(cin, items_string); string target_item; getline(cin, target_item); // Parse the items string into a vector of strings vector <string> items; stringstream items_stream(items_string); string item; while (getline(items_stream, item, ',')) { items.push_back(item); } // Find the target item in the vector int time = 0; for (const string& item : items) { time += 5; if (item == target_item) { break; } } cout << time << endl; return 0; }
3rd Jan 2023, 2:13 PM
Muhammad Rohisyam Bin Rodzy
0
boxes = input('boxes>: ').split(',') item = input('item>: ') time = 5 for i in boxes: if i == item: print(time) else: time+=5
28th Feb 2023, 4:23 PM
Ehab Helmy
Ehab Helmy - avatar
0
n=input() y=input() x=n.split(',') print(x) for i in x: if i==y: print((x.index(i)+1)*5)
2nd Mar 2023, 1:00 AM
mohammad
0
x=input() SarK=input() x=x.split(',') print(5*(x.index(SarK,0,len(x)-1)+1)) The only problem is case#4
30th May 2023, 8:52 AM
IsantosDowd
IsantosDowd - avatar
- 1
items = input().split(",") item = input() time=0 for i in items: time += 5 if i == item: break print(time)
17th Oct 2021, 8:03 PM
Mateo González Bufi
Mateo González Bufi - avatar
- 2
This is my solution: s=input().split(",") r=input() count=0 for i in s: iter(s) count+=5 if i==r: print (count)
14th Oct 2021, 3:16 PM
Norberto Costa
Norberto Costa - avatar