Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #create a program that displays random numbers with the amount of numbers being chosen by the users +
- # input and calculate the total of the numbers, average the numbers and show the largest and smallest numbers generated
- import random
- #the starting function
- continueLoop = '1'
- #while loop to repeat the program
- while continueLoop == '1':
- #new funtion
- def main():
- num = 0
- num = int(input ("How many numbers do you want? "))
- #open a file called myRandomNumbers.txt
- genRand(num)
- numArray = readNum()
- numAverage = avgNum(numArray)
- maxNum= maximum(numArray)
- minNum = minimum(numArray)
- #generating random numbers based on the input in the main() funtion
- def genRand(num):
- with open('myRandomNumbers.txt', 'w') as file:
- for count in range(num):
- individualrandomnum = random.randint(1,500)
- file.write(str(individualrandomnum) + '\n')
- #program reads the numbers and prints them
- def readNum():
- numArray = []
- with open('myRandomNumbers.txt', 'r') as file:
- lines = file.readlines()
- for line in lines:
- numArray.append(str(line) + ' ')
- print ("The random numbers generated are:" + '\n' + (str(lines)))
- return numArray
- #funtion for finding the average
- def avgNum(numArray):
- #[one, two, three, four]
- total = 0
- for num in numArray:
- #all the numbers added together
- total += int(num)
- #total = 10
- #the average number equation
- avgNumber = total / len(numArray)
- print("\n" + "Total of the random numbers: " + str(total))
- print("Count of the random numbers is: " + str(len(numArray)))
- print('Average of the random numbers:' + f'{avgNumber:.2f}')
- return avgNumber
- #function to find the highest number in the array
- def maximum(num):
- maximum = num[0]
- for a in num:
- if a > maximum:
- maximum = a
- print("The largest of the random numbers: " + str(maximum))
- return maximum
- #function to find the lowest number in the array
- def minimum(num):
- minimum = num[0]
- for a in num:
- if a < minimum:
- minimum = a
- print("The smallest of random numbers: " + str(minimum))
- return minimum
- if __name__ == '__main__':
- main()
- #determines if the function should be repeated
- continueLoop = input ("run program again 1=yes 0=no ")
Advertisement
Add Comment
Please, Sign In to add comment