You are viewing our Forum Archives. To view or take place in current topics click here.
Rounding numbers in python?
Posted:

Rounding numbers in python?Posted:

Coder_
  • New Member
Status: Offline
Joined: Oct 30, 201311Year Member
Posts: 9
Reputation Power: 0
Status: Offline
Joined: Oct 30, 201311Year Member
Posts: 9
Reputation Power: 0
So basically I have this question that tells me the following:

The grade boundaries for a test are;
U - 0
D - 40
C - 50
B - 60
A - 70
Write a program that asks for a mark and then says what grade it is worth.


Now I know how to do this although I thought that I would improve on the code and round up or down the numbers inbetween each level. I can not figure out how to do it and this is my code that I have just wrote. Can you tell we what is wrong with it? Thanks

[ Register or Signin to view external links. ]
#2. Posted:
Huayra
  • TTG Contender
Status: Offline
Joined: Apr 08, 201311Year Member
Posts: 3,101
Reputation Power: 144
Status: Offline
Joined: Apr 08, 201311Year Member
Posts: 3,101
Reputation Power: 144
I'm doing the exact same on my OCR task in school,really hard for me

I shall be looking at this post tomorrow.
#3. Posted:
Coder_
  • New Member
Status: Offline
Joined: Oct 30, 201311Year Member
Posts: 9
Reputation Power: 0
Status: Offline
Joined: Oct 30, 201311Year Member
Posts: 9
Reputation Power: 0
I know right, we are only at the basics really but I wanted to challenge myself and hit a dead end. I will be here tomorrow waiting for your response!
#4. Posted:
UnrealEgg
  • Powerhouse
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49
What I think you want to do:

nearestTen = grade - (grade % 10);

For example:
nearestTen = 56 - (56 % 10);
nearestTen = 56 - (6);
nearestTen = 50;
#5. Posted:
Coder_
  • New Member
Status: Offline
Joined: Oct 30, 201311Year Member
Posts: 9
Reputation Power: 0
Status: Offline
Joined: Oct 30, 201311Year Member
Posts: 9
Reputation Power: 0
Sorry, I don't understand what you mean by this. Could you try to be more specific? I am still quite new. Sorry :S
#6. Posted:
UnrealEgg
  • Powerhouse
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49
It's mainly just a bit of math using the modulo operator (%). Getting the modulus gets the remainder of the division.

So 56 / 10 = 5 remainder 6.

So using what I did above, you can "round down" to the nearest 10.

You could do it to the nearest 5 too. 56 / 5 = 11 remainder 1. Taking off 1 would get you 55.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.