Advertisement
Guest User

Untitled

a guest
Sep 13th, 2023
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. print('___________________________________________________')
  2. print('Меню:')
  3. print('     1) реверс строки. пример -> ремирп')
  4. print('     2) превращение строки в заборчик -> ЗаБоРчИк')
  5. print('     3) поиск самого длинного слова слова в строке')
  6. print('     0) выйти')
  7. print('___________________________________________________')
  8.  
  9. user_choice = input('Введите число для выбора работы программы (например 1, 2 или 3): ')
  10.  
  11. def askString():
  12.     return input('Введите слово или несколько слов: ')
  13.  
  14. def stringReverse(string):
  15.     # всезнайки с форума напишут так:
  16.     # print( string[::-1] )
  17.  
  18.     # а я учусь и напишу так
  19.     string_length = len(string)
  20.     result = ''
  21.     while (True):
  22.         result = result + string[string_length - 1]
  23.         string_length = string_length - 1
  24.         if string_length == 0:
  25.             break
  26.     return result
  27.  
  28.  
  29. def stringZabor(string):
  30.     string_length = len(string)
  31.     currentCharIndex = 0
  32.     boolFlagZabora = True
  33.     result = ''
  34.     while (True):
  35.         if boolFlagZabora == True:
  36.             result = result + string[currentCharIndex].upper()
  37.             boolFlagZabora = False
  38.         else:
  39.             result = result + string[currentCharIndex].lower()
  40.             boolFlagZabora = True
  41.  
  42.         currentCharIndex = currentCharIndex + 1
  43.         if currentCharIndex == string_length:
  44.             break
  45.     return result
  46.  
  47.  
  48. def stringLongWordSearch(string):
  49.     wordList = string.split()
  50.     longest_word = max(wordList, key=len)
  51.     return longest_word
  52.  
  53. #match/case не работает в питон 3.9 поэтому делаю на любимых if'ах
  54. if user_choice[0:3] == "2+1" or user_choice[0:3] == "2 1" or user_choice[0:3] == "2и1" or user_choice[0:2] == "21":
  55.     print(stringReverse(stringZabor(askString())))
  56. elif user_choice[0:3] == "1+2" or user_choice[0:3] == "1 2" or user_choice[0:3] == "1и2" or user_choice[0:2] == "12":
  57.     print(stringZabor(stringReverse(askString())))
  58. elif user_choice[0] == "1":
  59.     print(stringReverse(askString()))
  60. elif user_choice[0] == "2":
  61.     print(stringZabor(askString()))
  62. elif user_choice[0] == "3":
  63.     print(stringLongWordSearch(askString()))
  64. elif user_choice[0] == "0":
  65.     print("Пользователь нажал выход")
  66. else:
  67.     print('Ошибка, неверный выбор пользователя.')
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement