Leo
Leo

Reputation: 111

Upcounting output

I've a problem and I have no idea how to solve it.

Well, I've written a small Python program. It calculates a number and then should output another number. E.g. when the solution is below 100 it should print '100m', when it's <200 it should print '101m', <300 = '102m' etc...

I could write something like

if solution < 100:
  print '100m'
elif solution < 200:
  print '101m'
elif solution < 300:
  print '102m'

Would be possible BUT I think it's impossible to do that again and again until 1000 or 2000 and it would look weird in the code. ;-)

Hope there is an answer... (Oh and sorry for my bad English and Python knowledge)

Upvotes: 0

Views: 105

Answers (4)

BergmannF
BergmannF

Reputation: 10245

It seems that you want to increment the printed number by 1 for every step of 100 that the solution variable takes?

If so you could calculate the the addend easily:

addend = (solution / 100)
print str(100 + addend) + "m"

Upvotes: 1

kaspersky
kaspersky

Reputation: 4097

Your logic reads to me as:

print solution / 100 + 100

Upvotes: 0

Katriel
Katriel

Reputation: 123632

print("10{}m".format(x//100))

(You really just want to divide by 100 and round down.)

Upvotes: 5

Marcin
Marcin

Reputation: 49816

Divide by 100; round down. That number is the last digit in your examples.

Upvotes: 1

Related Questions