in this code i dont understand why u need the last line or what it does Watch

jenijeni
Badges: 9
Rep:
?
#1
Report Thread starter 4 weeks ago
#1
can someone help me with the purpous of the last 3 lines of this code and what they do
also what is str forin python


analysis = int(input("Enter grade for analysis paper"))
design = int(input("Enter grade for design paper"))
implementation = int(input("Enter grade for implemation paper"))
evaluation = int(input("Enter grade for evaluation paper"))

total = analysis + design + implementation + evaluation

grades = [2,4,13,22,31,41,54,67,80]

print ("your total mark is", total)

if total < 2 :
print ("grade U")

elif total < 4 :
print ("grade 2")

elif total < 13 :
print ("grade 3")

elif total < 22 :
print ("grade 4")

elif total < 31 :
print ("grade 5")

elif total < 41 :
print ("grade 6")

elif total < 54 :
print ("grade 7")

elif total < 67 :
print ("grade 8")

elif total < 80 :
print ("grade 9")








grade = 1

print("You needed another " + str(grades[grade + 1] - grades[grade]) + " marks to get the next grade.")
0
reply
winterscoming
Badges: 18
Rep:
?
#2
Report 4 weeks ago
#2
(Original post by jenijeni)
can someone help me with the purpous of the last 3 lines of this code and what they do
also what is str forin python
str is for converting something into a string. For example, str(10) convers the integer 10 into string "10".

Strings behave differently to integers - for example, when you "add" integers together, it's just plain maths, but when you "add" strings together, it joins them to create a longer string. e.g.
Code:
first = "number: "
num = 10
message = first + str(num)
print(message)
The str() is necessary because without it, Python would complain about trying to add a string to a number (i.e. Python wouldn't understand because strings and integers are like apples and oranges)


To understand this line at the bottom....
Code:
print("You needed another " + str(grades[grade + 1] - grades[grade]) + " marks to get the next grade.")

Firstly look at this list you've got near the top of your program. Remember that lists have positions starting from zero counting upwards:
Code:
grades = [2,4,13,22,31,41,54,67,80]
i.e.
Position 0 is 2
Position 1 is 4
Position 2 is 13
etc.



Then split up the line like this (you've got a variable called grade which stores a value of 1 and doesn't change - are you sure that is what you want..??):
Code:
# Get a number from the grades list, at position: [grade+1]
some_number = grades[grade + 1]

# Get another number from the grades list, at position: [grade]
other_number = grades[grade]

# Do a calculation
result = some_number - other_number

# Convert the result to a string
result_string = str(result)

# Build a message by adding some strings together
message = "You needed another " + result_string + " marks to get the next grade."

# Print the message
print(message)
Last edited by winterscoming; 4 weeks ago
2
reply
jenijeni
Badges: 9
Rep:
?
#3
Report Thread starter 4 weeks ago
#3
thankyou so much for your help, im currently doing gcses and i used to self tech myself but now i am finding it too hard, would you be able to help me out with the basics, and occassionally checking my codes
0
reply
winterscoming
Badges: 18
Rep:
?
#4
Report 4 weeks ago
#4
(Original post by jenijeni)
thankyou so much for your help, im currently doing gcses and i used to self tech myself but now i am finding it too hard, would you be able to help me out with the basics, and occassionally checking my codes
Which parts of Python are you finding difficult? I can answer and try to help or guide yo if you're struggling with anything, but you were definitely doing the right thing by self-teaching. Programming always starts off being really difficult at first, and it takes time but it eventually gets easier and things start to click into place the more you practice. It's worth taking that time to do that now because one of the GCSE exam papers will be all about problem solving and reading or writing code.

Did you see the links I posted in the other thread?
0
reply
jenijeni
Badges: 9
Rep:
?
#5
Report Thread starter 4 weeks ago
#5
yh i did thanks for that, i am folllowing some tasks right now and although u explained some of it before i dont fully understand when to use the str, i posted a thread on this code i was stuck on aswell but do u mind helping me out, but also explain why your doing what you put in the code;

the task...

Write a program that inputs a mark from the keyboard for sections of a project: ‘analysis’, ‘design’, ‘implementation’ and ‘evaluation’. The program should output the total mark, the grade, and how many more marks were needed to get into the next mark band.

Grades are:
<2 U
2 1
4 2
13 3
22 4
31 5
41 6
54 7
67 8
80 9


what i am doing: this is basically everything i understand, i know u helped me with the next step, but please can you help me a bit more, thank you so much for ur time



analysis = int(input("enter grade for analysis paper"))
design = int(input("enter grade for design paper"))
implementation = int(input("enter grade for implemation paper"))
evaluation = int(input("enter grade for evaluation paper"))

total = analysis + design + implementation + evaluation

print ("your total mark is", total)

if total < 2 :
print ("grade U")

elif total < 4 :
print ("grade 2")

elif total < 13 :
print ("grade 3")

elif total < 22 :
print ("grade 4")

elif total < 31 :
print ("grade 5")

elif total < 41 :
print ("grade 6")

elif total < 54 :
print ("grade 7")

elif total < 67 :
print ("grade 8")

elif total < 80 :
print ("grade 9")
0
reply
winterscoming
Badges: 18
Rep:
?
#6
Report 4 weeks ago
#6
(Original post by jenijeni)
yh i did thanks for that, i am folllowing some tasks right now and although u explained some of it before i dont fully understand when to use the str, i posted a thread on this code i was stuck on aswell but do u mind helping me out, but also explain why your doing what you put in the code;

Have you had data types explained yet? The important thing to know is that Python has different ways of storing individual values. that includes integers (whole numbers), strings (sequences of characters), 'floating point' numbers, 'boolean' true/false values, and several others. That's the reason why 10 is different to "10" which is different to 10.0

Depending on what your code is doing, sometimes you want a number to convert into a string (like when using print to write a message to the screen). Other times you might do something like read a number from the user with input(), in which case it's useful to use int() and convert from a string into a number. That's the reason why functions like str(), int() and float exist.

The bottom lineis that if you try to use the 'wrong' data type in the wrong situation, then Python just gets confused and shows an error, so it's usually obvious when you need to use str, because you get something like this:
https://repl.it/repls/EarnestMarvelousPlot
If you press the 'run' button in the above, then it shows an error on line 3 saying "TypeError: must be str, not int" which is a hint that it's trying to join a string and integer value together, but doesn't know how to do that.

And here's the same code, fixed using str(), without the error: https://repl.it/repls/ComplexSaddleb...rchangeability


You should definitely spend time on Codecademy because that'll explain a lot of this stuff and you'll be able to see what the code is doing from the interactive lessons as long as you go through it properly and try to understand everything that's happening: https://www.codecademy.com/learn/learn-python

At the very least, make sure you the first 10 lessons on codecademy because those explain a lot of detail, and you could complete the whole thing in maybe a month if you stick at it.


(Original post by jenijeni)
the task...
You've done most of it already, but a good way to approach programming is to solve one problem at a time - and to try to use the simplest solution that you can possibly find, even if that solution isn't very elegant.


If you need to calculate how many marks to the next boundary then it's just subtraction of the total marks from the grade boundary

You could just do the simplest thing which works, by doing that calculation in each of your if or elif blocks. i.e.
Code:
elif total < 54 :
    print ("grade 7")
    how_many_marks = 54 - total

    # print using a comma, just because it doesn't use str() and it looks neater ...
    print ("You missed the next grade by", how_many_marks)
That isn't very elegant though because that would need to happen in every if or elif block. You'd be copy+paste'ing that same calculation 9 times. But having something working is the most important thing - elegance is a whole separate other problem to solve after the program already works properly.


You could choose to do something more elegant by using the list from your code above and put the grade (0, 1, 2, 3, etc) into a variable instead of just printing it out to the screen:
Code:
elif total < 41 :
    # The print() is gone, and so is the calculation...
    grade = 6

elif total < 54 :
    # The print() is gone, and so is the calculation...
    grade = 7
The grade variable happens to line-up neatly with the list from your first post - i.e. 'grade' correlates to a position in the list (assuming that you use grade = 0 for the bottom/ungraded mark, you can deal with the U problem separately.).

Variables are really useful to use because you can store values in them for later use - that's a good thing because it lets you split up the program. i.e. the program is broken up into multiple "parts" for each problem:
1) Inputting data from the user
2) logic to get a number for the grade (use a variable to keep it for later)
3) calculating the number of marks for the next boundary (use a variable to keep it for later)
4) printing the grade and marks missed to the user. (based on the variables from step 2 and step 3)


So, knowing that the grade can be used as a list position (Because the grade is still a number), then everything I've already explained my previous reply should start to tie up neatly.

Also, that's more elegant because each problem happens in separate steps. But you also only need to 'print' the grade in one place too.

Again, if this stuff still seems hard to understand, then it's really worth doing codecademy for at least a few weeks because you'll get harder stuff than this in the final exam. The idea of breaking up a program into small pieces (divide-and-conquer) is really important.

Also, when writing code it's a really good idea to keep the main logic and calculations completely separate from things which use input() or print() -- that's where variables and lists are really useful because those store information in memory to be used in other parts of the program.

Hopefully that makes sense and gives you a better idea of what to do?
0
reply
jenijeni
Badges: 9
Rep:
?
#7
Report Thread starter 4 weeks ago
#7
thankyou so much
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

University open days

  • University of Birmingham
    Postgraduate Open Day Postgraduate
    Wed, 20 Mar '19
  • King's College London
    Postgraduate Taught Courses - Arts & Sciences - Strand Campus Postgraduate
    Wed, 20 Mar '19
  • University of East Anglia
    All Departments Open 13:00-17:00. Find out more about our diverse range of subject areas and career progression in the Arts & Humanities, Social Sciences, Medicine & Health Sciences, and the Sciences. Postgraduate
    Wed, 20 Mar '19

Where do you need more help?

Which Uni should I go to? (35)
14.34%
How successful will I become if I take my planned subjects? (21)
8.61%
How happy will I be if I take this career? (50)
20.49%
How do I achieve my dream Uni placement? (35)
14.34%
What should I study to achieve my dream career? (30)
12.3%
How can I be the best version of myself? (73)
29.92%

Watched Threads

View All