Python Forum
Is that code correct?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is that code correct?
#1
Hi there,

This is my first participation. to answer ►How many miles are there in 10 kilometers?
Is this way correct:

miles_in_kilometer = 10 / 1.61
print('There are {} miles in 10 kilometers'.format (miles_in_kilometer))
Reply
#2
It depends on what you mean by correct?
if you want the output to be
Output:
There are 6.211180124223602 miles in 10 kilometers
Then it is correct?
Note: your conversion amount 1.61 is quite rounded up, it would probably be better using 1.609344
Reply
#3
Thanks for your prompt reply,
I mean is it correct when I answer this question..

How many miles are there in 10 kilometers?

Is it approximately 6.1 miles in 10 kilometers? and this Python code is correct output..!
Reply
#4
10 km to miles
Reply
#5
Thanks,
I am trying to understand all exercises in Think Python book ...

Thanks again..
Reply
#6
You probably want to control display of the result. I would go with yard precision:

>>> miles_in_kilometer = 10 / 1.61
>>> print('There are {:.3f} miles in 10 kilometers'.format(miles_in_kilometer))
There are 6.211 miles in 10 kilometers
Of course one can use expressions inside f-strings so it can be written also as:

>>> f'There are {10/1.61:.3f} miles in 10 kilometers'
'There are 6.211 miles in 10 kilometers'
PS - don't use space between format and opening parenthesis.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
Without an example of what a correct output should look like, no one knows what correct is.
@perfringo is assuming you want 3 decimal places but that might not be correct.
Reply
#8
Thanks @perfringo,
It's a good way to display decimal with f-string.

Thank to you all
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Please check whether the code about the for loop question is correct. (SyntaxError) lilliancsk01 10 2,568 Nov-08-2022, 01:25 PM
Last Post: deanhystad
  Syntax error for code that seems correct mcnhscc39 4 3,591 Nov-15-2018, 08:40 PM
Last Post: j.crater
  Correct pesudo code. Ayiden 4 4,275 Mar-24-2017, 10:15 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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