New to python, needing help as usual, I have to create a function that asks the user for a for an int between 10 and 50 if the number outside of the range is entered, print an error message and continue asking for input but if nothing is entered it must return a list of all the numbers the user inputted as well as the average of these numbers.
Example input/output:
myAvg()
Enter an int: 34
Enter an int: 43
Enter an int: 23
Numbers entered: 34 42 23
Average of list: 33
This is my code so far, unfinished to the point of getting it to return the list. I get an unexpected EOF while parsing if I'm not mistaken that is due to the eval(input)) but I'm not sure how to go about fixing this.
def myAvg():
lst = []
while True:
n = eval(input('Enter an int between 10 and 50: '))
if n < 10:
print('Please enter ant int between 10 and 50')
elif n > 50:
print('Please enter an int between 10 and 50')
elif n == '':
return lst
lst.append(n)
Edit: now recieving ValueError: invalid literal for int() with base 10: '' trying to only use things we covered in class
def myAvg():
lst = []
while True:
n = input('Enter an int between 10 and 50: ')
if int(n) < 10 or int(n) > 50:
print("Please enter an integer between 10 and 50")
elif n == '':
lst.append(int(n))
return lst
Edit2:
def myAvg():
lst = []
while True:
n = input("Enter an integer between 10 and 50: ")
if n == '':
print('Numbers entered:')
return lst
else:
ntemp = int(n)
if ntemp < 10 or ntemp > 50:
print("Please enter a value between 10 and 50.")
else:
lst.append(ntemp)
print('Average of numbers:')
return sum(lst) / len(lst)
What gets outputted:
Enter an integer between 10 and 50: 45
Average of numbers:
45.0
evalon user input. It gives your user the power to delete your hard drive and email embarrassing pictures to your grandmother. If you want to turn a string into an integer, useint(). - Kevin