Make a program that asks the user for a series of numbers until they either want to output the average or quit the program.

To complete this task, the programme needs to keep asking the user to input numbers, until they average or quit # this line initally takes in the input of the user number = input("Enter a number (input 'a' to average and 'q' to quit):") # the user inputted numbers need to be stored together in an array # an empty array is initialised for this purpose nums = [] # if the number is not 'a' (meaning take an average) and not 'q' for quit, the while loop is entered # this while loop continues asking for input and storing it until the user either asks for the average or quits while (number != 'a') and (number != 'q'):  #  the user input is stored in the array  nums.append(float(number))  #  ask for another input  number = input("Enter a number (input 'a' to average and 'q' to quit):") # if the user inputs a or q, the programme comes out of the while loop and is here # if the user inputs a the average is computed if number == 'a':  #  an edge case if no numbers have been inputted so far  if nums == []:    print('You have not inputted any numbers yet, please start the programme again')  #  if numbers have been inputted - calculate mean  else:    #  calculate the mean, do total/length of the array    total = sum(nums)    length = len(nums)    average = total/length    print ("average: ", round(average,2)) # else, if q was entered, quit elif number == 'q':  print ('You have quit the programme') 

SM
Answered by Shabri M. Python tutor

11226 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Write a function that takes an imput (call it usr_in), the user's age (usr_age) and the user's name (usr_nm) and outputs the following string: "Hi, My name is [usr_nm], I am [usr_age] years old, and my favorite number is [usr_in]"


Write a simple number guessing game, give the user 3 tries to guess a number between 1 and 10


Which four data types are used in Python? Can you give an example of each?


Given a list of N numbers, use a single list comprehension to produce a new list that only contains those values that are: (a) even numbers, and (b) from elements in the original list that had even indices .


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences