# Simple but Problematic for beginners

Page 1 of 1

## 10 Replies - 756 Views - Last Post: 13 August 2012 - 09:06 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=288704&amp;s=3e0791274da3e25a08b1965a302bf308&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 EmiT FliX

• New D.I.C Head

Reputation: 0
• Posts: 48
• Joined: 05-August 12

# Simple but Problematic for beginners

Posted 12 August 2012 - 08:26 PM

```mark = int(raw_input("Mark:"))
if mark < 50:
print "Grade = F"
if mark >= 50 < 64:
print "Grade = P"
if mark >= 65 < 74:
print "Grade = CR"
if mark >= 75 < 84:
print "Grade = D"
if mark >= 85 < 100:
print "Grade = GOD DAMN BOI!!!!"

```

This doesn't print out right
say this happens
Mark: 66
this will print out as
Grade = P
Grade = CR

Now I have learnt that python is something where you throw every bit of logic you know away...

Anyways why is this printing wrong and how can it be fixed

Is This A Good Question/Topic? 0

## Replies To: Simple but Problematic for beginners

### #2 alexr1090

• D.I.C Head

Reputation: 44
• Posts: 125
• Joined: 08-May 11

## Re: Simple but Problematic for beginners

Posted 12 August 2012 - 09:26 PM

Your logic is good. Python wants you to write it like this
```if mark >=50 and mark < 64:
```
though.
Was This Post Helpful? 0

### #3 EmiT FliX

• New D.I.C Head

Reputation: 0
• Posts: 48
• Joined: 05-August 12

## Re: Simple but Problematic for beginners

Posted 12 August 2012 - 09:46 PM

```mark = int(raw_input("Mark:"))
if mark < 50:
print "Grade = F"
else:
if mark >= 50 and mark <= 65:
print "Grade = D"

if mark >= 65 and mark <= 75:
print "Grade = H"
else:
if mark >= 75 and mark <= 85:
print "Grade = HD"

```

Now all I get is a response back from the first ELSE
Was This Post Helpful? 0

### #4 alexr1090

• D.I.C Head

Reputation: 44
• Posts: 125
• Joined: 08-May 11

## Re: Simple but Problematic for beginners

Posted 12 August 2012 - 09:59 PM

change the first else to elif and get rid of a tab below the last else
Was This Post Helpful? 0

### #5 EmiT FliX

• New D.I.C Head

Reputation: 0
• Posts: 48
• Joined: 05-August 12

## Re: Simple but Problematic for beginners

Posted 12 August 2012 - 10:05 PM

Syntax error on the elif
Was This Post Helpful? 0

### #6 alexr1090

• D.I.C Head

Reputation: 44
• Posts: 125
• Joined: 08-May 11

## Re: Simple but Problematic for beginners

Posted 12 August 2012 - 10:14 PM

oh yeah sorry I didn't look close enough. There has to be a statement after an elif. So basically
```mark = int(raw_input("Mark:"))
if mark < 50:
print "Grade = F"
elif mark >= 50 and mark <= 65:
print "Grade = D"

elif mark >= 65 and mark <= 75:
print "Grade = H"
elif mark >= 75 and mark <= 85:
print "Grade = HD"

```

So it's if mark <50: do this and go to past the last elif statement (in this case the end of the program). If mark isn't <50 but is >=50 and <=65 print grade d. Otherwise if mark is >=65 and <=75 print grade=H. Otherwise if mark >=76 and mark <=85 print grade = hd. Making sense now?
Was This Post Helpful? 0

### #7 EmiT FliX

• New D.I.C Head

Reputation: 0
• Posts: 48
• Joined: 05-August 12

## Re: Simple but Problematic for beginners

Posted 12 August 2012 - 10:16 PM

Yes, that makes perfect sense and thanks for the code, saved alot of time, I'm just reading up on elif
Was This Post Helpful? 0

### #8 alexr1090

• D.I.C Head

Reputation: 44
• Posts: 125
• Joined: 08-May 11

## Re: Simple but Problematic for beginners

Posted 12 August 2012 - 10:20 PM

no problem glad I could help.
Was This Post Helpful? 0

### #9 baavgai

• Dreaming Coder

Reputation: 6071
• Posts: 13,152
• Joined: 16-October 07

## Re: Simple but Problematic for beginners

Posted 13 August 2012 - 04:23 AM

Now that you have the syntax, note that you don't two value test.
```if mark < 50:
print "Grade = F"
# here, you already know mark >= 50
# because you know that "not mark < 50"
# you can take advantage of this
elif mark >= 50 and mark <= 65:

```

e.g.
```if mark < 50:
print "Grade = F"
elif mark < 64:
print "Grade = P"
elif mark < 74:
print "Grade = CR"
elif mark < 84:
print "Grade = D"
elif mark < 100:
print "Grade = GOD DAMN BOI!!!!"
else:
# if we make it here, mark >=100
print "No one is that good."

```

Was This Post Helpful? 0

### #10 EmiT FliX

• New D.I.C Head

Reputation: 0
• Posts: 48
• Joined: 05-August 12

## Re: Simple but Problematic for beginners

Posted 13 August 2012 - 04:29 AM

Yes, I was having trouble with this for a very long time atleast 3 or 4 hours , but it was dumbed down enough for me to understand, and tomorrow, help others having the problem, though I do like your code usage
Was This Post Helpful? 0

### #11 atraub

• Pythoneer

Reputation: 789
• Posts: 2,106
• Joined: 23-December 08

## Re: Simple but Problematic for beginners

Posted 13 August 2012 - 09:06 AM

alexr1090, on 13 August 2012 - 12:26 AM, said:

Your logic is good. Python wants you to write it like this
```if mark >=50 and mark < 64:
```
though.

I do want to point this out while I'm here. if mark >= 50 < 64 is wrong but not as wrong as you think. this condition checks to see if mark is greater than or equal to 50 and if 50 is less than 64. The latter will always equate to true, when will 50 ever be greater than 64? This syntax is correct and does what you had in mind
```if 50 <= mark < 64
```

For the narrow set of situations where it's appropriate, this syntax is much cleaner... if 50 is less than or equal to mark and mark is less than 64. Granted, baagvais approach is better, but I wanted you to be aware that this syntax exists.

This post has been edited by atraub: 13 August 2012 - 04:28 PM

Was This Post Helpful? 1

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }