my code works just fine in terms of what I want it to print, but somehow it prints fs three times. So instead of for exampling printing A just once, it prints A tree times, each on a separate line. Why?
sr = input("Enter score: ")
for i in (sr):
try:
fs = float(sr)
if fs > 1.0: print("decimal only")
elif fs >= 0.9: print("A")
elif fs >= 0.8: print("B")
elif fs >= 0.7: print("C")
elif fs >= 0.6: print("D")
elif fs < 0.6: print("F")
except:
print("Bad score")
sr = input("Enter score: ")
continue
If user input is for example 0.8, it should just print B.
Right now you are not processing user input, therefore when you for loop over a string, you loop over the letters one by one therefore one for each ".08", You should process your input by first splitting by spaces (if you are expecting multiple inputs separated by spaces) and cast them to an integer.
It is not clear why you want to for loop over your input, if you are expecting a single input there is no need, if you want multiple inputs you must decide if you want them all input before processing or one at a time ( which would require a while loop).
sr = input("Enter score: ")
sr = sr.split(' ')
for i in (sr):
i = int(i)
try:
fs = float(sr)
if fs > 1.0: print("decimal only")
elif fs >= 0.9: print("A")
elif fs >= 0.8: print("B")
elif fs >= 0.7: print("C")
elif fs >= 0.6: print("D")
elif fs < 0.6: print("F")
except:
print("Bad score")
sr = input("Enter score: ")
continue
Why are u typing : -
for i in (sr): #
This will lead to loopind till ur digits example for 0.8 - three times for 0.88 - Four times for 0.888 - Five times instead do this kind of stuffs.
sr = input("Enter score: ")
if sr>=0 :
try:
fs = float(sr)
if fs > 1.0: print("decimal only")
elif fs >= 0.9: print("A")
elif fs >= 0.8: print("B")
elif fs >= 0.7: print("C")
elif fs >= 0.6: print("D")
elif fs < 0.6: print("F")
except:
print("Bad score")
sr = input("Enter score: ")
continue
There's no need to use a for-loop unless you want the program to run continuously after evaluating the user input. If you do then just adding a while-loop would work.
while True:
try:
sr = input("Enter score: ")
fs = float(sr)
if fs > 1.0: print("decimal only")
elif fs >= 0.9: print("A")
elif fs >= 0.8: print("B")
elif fs >= 0.7: print("C")
elif fs >= 0.6: print("D")
elif fs < 0.6: print("F")
except:
print("Bad score")
sr = input("Enter score: ")
Related
score = input("What is your score? ")
if score == str(100):
print('Perfect!')
elif score <= str(range(95, 99)):
print('Great!')
elif score <= str(range(90, 95)):
print('Good')
else:
print('Fail')
It works when I type 95 to 100, but it doesn't work when I type other numbers.
Use ints to compare numbers, not strings:
score = int(input("What is your score? "))
if score == 100:
print('Perfect!')
elif score in range(95, 100): # This 100 catches the 99 case
print('Great!')
elif score in range(90, 95):
print('Good')
else:
print('Fail')
I'm learning python, and I'm trying with a set of exercises, but I'm stuck on the next one:
inp_1 = input('your score between 0.0 and 1.0')
try:
score = float(inp_1)
if 0.9 <= score <= 1.0:
print ("A") elif score >= 0.8:
print ("B") elif score >= 0.7:
print ("C") elif score >= 0.6:
print ("D") elif score < 0.6:
print ("Your grade is an F")
else:
print ('your score is more than 1.0')
except:
print ('invalid input, please try with a number')
but I'm get the next message error:
IndentationError: unindent does not match any outer indentation level on line 7 elif score >= 0.8: ^ in main.py
The indentation (= number of tabs / spaces in front of each line) is important in python. The code you posted isn't indented properly. A correct indentation would look like this:
inp_1 = input('your score between 0.0 and 1.0')
try:
score = float(inp_1)
if 0.9 <= score <= 1.0:
print ("A")
elif score >= 0.8:
print ("B")
elif score >= 0.7:
print ("C")
elif score >= 0.6:
print ("D")
elif score < 0.6:
print ("Your grade is an F")
else:
print ('your score is more than 1.0')
except:
print ('invalid input, please try with a number')
The first line is always un-indented. When starting a block (e.g. try:, if:, elif:, ...), all following lines that belong within this block are indented with 4 spaces more than the opening line. "Closing" a block is done by writing the next statement with less indentation.
Another example:
if False:
print(1)
print(2)
# prints nothing because both print statements are part of the if condition
if False:
print(1)
print(2)
# prints 2 because the `print(2)` statement is not part of the if condition
Does this answer your question?
Your indentations should be like this:
inp_1 = input('your score between 0.0 and 1.0')
try:
score = float(inp_1)
if 0.9 <= score <= 1.0:
print ("A")
elif score >= 0.8:
print ("B")
elif score >= 0.7:
print ("C")
elif score >= 0.6:
print ("D")
elif score < 0.6:
print ("Your grade is an F")
else:
print ('your score is more than 1.0')
except:
print ('invalid input, please try with a number')
I think you didn't understand indentations fully. This isn't like any other languages. You need to make indentations correctly.
Hope it will help you 😃
I am trying to write a program for this assignment:
Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error. If the score is between 0.0 and 1.0, print a grade using the following table:
Score Grade
>= 0.9 A
>= 0.8 B
>= 0.7 C
>= 0.6 D
< 0.6 F
If the user enters a value out of range, print a suitable error message and exit.
But it doesn't print the sentence.
try:
inp = raw_input("Enter Score: ")
score = float(inp)
except:
print "Please enter a score number between 0.0 and 1.0"
quit()
if score >= 0.9 :
print "A"
elif score >= 0.8 :
print "B"
elif score >= 0.7 :
print "C"
elif score >= 0.6 :
print "D"
elif score < 0.6 :
print "F"
else:
print "Your score number is not in the 0 - 1 range."
score = input("Enter Score: ")
scor = float(score)
if 0.0 < scor > 1.0:
print("error")
elif scor >= 0.9:
print("A")
elif scor >= 0.8:
print("B")
elif scor >= 0.7:
print("C")
elif scor >= 0.6:
print("D")
elif scor < 0.6:
print("F")
You should raise the exception to get it printed. Here is the code changes that you are looking for.
try:
inp = raw_input("Enter Score: ")
score = float(inp)
if score > 1.0 or score < 0
raise
except:
print "Please enter a score number between 0.0 and 1.0"
quit()
if score >= 0.9 :
print "A"
elif score >= 0.8 :
print "B"
elif score >= 0.7 :
print "C"
elif score >= 0.6 :
print "D"
elif score < 0.6 :
print "F"
else:
#your logic
Hope this was helpful.
Pavan, your code doesn't pass the exam, because you accept values which are higher than 1.0 or smaller than 0.0 which is incorrect.
Add a check somewhere which validates that the score is between 0.0 and 1.0.
So like I highlighted in the comment section please make the modifications to your if statement like below:
try:
inp = raw_input("Enter Score: ")
score = float(inp)
except:
print "Please enter a score number between 0.0 and 1.0"
quit()
# change your if statement here:
if score > 1.0 or score < 0.0:
print "Your score number is not in the 0 - 1 range."
elif score >= 0.9 :
print "A"
elif score >= 0.8 :
print "B"
elif score >= 0.7 :
print "C"
elif score >= 0.6 :
print "D"
elif score < 0.6 :
print "F"
EDIT: First of all the if branch checks for an out of bound score which is a value above 1.0 and below 0.0, then prints an error message.
If the score is within limits than the other elif blocks check to value of the score as in the original post.
inp = input("Enter score between 0.0 and 1.0:")
score = float(inp)
if score >= 0.9 and score <= 1.0:
print("A")
if score >= 0.8 and score < 0.9:
print("B")
if score >= 0.7 and score < 0.8:
print("C")
if score >= 0.6 and score < 0.7:
print("D")
if score >= 0.0 and score <0.6:
print("F")
elif score > 1.0:
print("Error, please enter score between 0.0 and 1.0")
quit()
scr=(input("Enter the Score: "))
try:
scr=float(scr)
if scr>=0.0 and scr<=1.0:
if scr >= 0.9:
print("A")
elif scr >= 0.8:
print("B")
elif scr >= 0.7:
print("C")
elif scr >= 0.6:
print("D")
elif scr<0.6:
print("F")
else:
print("Out Of range")
except:
print("Try a number")
Explanation:
scr is the score which will take float as input ranging from 0.0 to 1.0. If the score is less than 0.0 or greater than 1.0 then it prints "Out of Range". If entered input is not float/ int then it will print "Try a Number".
inp = input("Enter a Score between 0.0 and 1.0")
score = float(inp)
if score >= 0.9 and score <=1.0:
print ('A')
elif score >= 0.8 and score <0.9:
print ('B')
elif score >= 0.7 and score <0.8:
print ('C')
elif score >= 0.6 and score <0.7:
print ('D')
elif score >= 0.0 and score <0.6:
print ('F')
elif score > 1.0 or score < 0.0:
print ("Error, the score should be in the range between 0.0 and 1.0")
quit()
I am working on a python lettering assignment.
90 or above is an A and so on and so on for the rest of the letter grades; but when a value is inputted as a negative number, I need the code to do nothing other than display an error.
This is what i tried so far:
#Design a Python program to assign grade to 10 students
#For each student, the program first asks for the user to enter a positive number
#A if the score is greater than or equal to 90
#B if the score is greater than or equal to 80 but less than 90
#C if the score is greater than or equal to 70 but less than 80
#D if the score is greater than or equal to 60 but less than 70
#F is the score is less than 60
#Ihen the program dispalys the letter grade for this student.
#Use while loop to repeat the above grade process for 10 students.
keep_going = 'y'
while keep_going == "y":
num = float(input("Enter a number: "))
if num >= 90:
print("You have an A")
elif num >= 80:
print("You have an 3")
elif num >= 70:
print("You have an C")
elif num >= 60:
print("You have an D")
elif (num < 60 and <= 0:
print ("You have an F")
else:
print("lnvalid Test Score.")
Original screenshot
I see three problems, all in the same line:
elif (num < 60 and <= 0:
Syntax: num < 60 and <= 0 is not a valid expression; should be num < 60 and num <= 0
Logic: num <= 0 is not what you want, it should be num >= 0
Syntax: you missed a closing bracket ).
If you change those, it should work.
grade = int(input("Enter Score:"))
print "FFFFFDCBAA"[grade//10] if grade >= 0 else "ERROR!!!!"
you just have to change your elif for below 60.
keep_going = 'y'
while keep_going == "y":
num = float(input("Enter a number: "))
if num >= 90:
print("You have an A")
elif num >= 80:
print("You have an 3")
elif num >= 70:
print("You have an C")
elif num >= 60:
print("You have an D")
elif 60 > num >= 0:
print ("You have an F")
else:
print("lnvalid Test Score.")
I am trying to solve this grade input program. I need to input the grades between 0.6 to 1.0 and assign them a letter value. I can only use the if else method as we haven't gotten to the other methods yet in class...
score = raw_input("Enter Score Grade:")
sco = int(float(score))
if score < 0.60:
print "Your grade is an F"
elif score >= 0.6:
print "grade d"
elif score >= 0.7:
print "grade c"
elif score >= 0.8:
print "grade b"
elif score >= 0.9:
print "grade a"
else:
print "wrong score"`
You don't have to go highest to lowest score, you can also do this:
score = float(raw_input("Enter Score Grade:"))
if score < 0.60:
print "Your grade is an F"
elif score < 0.7:
print "grade d"
elif score < 0.8:
print "grade c"
elif score < 0.9:
print "grade b"
elif score <= 1.0:
print "grade a"
else:
print "wrong score"
If you do decide to check from highest to lowest, being consistent is a good practice. You can check your failing grade last:
score = float(raw_input("Enter Score Grade:"))
if score > 1:
print "wrong score"
elif score >= 0.9:
print "grade a"
elif score >= 0.8:
print "grade b"
elif score >= 0.7:
print "grade c"
elif score >= 0.6:
print "grade d"
else:
print "Your grade is an F"
As a reusable function :
def grade(score):
if score > 1:
return "wrong score"
if score >= 0.9:
return "grade a"
if score >= 0.8:
return "grade b"
if score >= 0.7:
return "grade c"
if score >= 0.6:
return "grade d"
return "Your grade is an F"
score = float(raw_input("Enter Score Grade:"))
print grade(score)
You should start from the largest grade first:
as you see 0.92 > 0.6 and also 0.92 > 0.9
But according to yout logic, it will satisfy the first if and will never reach last if.
Do something like this:
score = raw_input("Enter Score Grade:")
sco = int(float(score))
if score < 0.60:
print ("Your grade is an F")
elif score >= 0.9:
print ("grade a")
elif score >= 0.8:
print ("grade b")
elif score >= 0.7:
print ("grade c")
elif score >= 0.6:
print ("grade d")
else:
print ("wrong score")
You need to go from high grade to low grade. You need to change sco = int(float(score)) to score = float(score). It is not required to change it to int as you are comparing float
score = raw_input('Enter the Score')
score = float(score)
if score >= 0.9:
grade = 'A'
elif score >= 0.8:
grade = 'B'
elif score >= 0.7:
grade = 'C'
elif score >= 0.6:
grade = 'D'
else:
grade = 'F'
print 'Your Grade : ' + grade
You have to take not of the first condition. If the first condition is equal to 0.9, it will equate to false and give the grade of 'B'. Also, it will be wise to convert the input to int first
score = int(raw_input('Enter the Score'))
score = float(score)
if score > 0.9:
grade = 'A'
elif score >= 0.8:
grade = 'B'
elif score >= 0.7:
grade = 'C'
elif score >= 0.6:
grade = 'D'
else:
grade = 'F'
print 'Your Grade : ' + grade
It is therefore advisable to use the greater than or equal to so as to handle the situation when it is equal to the upper value(ie 0.9).
score = int(raw_input('Enter the Score'))
score = float(score)
if score >= 0.9:
grade = 'A'
elif score >= 0.8:
grade = 'B'
elif score >= 0.7:
grade = 'C'
elif score >= 0.6:
grade = 'D'
else:
grade = 'F'
print 'Your Grade : ' + grade