1
votes

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
2
Don't use eval on 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, use int(). - Kevin
My poor grandmother, then how would I convert the value n to int while n is still able to == ' '? - D. Leigh
Store the string version of the input in an intermediary variable. Check that for "", and only afterwards attempt to convert to int. This may require you to rearrange your conditionals. - Kevin
@D.Leigh your indentation is off, check my example code #2 which works fine. - jermenkoo
I need the code to show the list of numbers entered and the sum of that list at the bottom of the output. No matter how I format it it won't seem to return properly - D. Leigh

2 Answers

0
votes

Again, like kevin said, don't use eval.

def myAvg():
    lst = []
    while True:
        n = raw_input('Enter an int between 10 and 50: ')
        if n=="":
            print lst
            break
        if 10<int(n)<50:
            lst.append(int(n))

myAvg()
0
votes

Here is your code, it tries to convert a number to integer and when an empty input is supplied, it captures the ValueError thrown and returns the list. I also merged the two conditions n < 10 and n > 50 into one.

def myAvg():
    lst = []

    while True:
        try:
            n = int(input("Enter a number: "))

            if n < 10 or n > 50:
                print("Please enter a number between 10 and 50")
            else:
                lst.append(n)
        except ValueError:
            print(lst)
            return sum(lst) / len(lst)

print(myAvg())

Output:

C:\Users\jermenkoo\Desktop>test.py
Enter a number: 10
Enter a number: 10
Enter a number: 1
Please enter a number between 10 and 50
Enter a number:
10.0

Most importantly, eval() is DANGEROUS and SHOULD NOT BE USED as it can execute arbitrary input such as __import__('os').system(<your command here>). If command is rm, you might find all your files deleted.

Other possibility is to check whether input supplied is equal to '' such as here:

def myAvg():
    lst = []

    while True:
        n = input("Enter a number: ")

        if n == '':
            print(lst)
            return sum(lst) / len(lst)
        else:
            temp = int(n)

            if temp < 10 or temp > 50:
                print("Please enter a value between 10 and 50.")
            else:
                lst.append(temp)

print(myAvg())

Calculating average of the list is then an easy task - sum(lst) / len(lst). sum(lst) returns the sum of the numbers in the list, len(lst) returns the number of elements.

Since you seem to be using Python 2.x, here is the code modified:

def myAvg():
    lst = []

    while True:
        n = raw_input("Enter a number: ")

        if n == '':
            return 1.0 * sum(lst) / len(lst)
        else:
            temp = int(n)

            if temp < 10 or temp > 50:
                print("Please enter a value between 10 and 50.")
            else:
                lst.append(temp)

print(myAvg())