Complete beginner with Elif [Python] - python

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!

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.

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'
...

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.

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

Related

Python if/elif statement not working correctly

Please help understand what is wrong with the code below. The code works fine if I pass values up to 34. Once I pass 35 or higher, the output is incorrect.
tuk=0
if tuk <= 24:
print ('The text is very easy to read.')
elif tuk >= 25 & tuk <= 34:
print('The text is easy to read.')
elif tuk >= 35 & tuk <= 44:
print('The text is moderately difficult to read.')
elif tuk >= 45 & tuk <= 54:
print('The text is difficult to read')
elif tuk >= 55:
print('The text is very difficult to read')
else:
print('This is beyond')
Your code is correct, but changing the "&" to "and" because "&" is a bitwise logical operator, not a logical one.
For example:
z = 0 := 0b000
a = 1 := 0b001
b = 2 := 0b010
c = 3 := 0b011
d = 4 := 0b100
now the result of the expression (a & c) is 0b001 or 1
but in logical expression (a and c) the result will be true, which I suppose you are interested in.

Beginner in Python: if statement is not returning print function

I recently tried to make a love calculator with this code. However, when faced with a name that has over 11 characters in common for either 'love' or 'true' it does not return the proper statement. For example, if I get 711 returned because the 'love' statement is over 9, it just gives me the 'else' option instead of the => 90 statement. I'm not sure what I did wrong. Thank you for any help in advance!
print("Welcome to the Love Calculator!")
name1 = input("What is your name? \n")
name2 = input("What is their name? \n")
combined_names = str(name1.lower()) + str(name2.lower())
t = combined_names.count('t')
r = combined_names.count('r')
u = combined_names.count('u')
e = combined_names.count('e')
l = combined_names.count('l')
o = combined_names.count('o')
v = combined_names.count('v')
e = combined_names.count('e')
Love = l + o + v + e
true = t + r + u + e
truelove = int(str(true) + str(Love))
if truelove <= 10 and truelove >= 90:
print(f"Your score is {truelove}, you go together like coke and mentos")
elif truelove >= 40 and truelove <= 50:
print(f"Your score is {truelove}, you are alright together")
else:
print(f"Your score is {truelove}")
truelove <= 10 and truelove >= 90
Will always give false and not pass this if statement.
To be able to run it you can try.
truelove >= 10 and truelove <= 90
EDIT: I saw that your elif statement will never work because first if statement is wider range. So flipping the if statements will fix it.
if truelove >= 40 and truelove <= 50:
print(f"Your score is {truelove}, you are alright together")
elif truelove >= 10 and truelove >= 90:
print(f"Your score is {truelove}, you go together like coke and mentos")
else:
print(f"Your score is {truelove}")```

Why am I getting this message invalid syntax?

i write code if statement but there is a wrong in it .
this is the code
mark = float(input('enter your mark : '))
if mark < 50:
result = 'failed'
elif mark >= 50 and < 75:
result = 'accepted'
elif mark >= 75 and < 85:
result = 'good'
elif mark >= 85 and < 90:
result = 'very good'
else:
result = 'excellent'
print(result)
the message appear is invalid syntax in line 4 about < assignment
any help here guys?
The proper syntax is either elif mark >= 50 and mark < 75: or elif 50 <= mark < 75:
mark >= 50 and < 75 is not a valid expression, you must write mark >= 50 and mark < 75 instead. Alternatively, you can use a chained comparison: 50 <= mark < 75.
This would be your code that actually runs:
mark = float(input('enter your mark : '))
if mark < 50:
result = 'failed'
elif mark >= 50 and mark < 75:
result = 'accepted'
elif mark >= 75 and mark < 85:
result = 'good'
elif mark >= 85 and mark < 90:
result = 'very good'
else:
result = 'excellent'
print(result)
As others stated mark >= 50 and < 85 is not valid in Python.

greater than but less than function in python usage

while I was studding python I run into this problem that I'm trying to filter if a number is bigger than n and smaller than x
in other thread I read that you could just do this:
if 10 < a < 20:
whatever
but when I run the code Im getting invalid syntax
while guess != rightnum:
guess=int(input('your guess: '))
diff= abs(guess - rightnum)
if guess > rightnum and diff >= 1000 :
print(random.choice(muchless))
elif guess > rightnum and 1000 > diff >= 100
print(random.choice(less))
elif guess > rightnum and diff < 100
print(random.choice(fewless))
your elif statements don't end with :!
You missed 2 colons
while guess != rightnum:
guess=int(input('your guess: '))
diff= abs(guess - rightnum)
if guess > rightnum and diff >= 1000 :
print(random.choice(muchless))
elif guess > rightnum and 1000 > diff >= 100 :
print(random.choice(less))
elif guess > rightnum and diff < 100 :
print(random.choice(fewless))

Wondering why I keep receiving errors

I'm getting confused with functions as i have just tried to employ the if and else
functions and i can't get them working...
def instructions():
print ("Hello, and welcome to the programme")
def getInput(b):
b=int(input("Enter how many bags of Java coffe you have purchased: "))
if b <= 25:
bonus = b + 0:
if b >= 25:
else:
b <= 49:
bonus = b * 0.05:
if b <= 50:
else:
b >= 99:
bonus = b * 0.1:
if b >= 100:
bonus = b * 0.2:
...
instructions()
...
print("")
...
getInput()
the else on the second line is coming up as an error and so to id the b near the top.
Any help would be greatly appreciated.
Thanks.
Let me help you out a bit
if b <= 25:
bonus = b + 0:
elif b >= 25 and b <= 49:
bonus = b * 0.05:
elif b >= 50 and b <= 99:
bonus = b * 0.1:
else:
bonus = b * 0.2:
Should give you more of what you expect.
Also you will have issues with your get input it is expecting an argument when it should be returning one it should look more like this:
def getInput():
return int(input("Enter how many bags of Java coffe you have purchased: "))
You'll probably want to indent all of the bonus code into a function of its own and pass the return from getInput into it
Your code indentation is incorrect - this is important in python; it is part of the syntax.
The logic could do with a bit of cleaning up too;
def instructions():
print ("Hello, and welcome to the programme")
def getInput(b):
b=int(input("Enter how many bags of Java coffe you have purchased: "))
bonus = 0:
if 25 < b <= 50:
bonus = b * 0.05
elif b <= 99:
bonus = b * 0.1
elif b >= 100:
bonus = b * 0.2:
return b, bonus
...
instructions()
...
bagsBought, bonus = getInput()
print "You bought %i, and got %i bonus bags!" % (bagsBounght, bonus)

Categories