sophisticatedmarten

randomnumbergen

Jul 3rd, 2025
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | Source Code | 0 0
  1. #create a program that displays random numbers with the amount of numbers being chosen by the users +
  2. # input and calculate the total of the numbers, average the numbers and show the largest and smallest numbers generated
  3. import random
  4. #the starting function
  5. continueLoop = '1'
  6. #while loop to repeat the program
  7. while continueLoop == '1':
  8. #new funtion
  9.     def main():
  10.         num = 0
  11.         num = int(input ("How many numbers do you want? "))    
  12.         #open a file called myRandomNumbers.txt
  13.         genRand(num)
  14.         numArray = readNum()
  15.         numAverage = avgNum(numArray)
  16.         maxNum= maximum(numArray)
  17.         minNum = minimum(numArray)
  18.    
  19. #generating random numbers based on the input in the main() funtion        
  20.     def genRand(num):
  21.         with open('myRandomNumbers.txt', 'w') as file:
  22.             for count in range(num):
  23.                 individualrandomnum = random.randint(1,500)
  24.                 file.write(str(individualrandomnum) + '\n')
  25. #program reads the numbers and prints them                  
  26.     def readNum():
  27.         numArray = []
  28.         with open('myRandomNumbers.txt', 'r') as file:
  29.             lines = file.readlines()
  30.             for line in lines:
  31.                 numArray.append(str(line) + ' ')
  32.             print ("The random numbers generated are:" + '\n' + (str(lines)))
  33.         return numArray
  34.   #funtion for finding the average  
  35.     def avgNum(numArray):
  36.         #[one, two, three, four]
  37.         total = 0
  38.         for num in numArray:
  39.             #all the numbers added together
  40.             total += int(num)
  41.         #total = 10
  42.         #the average number equation
  43.         avgNumber = total / len(numArray)
  44.         print("\n" + "Total of the random numbers: " + str(total))
  45.         print("Count of the random numbers is: " + str(len(numArray)))
  46.         print('Average of the random numbers:' + f'{avgNumber:.2f}')
  47.         return avgNumber
  48.    #function to find the highest number in the array
  49.     def maximum(num):
  50.         maximum = num[0]
  51.         for a in num:
  52.             if a > maximum:
  53.                 maximum = a
  54.         print("The largest of the random numbers: " + str(maximum))
  55.         return maximum
  56.    
  57.       #function to find the lowest number in the array  
  58.     def minimum(num):  
  59.         minimum = num[0]
  60.         for a in num:
  61.             if a < minimum:
  62.                 minimum = a
  63.         print("The smallest of random numbers: " + str(minimum))
  64.         return minimum
  65.        
  66.     if __name__ == '__main__':
  67.         main()
  68.         #determines if the function should be repeated
  69.         continueLoop = input ("run program again 1=yes 0=no ")
  70.  
Advertisement
Add Comment
Please, Sign In to add comment