1

I tried to do the first assignment from http://www.cplusplus.com/forum/articles/12974/

g = int(raw_input('Enter the grade you scored: '))
if g >= 90 & g <= 100:
 print 'Your grade is: A'
elif g >= 80 & g < 90:
 print 'Your grade is: B'
elif g >= 70 & g < 80: 
 print 'Your grade is: C'
elif g >= 60 & g < 70:
 print 'Your grade is: D'
elif g >= 50 & g < 60:
 print 'Your grade is: E' 
elif g >= 0 & g <= 49:
 print 'Your grade is: F'
else:
 print 'You can only enter an integer within the range of 0-100.'

The problem is that whenever I run this program, any number I input that is greater than 0 will get:

Your grade is: A

Any help greatly appreciated. Thanks!

asked Aug 26, 2015 at 1:01

4 Answers 4

4

Bi Rico's answer is simple and correct. To explain the situation further:

  • The & operator computes the bitwise AND of two integers. For example, 5 & 3 == 1.
  • The precedence of & is above the comparison operators (such as < and >=). So a < b & c < d actually means a < (b & c) < d.
  • Python allows chained comparisons. For example, a < b == c >= d translates into a < b and b == c and c >= d.

Putting these facts together, this is what happens when you run your program:

  • Assume that g is assigned an integer value between 0 and 100, inclusive.
  • So the if-test g >= 90 & g <= 100 means (g >= (90 & g)) and ((90 & g) <= 100).
  • Bitwise AND is always smaller than or equal to both arguments (i.e. (a & b <= a) and (a & b <= b)).
  • Thus 90 & g <= g is always true. Likewise, 90 & g <= 100 is always true, because 90 <= 100.
  • Therefore the first if-test is always true, so the body will execute and the elif/else clauses will never execute.
answered Aug 26, 2015 at 1:10
Sign up to request clarification or add additional context in comments.

Comments

2

The problem in your code is that you're using & when you want to use and, try this:

if g >= 90 and g <= 100:
 print 'Your grade is: A'
...
answered Aug 26, 2015 at 1:04

1 Comment

Well that was easy. Thank you!
0

The code you had their was spot on but syntax is very important in Python. You want to use and where you have & like this:

g = int(raw_input('Enter the grade you scored: '))
if g >= 90 and g <= 100:
 print 'Your grade is: A'
elif g >= 80 and g < 90:
 print 'Your grade is: B'
elif g >= 70 and g < 80: 
 print 'Your grade is: C'
elif g >= 60 and g < 70:
 print 'Your grade is: D'
elif g >= 50 and g < 60:
 print 'Your grade is: E' 
elif g >= 0 and g <= 49:
 print 'Your grade is: F'
else:
 print 'You can only enter an integer within the range of 0-100.'

This will display what you need the correct way.

answered Aug 26, 2015 at 2:04

Comments

-1

Using the correct keyword is always important as certain things can cause errors. Use and, not &.

answered Aug 26, 2015 at 1:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.