Python Forum
Absolute Beginner Python Question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Absolute Beginner Python Question
#1
I am trying to make a python code that contains if/else/elif statements that tells you what your grade is based on your score. So the program should respond according to this score table:
Score Grade
>= 0.9 A
>= 0.8 B
>= 0.7 C
>= 0.6 D
< 0.6 F

I wrote this code, but it keeps giving me a traceback error. I have no idea why and I've tried changing so much. I would appreciate help, thank you!

prompt = input('Enter a score between 0.0 and 0.1:\n')
score = input(prompt)
if score > 1 :
print('Not a valid score')
if score >= 0.9 :
print('A')
if score >= 0.8 :
print('B')
if score >= 0.7 :
print('C')
if score >= 0.6 :
print('D')
if score >= 0.0 :
print('F')
else :
print('Not a valid score')
score = input('Press enter to exit')
Reply
#2
Please put your code in Python code tags, and full error traceback message in error tags. You can find help here.
Reply
#3
Anyway, a few things...
input() will return a string you typed in. You need a number, so convert the string to number before you start doing comparisons in if statements.
You don't need another "input", one is enough, just make sure to convert data properly. Whether you do that in same line or on a separate is up to you.
You are not using any elif statements. The final "else" statement will belong only to the final "if" statement.
Reply
#4
j.crater is correct. The output from the Python input() statement is a string whereas your program needs the user to have entered a decimal number. As also pointed out, the first input() statement is redundant. I assume that you want to assign the string 'Enter a score between 0.0 and 0.1):/n' (you may want to re-evaluate this in light of the expected input), to the variable prompt, which should simply be done with the code
prompt = 'Enter a score between 0.0 and 0.1:\n'
Now the line
score = input(prompt)
will display the string and get the input from the user. The variable prompt though is really not needed as both lines can be combined as
score = input('Enter a score between 0.0 and 0.1:\n')
which will both display your message and transfer the input to the variable score. The input, however, is still a string, not the decimal number the rest of your code is expecting. The next step is to convert whatever is entered from type string into type float (a floating point or decimal number). Turning one type of variable into another is called casting and, in this case, can be done with the following code
score = float(score)
where the string score is recast as a float.
This can be done on a line by itself or combined with the input() statement to give
score = float(input('Enter a score between 0.0 and 0.1':\n'))
A word of warning though: In both cases whatever the user enters is cast as type float and if what is entered cannot be converted to a valid float, another error will be generated - so the input part of your program needs to be more robust than just using the float() statement by itself.
Correcting the input() part and rerunning gives the output:

Enter a score between 0.0 and 0.1:
9.4
Not a valid score
A
B
C
D
F
Press enter to exit


which is not what you wanted. You are assuming that if an if statement is true then it will, somehow, skip the rest of the if tests which is not the case. For a series of related but mutually exclusive if statements you need to use the if - elif - else construct where the statements are only carried out if the one before is false and the else statement is now only carried out if all the if statements are false.
I am sure you can continue from this point. I can't see the reason for the third input statement - the program finishes at this point anyway. If you thought it would loop, you will have to do a bit more work.
I hope this helps.
Reply
#5
@wookie can you use inline tag when you highlight word.
Because color may not look ok on other Theme.
You can check out Theme down in right corner.
Reply
#6
@snippsat - OK thanks for that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Absolute paths in subprocess - file not found kittyticker 4 397 Jan-28-2024, 10:37 PM
Last Post: kittyticker
  A simple "If...Else" question from a beginner Serena2022 6 1,638 Jul-11-2022, 05:59 AM
Last Post: Serena2022
Question Beginner Boolean question [Guessing game] TKB 4 2,226 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Beginner question NameError amazing_python 6 2,378 Aug-13-2021, 07:28 AM
Last Post: amazing_python
  automatically get absolute paths oclmedyb 1 2,061 Mar-11-2021, 04:31 PM
Last Post: deanhystad
  Beginner question - storing values cybertron2 4 3,136 Mar-09-2021, 04:21 AM
Last Post: deanhystad
  beginner question about lists and functions sudonym3 5 2,666 Oct-17-2020, 12:31 AM
Last Post: perfringo
  Absolute beginner am I missing something? kamren 7 3,105 Sep-25-2020, 05:32 AM
Last Post: buran
  chkFile with absolute paths JarredAwesome 7 2,912 Sep-21-2020, 03:51 AM
Last Post: bowlofred
  absolute imports between folders mikisDW 0 1,500 Aug-05-2020, 12:26 PM
Last Post: mikisDW

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020