Вот мой код:Learning Python и пытается получить первые две буквы и две последние буквы строки
# B. both_ends
# Given a string s, return a string made of the first 2
# and the last 2 chars of the original string,
# so 'spring' yields 'spng'. However, if the string length
# is less than 2, return instead the empty string.
def both_ends(s):
if len(s) <= 2:
return ""
else:
return s[0] + s[1] + s[len(s)-2] + s[len(s-1)]
# +++your code here+++
return
К сожалению, моя программа не запускается. :(Я уверен, что я что-то с видом, так как я новичок с Python
Вот ошибка:.
> Traceback (most recent call last):
File "C:\Users\Sergio\Desktop\google-python-exercises\google-python-exercises\basic\string1.py", line 120, in <module>
main()
File "C:\Users\Sergio\Desktop\google-python-exercises\google-python-exercises\basic\string1.py", line 97, in main
test(both_ends('spring'), 'spng')
File "C:\Users\Sergio\Desktop\google-python-exercises\google-python-exercises\basic\string1.py", line 44, in both_ends
return s[0] + s[1] + s[len(s)-2] + s[len(s-1)]
TypeError: unsupported operand type(s) for -: 'str' and 'int'
Спасибо за помощь, ребята.: D
'", если длина строки меньше, чем 2, возвращение вместо пустой строки. ", а затем в вашем коде у вас есть' if len (s) <= 2: return "" '. обратите внимание на разницу? (<2 vs <= 2) – Ponkadoodle