0

Hiiii, thank you for all the answers! I've figured this out! I can't delete this message tho, and I'm not sure how to make it inactive. basically i am a mess. but thank you everyone!!!

Okay i am actually having 456344 problems, but this is my current one here is my code:

def averages():
    grade = 0
    x = eval(input("How many grades will you be entering? "))
    if type(x) != type(int(x)):
        x = eval(input("You can't enter that many grades. How many grades will you be entering? "))
     for i in range(x):
         y = eval(input("Please enter a grade between 0 and 100: "))
     if 0 <= y <= 100:
         grade = grade + y
     else:
         print("Your number is out of range!")
         y = eval(input("Please enter a grade between 0 and 100: "))
    average = grade/x
            print (y)
            print (x)
            print (grade)
            print (average)

    averages()

basically whenever i run the code this part doesn't work:

if 0 <= y <= 100:
    grade = grade + y

and it only calculates the last number entered in the average. Also, I'm supposed to make it give an error message if the number entered by the user is out of range (not between 0 and 100), but the error message isn't coming up? I'm not sure what's happening. Please help, thank you.

  • It only calculates the last number entered in the average because it is outside of `for` loop. – Ashwani Feb 02 '15 at 05:04
  • The indentation of your code is a bit wonky, so the `for` loops and `if` statements might not do what you want. Also, it's recommended to **not** use the `eval(input(prompt_string))` construction. Instead, get your input string and then convert it into an `int` (or `float` if required). – PM 2Ring Feb 02 '15 at 05:17

2 Answers2

0

As grade addition is outside for it considers last input only.

There are some indentation issues and eval issue. Most of all, don't use input() on Python 2 or eval() on Python 3. If you want integer numbers, use int() instead. Try this code :

def averages():
    grade = 0
    while True:
        try:
            x = int(raw_input("How many grades will you be entering? "))
        except:
            print "Input limit exceeds"
            continue
        else:
            break
    for i in range(x):
        y = int(raw_input("Please enter a grade between 0 and 100: "))
        if 0 <= y and y <= 100:
            grade = grade + y
        else:
            print("Your number is out of range!")
            y = int(input("Please enter a grade between 0 and 100: "))
    average = grade/x
    print (y)
    print (x)
    print (grade)
    print (average)

averages()
user123
  • 5,269
  • 16
  • 73
  • 121
  • @Alfie: I was in middle of writing answer. Please consider your voting – user123 Feb 02 '15 at 05:20
  • It's good that your code doesn't use `eval()` but please consider when `if type(x) != type(int(x)):` will be true... – PM 2Ring Feb 02 '15 at 05:30
  • @PM2Ring: is that fine now – user123 Feb 02 '15 at 05:38
  • Not really. If `x = int(input("How many grades will you be entering? "))` succeeds, `x` will be an `int`. If it doesn't succeed, the program will terminate with a `ValueError` exception. So the `type()` check will never be true. Really, that part of the code should be re-written using the techniques given in [this answer](http://stackoverflow.com/a/23294659/4014959). OTOH, handling that stuff wasn't part of the OP's question, so it's fair enough that your answer doesn't address that. – PM 2Ring Feb 02 '15 at 05:54
  • 1
    FWIW, it's recommended to avoid using `type()` like that; it's better to use `isinstance()` if you _really_ need to test object types, but it's even better to avoid type testing altogether, if possible, as it goes against the Python [duck-typing](http://en.wikipedia.org/wiki/Duck_typing) philosophy. – PM 2Ring Feb 02 '15 at 05:54
  • @PM2Ring: thanks for this info, I updated my answer. Now why does not it catch if input exceed `int` limit? – user123 Feb 02 '15 at 06:14
  • 1
    Your `try: ... except:` code doesn't follow the pattens in the answer I linked earlier. You shouldn't put code like `x = int(raw_input("You can't...` into the `except:` block as it could raise an exception, too. (you _could_ put it into another `try: ... except:` block, but that gets messy). Also, you should specify the exception (or exceptions) that the `except:` block handles, eg `except ValueError:`; a "naked" `except:` should only be used at the bottom of a chain of explicit exceptions when you _need_ to catch weird exceptions rather than allowing them to terminate the script. – PM 2Ring Feb 02 '15 at 06:27
  • 1
    Your code does not catch if the input exceeds the `int` limit because the `int()` function handles arbitrarily large integers; in Python 2 it automatically creates a `long` instead of a regular `int`, if necessary. – PM 2Ring Feb 02 '15 at 06:30
  • @PM2Ring: thanks for this info, so what is another way to bind user up to `int` limit only – user123 Feb 02 '15 at 06:41
  • You can get the highest value allowed in an `int` from `sys.maxint` (the smallest is `-1-sys.maxint`) , then use the techniques mentioned at http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – PM 2Ring Feb 02 '15 at 06:54
0

I have made some changes...

1) Replaced if-else statement with while

2) This condition checking makes sure to give out error message

def averages():
    grade = 0
    x = int(input("How many grades will you be entering? "))
    if type(x) != type(int(x)):
        x = int(input("You can't enter that many grades. How many grades will you be entering? "))
    for i in range(x):
        y = int(input("Please enter a grade between 0 and 100: "))
        while not (0 <= y <= 100):
            print("Your number is out of range!")
            y = int(input("Please enter a grade between 0 and 100: "))
        grade = grade + y

    average = grade/x
    print (y)
    print (x)
    print (grade)
    print (average)

averages()
Alfie
  • 2,706
  • 1
  • 14
  • 28
  • 1
    If you just need average to be an integer this would work fine.. else try using `float`. – Alfie Feb 02 '15 at 05:18