beginner in python, if statement -
score = raw_input ("enter digits between 0.0 and 1.0") float (score) #if score & Lt; 0.0: # Print "Invalid Input." #exit #elif Score & gt; 1.0: # Print "Invalid Input." #exit If Score & gt; = 9: Print "A" Alive Score & gt; = .8: Print "B" Alive Score & gt; = .7: Print "C" Alif Score & gt; = 6: print "D" alleph score & lt; .6: Print "F" and: Print "Invalid Input."
I'm having trouble with Python code; Some of my initials seem to be wrong with the statement but I do not know if it does not matter what the input I test it, it always gives A as the output. Any sign that I am doing wrong, would be very good, sorry for such a simple question, but I started learning the dragon, I am stuck on this code for more than an hour and I do not understand for my life, why it is not working Thanks
You float ()
call < score From
scores
:
score = float (score)
score
as a string Begins, is an immutable object, and it will not be transferred. Returns float ()
a new float object, and you are ignoring that return value.
5 if
statement, you can use one value to map to a sheet:
import bisect score = float (Raw_input ("Enter the digits between 0.0 and 1.0:")) score = [0,. 6, .7, .8 ,. 9] grade = ['F', 'D', 'C', 'B', 'A'] print grade [bisect.bisect (score, score) - 1]
For any given score, bisect.bisect ()
will return the entry point after to the nearest or similar value; Therefore . For 9
this will return 5
, to enter the value at the end, but for some (but not included) for 0
0.6
This will return 1
.
Subtract all you have to do again 1 and get the corresponding grade instead of the grade
list.
Demo:
& gt; & Gt; & Gt; Import bisector & gt; & Gt; & Gt; Score = [0, .6, .7, .8,. 9] & gt; & Gt; & Gt; Grade = ['F', 'D', 'C', 'B', 'A']> and gt; & Gt; & Gt; Bisect.bisect (Score, .75) 3> and gt; & Gt; Grade [bisect.bisect (score, .75) - 1] 'C' & gt; & Gt; & Gt; Grade [bisect.bisect (score, .25) - 1] 'f' & gt; & Gt; & Gt; Grade [bisect.bisect (score, 9.9) - 1] 'a'
Comments
Post a Comment