473,395 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Simple integer comparison problem

tom
Hi!
I ran in problem with simple exercise. I'm trying to get program to
return grade when given points but no matter what, I always get F.

def grader():
print "Insert points: "
points = raw_input('')
int(points)

if points 89 and points <= 100:
return "A"
elif points 89 and points <= 89:
return "B"
elif points 69 and points <= 79:
return "C"
elif points 59 and points <= 69:
return "D"
else:
return "F"

grade = grader()
print grade
Apr 14 '07 #1
5 4538
On Apr 14, 5:19 pm, t...@finland.com wrote:
Hi!
I ran in problem with simple exercise. I'm trying to get program to
return grade when given points but no matter what, I always get F.

def grader():
print "Insert points: "
points = raw_input('')
int(points)

if points 89 and points <= 100:
return "A"
elif points 89 and points <= 89:
return "B"
elif points 69 and points <= 79:
return "C"
elif points 59 and points <= 69:
return "D"
else:
return "F"

grade = grader()
print grade
You should write:
points = int(points)

int returns value, not change in place.

If I can suggest you can use simpler if-statement:
if 89 < points <= 100:

Apr 14 '07 #2
On Apr 14, 10:19 am, t...@finland.com wrote:
Hi!
I ran in problem with simple exercise. I'm trying to get program to
return grade when given points but no matter what, I always get F.

def grader():
print "Insert points: "
points = raw_input('')
int(points)

if points 89 and points <= 100:
return "A"
elif points 89 and points <= 89:
return "B"
elif points 69 and points <= 79:
return "C"
elif points 59 and points <= 69:
return "D"
else:
return "F"

grade = grader()
print grade
You have a typo in the first "elif": "points 89 and points <= 89" is
never true, so you'll get an "F" instead of a "B".

BTW, Python lets you write things like "80 <= points < 90".

Apr 14 '07 #3
tom
Thanks for help!
Apr 14 '07 #4
if points 89 and points <= 100:
return "A"
elif points 89 and points <= 89:
return "B"
elif points 69 and points <= 79:
return "C"
elif points 59 and points <= 69:
return "D"
else:
return "F"
The previous posters already pointed out your int problem. However, the
if-statement can be written with a lot less clutter:

if points 100:
return "Illegal score"
elif points 89:
return "A"
elif points 79:
return "B"
elif points 69:
return "C"
elif points 59:
return "D"
else:
return "F"

I have a feeling that there's a Python-solution that is shorter yet
better readable, I just can't figure it out yet...
Apr 14 '07 #5
Bart Willems wrote:
I have a feeling that there's a Python-solution that is shorter yet
better readable, I just can't figure it out yet...
Shorter (and faster for big lists): Yes. More readable: I don't know, I
guess that depends on ones familiarity with the procedure.

import bisect

def grader(score):
c = bisect.bisect([60,70,80,90],score)
return 'FDCBA'[c]

A.
Apr 14 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andreas Paasch | last post by:
I'm attempting to trigger things based on time. I have one shop that has opening hours, closing hours and lunch hours store as full hour values, integer, in MySQL. I retrieve them, based on...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
5
by: bruce.james.lee | last post by:
hi i have a problem with integer subtraction in C. printf("%d", c < (a - b)); a is got from a #define and is 0x80000000 and b is got from input and is also 0x80000000. c is ffffffff (-1)....
13
by: RadiationX | last post by:
I have to solve the following problem:Write a program that accepts two integers, and determines if the second is a factor (is evenly divisible into) the first. Here is the code i have so far. ...
18
by: Bob Cummings | last post by:
Not sure if this is the correct place or not. Anyhow in school we were taught that when trying to calculate the efficiency of an algorithm to focus on something called FLOPs or Floating Point...
7
by: db | last post by:
Hi@all Just got a comparison problem with javascript. I want to compare : document.getElementById(currentID).getAttribute("style") with a string, e.g: "background-color: lightgreen;" They...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
20
by: chutsu | last post by:
I'm trying to compare between pointer and integer in an "IF" statement how do I make this work? if(patient.id != NULL){ } Thanks Chris
6
by: lorlarz | last post by:
Regarding http://mynichecomputing.com/digitallearning/yourOwn.htm and how to make it fail when it should not with an integer OR parseInt to integer conversion problem. THE real problem IS is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.