0

So I started learning to program last week, and now I can't figure it out by myself. I need final answer to be "Size between 24 to 192." But if I round it, it gives me 192.0. What do I need to change?

minp = 23.75
maxp = 192.4
minp1 = round(minp, 0)
maxp1 = round(maxp, 0)
print("Size between " + str(minp1) + " to " + str(maxp1) + ".")
1
  • 1
    Just drop the , 0 part: round(minp). Commented Jan 27, 2017 at 22:15

2 Answers 2

2

That's because the type of minp1 and maxp1 are still floats, you can change them to ints by passing them to the int(..) constructor:

minp = 23.75
maxp = 192.4
minp1 = int(round(minp, 0))
maxp1 = int(round(maxp, 0))
print("Size between " + str(minp1) + " to " + str(maxp1) + ".")

Or in a terminal:

$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> minp = 23.75
>>> maxp = 192.4
>>> minp1 = int(round(minp, 0))
>>> maxp1 = int(round(maxp, 0))
>>> print("Size between " + str(minp1) + " to " + str(maxp1) + ".")
Size between 24 to 192.

Nevertheless, it is not a good idea to do string processing/concatenations yourself. You better format it with a formatting string, like:

minp = 23.75
maxp = 192.4
minp1 = round(minp, 0)
maxp1 = round(maxp, 0)
print("Size between %d to %d."%(minp1,maxp1))

Here %d stands for "Signed integer decimal". So by using format, Python will fill in the variables at the %d places itself which is elegant and requires less thinking from your side.

Sign up to request clarification or add additional context in comments.

3 Comments

@user7438048: excellent suggestion. Added that to the answer.
int(round(minp, 0)) would be simpler as round(minp).
@MarkDickinson: yeah but it would not explain why the misprint is happening.
0

If you wanted to leave the numbers as float and specifically output in integer format:

def r(val):
    return "{0:.0f}".format(round(val))

minp = 23.75
maxp = 192.4
print("Size between {0} to {1}.".format(r(minp), r(maxp)))

Result:

Size between 24 to 192.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.