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....
18 odpowiedzi
+ 3
#my solution
sth=input().split(',')
target=input()
print((sth.index(target)+1)*5)
+ 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)
+ 1
all=input().split(",")
t=input()
print((all.index(t) + 1)*5)
0
you are incrementing time, not returning it from inside a function.
delete the "return time" line and the code will run
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.
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?
0
Yes it is.
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.
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)
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.
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 😊🙌
0
My solution:
boxes = input().split(",")
item = input()
x = boxes.index(item)
wait = (x+1)*5
print(wait)
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;
}
0
boxes = input('boxes>: ').split(',')
item = input('item>: ')
time = 5
for i in boxes:
if i == item:
print(time)
else: time+=5
0
n=input()
y=input()
x=n.split(',')
print(x)
for i in x:
if i==y:
print((x.index(i)+1)*5)
0
x=input()
SarK=input()
x=x.split(',')
print(5*(x.index(SarK,0,len(x)-1)+1))
The only problem is case#4
- 1
items = input().split(",")
item = input()
time=0
for i in items:
time += 5
if i == item:
break
print(time)
- 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)