Advertisement
GeorgiLukanov87

First Steps in OOP - Lab

Sep 23rd, 2022
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.99 KB | None | 0 0
  1. # First Steps in OOP - Lab
  2.  
  3. # https://pastebin.com/u/GeorgiLukanov87
  4.  
  5. # 01. Rhombus of Stars
  6. # 02. Scope Mess
  7. # 03. Class Book
  8. # 04. Car
  9. # 05. Music
  10.  
  11.  
  12. ------------------------------------------------------------------------------------------------
  13.  
  14. # 01. Rhombus of Stars
  15.  
  16.  
  17. def print_rhombus_func(n):
  18.     for i in range(n):
  19.         print((n - 1 - i) * " " + (i + 1) * '* ')
  20.  
  21.     for i in range(n - 1):
  22.         print((i+1) * " " + (n-1-i) * "* ")
  23.  
  24.  
  25. n = int(input())
  26. print_rhombus_func(n)
  27.  
  28.  
  29. ------------------------------------------------------------------------------------------------
  30.  
  31.  
  32. # 02. Scope Mess
  33.  
  34. x = 'global'
  35.  
  36.  
  37. def outer():
  38.     global x
  39.     x = 'local'
  40.  
  41.     def inner():
  42.         global x
  43.         x = 'nonlocal'
  44.         print('inner:', x)
  45.  
  46.     def change_global():
  47.         global x
  48.         x = "global: changed!"
  49.  
  50.     print("outer:", x)
  51.     inner()
  52.     print("outer:", x)
  53.     change_global()
  54.  
  55.  
  56. print(x)
  57. outer()
  58. print(x)
  59.  
  60.  
  61. ------------------------------------------------------------------------------------------------
  62.  
  63. # 03. Class Book
  64.  
  65.  
  66. class Book:
  67.     def __init__(self, name, author, pages):
  68.         self.name = name
  69.         self.author = author
  70.         self.pages = pages
  71.  
  72.  
  73. # 04. Car
  74.  
  75. class Car:
  76.     def __init__(self, name, model, engine):
  77.         self.name = name
  78.         self.model = model
  79.         self.engine = engine
  80.  
  81.     def get_info(self):
  82.         return f'This is {self.name} {self.model} with engine {self.engine}'
  83.  
  84.  
  85. ------------------------------------------------------------------------------------------------
  86.  
  87. # 05. Music
  88.  
  89.  
  90. class Music:
  91.     def __init__(self, title, artist, lyrics):
  92.         self.title = title
  93.         self.artist = artist
  94.         self.lyrics = lyrics
  95.  
  96.     def print_info(self):
  97.         return f'This is "{self.title}" from "{self.artist}"'
  98.  
  99.     def play(self):
  100.         return self.lyrics
  101.  
  102. ------------------------------------------------------------------------------------------------
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement