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 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:
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".
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...
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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)....
|
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.
...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |