Advertisement
Retroledeom

Lab Activity 2

May 5th, 2022
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. rerun = 'Y'
  2.  
  3. # list to contain the inputted numbers
  4. numbers = []
  5.  
  6. # program is in a while loop
  7. while rerun.upper() == 'Y':
  8.     # input
  9.     try:
  10.         num = int(input('Task 1: Please give a number: '))
  11.     # Error Message when character is not a number.
  12.     except ValueError:
  13.         print("Sorry. That is not a number. Restart the program.")
  14.         quit()
  15.  
  16.     # Appends all the numbers made into a list made outside of while loop
  17.     numbers.append(num)
  18.  
  19.     # computations as well as output
  20.  
  21.     # Even / Odd Computation: computes if even or odd using modulo
  22.     even_odd = (num % 2)
  23.     if even_odd == 1:
  24.         print(f"Task 2: {num} is an odd number")
  25.     elif even_odd != 1:
  26.         print(f"Task 2: {num} is an even number")
  27.  
  28.     # Prime / Composite computation: computes if prime or composite
  29.     # by determining whether the number has more than 2 factors
  30.     if num > 1:
  31.         # range( all numbers above 2, up until the given number divided by 2 + 1)
  32.         for i in range(2, int(num / 2) + 1):
  33.             if (num % i) == 0:
  34.                 print(f'Task 3: {num} is a composite number')
  35.                 break
  36.         else:
  37.             print(f'Task 3: {num} is a prime number')
  38.     else:
  39.         print(f'Task 3: {num} is neither a prime or composite number')
  40.  
  41.     # Variables for the biggest and smallest numbers
  42.     biggest = max(numbers)
  43.     smallest = min(numbers)
  44.  
  45.     # Variables for the average of the average of the total numbers
  46.     totalNumbers = len(numbers)
  47.     sumNum = sum(numbers)
  48.     ave = float(sumNum) / float(totalNumbers)
  49.  
  50.     # computation for odd / even result without modulo use
  51.     # a/2 computes with a decimal point while a//2 computes without decimal point.
  52.     # They will not be equal if the number is odd hence the if else code syntax
  53.     if num / 2 == num // 2:
  54.         not_modulo = 'even'
  55.     else:
  56.         not_modulo = 'odd'
  57.  
  58.     print(f'Task 4: The highest number so far is {biggest}')
  59.     print(f'Task 4: The lowest number so far is {smallest}')
  60.     print(f'Task 5: The total sum of the entered numbers is: {sumNum}')
  61.     print(f'Task 6: The average sum of all numbers enters is: {ave}')
  62.     print(f'Task 7: The number {num} is {not_modulo} and is '
  63.           f'computed without the use of the modulo "%" operator')
  64.     rerun = input('Do you want to run the program again?'
  65.                   ' Press Y for yes. Press any other key for no : ')
  66.  
  67. # Termination of program
  68. print('Goodbye! Program ended.')
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement