Python Forum
Absolute Beginner Python Question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Absolute Beginner Python Question
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 :
if score >= 0.8 :
if score >= 0.7 :
if score >= 0.6 :
if score >= 0.0 :
else :
print('Not a valid score')
score = input('Press enter to exit')
Please put your code in Python code tags, and full error traceback message in error tags. You can find help here.
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.
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:
Not a valid score
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.
@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.
@snippsat - OK thanks for that.

Possibly Related Threads…
Thread Author Replies Views Last Post
  beginner question about lists and functions sudonym3 5 381 Oct-17-2020, 12:31 AM
Last Post: perfringo
  Absolute beginner am I missing something? kamren 7 475 Sep-25-2020, 05:32 AM
Last Post: buran
  chkFile with absolute paths JarredAwesome 7 398 Sep-21-2020, 03:51 AM
Last Post: bowlofred
  absolute imports between folders mikisDW 0 289 Aug-05-2020, 12:26 PM
Last Post: mikisDW
  beginner question ___ 1 350 Jul-12-2020, 08:12 AM
Last Post: Gribouillis
  I am an absolute newbie.Help me! feynarun 4 564 Apr-11-2020, 09:25 PM
Last Post: feynarun
  Beginner question: lxml's findall in an xml namespace aecklers 0 697 Jan-22-2020, 10:53 AM
Last Post: aecklers
  Super easy beginner question AkulaLA 3 673 Nov-07-2019, 03:42 AM
Last Post: Larz60+
  Basic Beginner question NHeav 4 750 Sep-13-2019, 11:43 AM
Last Post: NHeav
  Beginner Question - Esaping the Escape Character correctly? Bramen 4 767 Aug-27-2019, 02:38 PM
Last Post: Bramen

Forum Jump:

User Panel Messages

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