3
u/Obsc3nity Aug 20 '25
Generally it’s considered bad practice to print randomly throughout methods. If you instead use a return statement you can return float(mile)*1.61 and then main can decide what to do with the converted info. This also makes the function definition better fit the name, as the name mile_to_km doesn’t indicate that there would be any printing.
1
2
u/Sneaky_processor Aug 20 '25
Consider using return statements in your functions instead of printing.
And i don't think the main logic needs to be a function since you really dont reuse it ( the main reason of a function is if the code is gonna be used multiple times).
2
u/Overall-Screen-752 Aug 20 '25
I think a main method is a best practice as it shadows other languages and makes it abundantly clear that this is the entry point of the program. I would prefer to see an if name == “main”: wrapping the main call too, but OP will get there eventually
1
u/Red_Priest0 Aug 20 '25
Thanks for response , I don't understand what is this concept and which part of python it's come under
1
u/Overall-Screen-752 Aug 23 '25
I see that someone else explained this concept, but if you’re interested in more you can search up “python dunder methods” to learn more about this. Dunder is a shorthand/portmanteau of “double underscore” and refers to methods in the python standard library with double underscores on either side. Happy coding.
1
2
5
1
u/Kqyxzoj Aug 21 '25
More importantly, is converter2 okay? Friends and family are concerned for the well-being of converter2. :/
1
1
u/Loud-Bake-2740 Aug 20 '25
as others have mentioned, use return rather than print. BUT if you are going to print, you should use an f-string (formatted string). this allows you to more cleanly put variables into a print statement. see the following
print(f”There are {kilometer_value} kilometers in {miles_value} miles”) this is the general standard for how to print things, and will make it much easier to do on the fly later on
1
u/Rebulien Aug 21 '25
Might be a bit complex for the beginner, but if you are coming from different languages, where you have to define types (typed languages like C++, Java etc.), I would recommend you learning and start implementing type hints. Once your code grow, you will start to appreciate it.
What it does is it hints you and the editor, what type does the function expect.
# for python 3.12 and earlier
from __future__ import annotations
# function multiplies string
def fnc_without_annotation(foo, bar):
return foo*bar
def fnc_with_annotation(foo: int, bar: str):
return foo*barNote that if you type hint your variable, the editor can help you suggesting methods. Also, its just type HINT, the language itself does not process it, meaning you can still call the function with incorrect arguments.
fnc_with_annotation(3, 'a') # 'aaa'
fnc_with_annotation(3, 3) # 9
fnc_with_annotation(True, 'a') # Runtime Error Happy coding!
1
u/lokidev Aug 21 '25
My general recommendations for a beginner:
validate the input. You expect a number. Find a way to validate that you really get a number and return an error if not
Check out https://pep8.org
Check out f-strings - they're nice to use and good for you
Find out what `if __name__ == '__main__'` means and use it afterwards
converter3 is not a a good name
let your functionname and variable names speak. Most (not all) comments are trivial
find the way (refer to 2.) how comments are being done for functions in python :)
4
u/LavaDrinker21 Aug 20 '25
I'll explain it because others are thinking it:
if __name__ == "__main__":is basically the "main" function in python (not required but good to know)Realistically it means that if you open the file as itself (
python main.py) it'll execute whatever is in that block, but if you instead call it like a module (import main) then it'll only give you each part to use in the new program.Edit: model != part
(part: vars, funcs, classes, etc)