why does this code only work for one case?. python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why does this code only work for one case?. python

https://www.sololearn.com/coach/74?ref=app https://code.sololearn.com/ccpte48rG59k/?ref=app n=str(input()) a=0 b="" for i in range(len(n)): if n[len(n)-i-1]== ("=" or "/"): a+=i break else: continue for i in range(len(n)-a,len(n)): b+=str(n[i]) print(b)

18th Mar 2021, 6:33 AM
Central Processing Unit
Central Processing Unit - avatar
27 Answers
+ 7
i dont understand how changing the literal to list changes anything. edited the code to this n=str(input()) a=[] b="" for i in range(len(n)): if n[len(n)-i-1]== "=" or n[len(n)-i-1]=="/": a.append(i) break else: continue for i in range(len(n)-a[0],len(n)): b+=str(n[i]) print(b) and it worked for all cases. i also tried fixing the logical error first and that didnt work, so the only problem was having the number not in a list. i still dont understand how changing a single number to a list does anything, but thanks anyway!!! Rik Wittkopp visph Eddy M Mir Abir Hossain Atul
18th Mar 2021, 9:22 AM
Central Processing Unit
Central Processing Unit - avatar
+ 5
I think, you have to try on PC. Because I have problems with Sololearn ide to take user input.
20th Mar 2021, 6:32 AM
gajanand dave
+ 4
Mir Abir Hossain and Sazal Jain are right: left side of `n[len(n)-i-1]== ("=" or "/")` is right, but `("=" or "/")` always gives "=" (`or` operator: if value from the left converted to Boolean (bool) is True (in case of strings (str), if contains at least one character), return it; otherwise return the value from the right). If you fix it, your code is working (although you wrote a lot of unnecessary code which can be shortened using slices and other things you possibly do not know yet and language aspects).
19th Mar 2021, 3:41 PM
#0009e7 [get]
#0009e7 [get] - avatar
+ 3
No one knows about that Please show your attempt and specify the language too
18th Mar 2021, 6:34 AM
Atul [Inactive]
+ 3
There are some syntax mistake and some logical mistake. if n[len(n)-i-1]==("=" or "/"): should be --> if n[len(n)-i-1]=="=" or n[len(n)-i-1]=="/" (This is causing you no output) and logical mistake was a+=i. Instead you can use --> a=[] a.append(i) That way you can keep track of all the '/' and '=' (This is not a solution. Just figuring out what is going wrong in your code.)
18th Mar 2021, 9:11 AM
Mir Abir Hossain
Mir Abir Hossain - avatar
+ 3
Syntax mistake It should be n[len(n)-1-i]=="=" or n[len(n)-1-i]=="/":
19th Mar 2021, 4:54 AM
Sazal Jain
Sazal Jain - avatar
+ 2
what is the task description/requirements? I cannot access the code coach link ^^
18th Mar 2021, 7:09 AM
visph
visph - avatar
+ 2
visph Task details attached for you: You and your friends like to share YouTube links all throughout the day. You want to keep track of all the videos you watch in your own personal notepad, but you find that keeping the entire link is unnecessary. Keep the video ID (the combination of letters and numbers at the end of the link) in your notepad to slim down the URL. Task: Create a program that parses through a link, extracts and outputs the YouTube video ID. Input Format: A string containing the URL to a YouTube video. The format of the string can be in "https://www.youtube.com/watch?v=kbxkq_w51PM" or the shortened "https://youtu.be/KMBBjzp5hdc" format. Output Format: A string containing the extracted YouTube video id. Sample Input: https://www.youtube.com/watch?v=RRW2aUSw5vU Sample Output: RRW2aUSw5vU
18th Mar 2021, 8:37 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
# with re module: import re url = input() print(re.search('\w+
#x27;,url).group(0))
18th Mar 2021, 8:43 AM
visph
visph - avatar
+ 2
Central Processing Unit Might I suggest that your code could be completed using easier logic. visph has provided a simple solution. Or you could consider this: print((input()[-11:]))
18th Mar 2021, 8:53 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Rik Wittkopp your solution is quite simpler, but requires to be sure that video id is always 11 char long ;)
18th Mar 2021, 8:56 AM
visph
visph - avatar
+ 2
Rik Wittkopp that may be the case... but who knows? only youtube has the response ^^
18th Mar 2021, 9:05 AM
visph
visph - avatar
+ 2
Congrats! Central Processing Unit changing into list made the code work because you used a+=i. i values are different from a values in your previous code. Suppose your i values are 5 and 7. But your a value is 12 (5+7) so in the next for loop len(n)-a is creating len(n)-12, whereas you needed len(n)-7. Check this wrong code and output to get an idea how it went wrong. https://code.sololearn.com/cEM5HFgLJDHR/?ref=app
18th Mar 2021, 9:33 AM
Mir Abir Hossain
Mir Abir Hossain - avatar
+ 2
Deepika Pathak Spam is not welcome in the Q&A thread. Please delete your posts and reserve this thread for coding related questions only. Treat Sololearn with respect and you will be treated with respect also
19th Mar 2021, 9:28 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Try this => import re takeLink =list(input()) stri="".join(takeLink ) backSlash=0 if re.search("com",stri): for i in range(len(takeLink)): if takeLink[i]==r'=': break print("".join(takeLink[i+1:])) else: for i in range(len(takeLink )): if takeLink[i]==r'/': backSlash +=1 if backSlash == 3: break print ("".join(takeLink[i+1:]))
20th Mar 2021, 3:34 AM
Shobhit :)
Shobhit :) - avatar
+ 1
Atul i have put the code in.
18th Mar 2021, 6:36 AM
Central Processing Unit
Central Processing Unit - avatar
+ 1
Tried some other strings: #"==abc" >>> "bc==abc" #"a=b=c" >>> "=b=c" #"=abc=" >>> "abc=" #"a=bc=" >>> "bc=" #"abc==" >>> "="
18th Mar 2021, 8:46 AM
Eddy M
+ 1
visph That's true, I will need to look into that further. It was an assumption I made at the time I was looking for a solution
18th Mar 2021, 8:59 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
i dont really know whats wrong with my code. in the test cases. sometimes it works and sometimes it has no output. no clue whats going on here. Rik Wittkopp visph Eddy M Atul
18th Mar 2021, 9:02 AM
Central Processing Unit
Central Processing Unit - avatar
+ 1
visph 😂
18th Mar 2021, 9:05 AM
Rik Wittkopp
Rik Wittkopp - avatar