Advertisement
Zafichi

hw2

Dec 13th, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. # 1
  2. try:
  3.     first_num = float(input('Enter first number:'))
  4.     second_num = float(input('Enter second number:'))
  5.     sum_of_nums = first_num + second_num
  6.     difference = first_num - second_num
  7.     print(int(sum_of_nums))
  8.     print(difference)
  9.     print(sum_of_nums)
  10. except ValueError as n:
  11.     print("Exception, ", n)
  12. else:
  13.     print('Ok')
  14.  
  15.  
  16. # 2
  17. def num(value):
  18.     if value % 2 == 0 and 2 <= value <= 27:
  19.         print("In first range")
  20.     elif value % 2 != 0 and 29 <= value <= 53:
  21.         print("In second range")
  22.     elif value % 2 == 0:
  23.         print("Odd")
  24.     else:
  25.         print("It's something")
  26.  
  27.  
  28. num(int(input("Enter number: ")))
  29.  
  30.  
  31. # 3
  32. def answer(first_value, second_value):
  33.     print(int(first_value / second_value))
  34.     print(first_value / second_value)
  35.  
  36.  
  37. first_num = float(input('Enter first number:'))
  38. second_num = float(input('Enter second number:'))
  39.  
  40. answer(first_num, second_num)
  41.  
  42. # 4
  43. my_str = 'Going through the forest is my favourite part of the walk. My dog Benji loves it too. I\'m Grace. ' \
  44.            'I live on a farm with my parents and I take Benji for a walk most days after school. While Benji\'s ' \
  45.            'playing, I stop to take a photo of a butterfly. I\'m thinking about posting it on Facebook, but then ' \
  46.            'I hear Benji barking. He\'s jumping and running around a boy. The poor boy looks worried. \'Benji, stop! ' \
  47.            'Come here!\' I call and throw him his ball. I\'m about to say sorry to the boy, but he\'s gone.'
  48.  
  49.  
  50. def str_in_column(some_str, max_len):
  51.     new_str = '   '
  52.     count = 0
  53.     for i in some_str.split():
  54.         count += len(i)
  55.         if count > max_len:
  56.             new_str += '\n\t'
  57.             count = len(i)
  58.         elif new_str != '':
  59.             new_str += ' '
  60.             count += 1
  61.         new_str += i
  62.     print(new_str)
  63.  
  64.  
  65. str_in_column(my_str, 20)
  66.  
  67.  
  68. # 5
  69. def find_max(num_of_results, input_lst):
  70.     for i in input_lst:
  71.         if type(i) == str:
  72.             input_lst.remove(i)
  73.     input_lst.sort()
  74.     input_lst.reverse()
  75.     print(input_lst[0:num_of_results])
  76.  
  77.  
  78. lst = [20, 58, 9, 23, 62, 'hi', 46, 48, 125.23, 55, 95, 68, 2, 2, 50, 'list', 76, 14, 92, 12, 71, 12, 30]
  79. find_max(int(input('How much results do you want: ')), lst)
  80.  
  81.  
  82. # 6
  83. import math
  84.  
  85.  
  86. def square_of_circle(r):
  87.     s = (r**2) * math.pi
  88.     print('Square of circle is: ', s)
  89.  
  90.  
  91. square_of_circle(float(input('Enter radius of circle: ')))
  92.  
  93.  
  94. # 7
  95. def lo_rew(first_str, second_str):
  96.     print(second_str.lower()[::-1], first_str.lower()[::-1])
  97.  
  98.  
  99. lo_rew("Hello", 'World')
  100.  
  101.  
  102. # 8
  103. def list_of_one(lst_of_nums):
  104.     while len(lst_of_nums) != 1:
  105.         if len(lst_of_nums) > 3 and len(lst_of_nums) % 2 == 0:
  106.             del lst_of_nums[1::2]
  107.         elif len(lst_of_nums) > 3 and len(lst_of_nums) % 2 != 0:
  108.             del lst_of_nums[1::2]
  109.             lst_of_nums.pop(0)
  110.         elif len(lst_of_nums) == 3:
  111.             lst_of_nums.pop(1)
  112.             lst_of_nums.pop(0)
  113.         elif len(lst_of_nums) == 2:
  114.             lst_of_nums.pop()
  115.         print(lst_of_nums)
  116.  
  117.  
  118. lst = [1, 2, 3, 4, 5, 6, 7, 8]
  119. list_of_one(lst)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement