+ 3

Help me to find mistake in this code?

I have attempted a try. https://code.sololearn.com/cxR38Eci5kT1/?ref=app I'm trying to solve this but I'm missing somewhere please help me to find out the mistake. If sample input: 'School' Print 0 # because first element is not equal to last element If sample input: 'noon' Print 2 #because first element = last element

6th May 2021, 6:18 PM
Utsav Singh
Utsav Singh - avatar
11 Answers
+ 2
Iutsavsingh if I understand your question correctly, you are checking the first and the last element, then why use a loop? Have a look at this one: s = input("enter string:-\n") if s[0] is s[-1]: print(2) else: print(0)
6th May 2021, 7:49 PM
Md. Faheem Hossain
Md. Faheem Hossain - avatar
+ 3
Iutsavsingh This does the job: a = input() print(2 * (a[0] == a[-1])) # Hope this helps
6th May 2021, 8:05 PM
Calvin Thomas
Calvin Thomas - avatar
+ 3
Iutsavsingh if you want to take range of inputs, then why, out of loop, you were taking an str as input. You should have done this: # n is the number of range of # inputs you want to take n = int(input()) for _ in range(n): s = input() if s[0] ....... ....... Or, if you want infinity then use a while loop instead of for: while True: ...... ...... # stop it by ctrl+c In the meantime you've changed your code, as far as I can recall, it was something like: s = input() for i in range(s): ...... range() takes integers as input, so it will raise a TypeError. But sorry, I don't see any index error here. Please give the exact code with which you are having trouble, only then we can help you. # the approach of Rik Wittkopp will work fine regardless of what system you are using
7th May 2021, 5:15 PM
Md. Faheem Hossain
Md. Faheem Hossain - avatar
+ 3
Faheem yes I copied code from the platform where I was doing a challenge there I faced different error and in sololearn it is showing index error and now I understood the problem and I am totally satisfied with Rik Wittkopp approach
7th May 2021, 5:48 PM
Utsav Singh
Utsav Singh - avatar
+ 1
s = input() if s[0] == s[-1]: print(2) else: print(0)
6th May 2021, 6:26 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Rik Wittkopp string range out of index
6th May 2021, 6:31 PM
Utsav Singh
Utsav Singh - avatar
+ 1
Faheem see my concern is whenever it takes for input so I run for loop which will take range of input and iterate over the element and I understood your approach but why this showing index out of range
7th May 2021, 2:04 AM
Utsav Singh
Utsav Singh - avatar
+ 1
Iutsavsingh What code are you running which shows error: index out of range
7th May 2021, 2:15 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Iutsavsingh I am surprised by that as it consistently produces the result you asked for when I run it. I am using the Sololearn platform to test. Are you using something else
7th May 2021, 2:41 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Rik Wittkopp it's working on vscode But not on sololearn
7th May 2021, 3:40 AM
Utsav Singh
Utsav Singh - avatar
0
Rik Wittkopp your approach
7th May 2021, 2:22 AM
Utsav Singh
Utsav Singh - avatar