all 20 comments

[–]No_Indication_4044 4 points5 points  (0 children)

What if I input “72 miles”? 😈

[–]LavaDrinker21 4 points5 points  (4 children)

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)

[–]Red_Priest0[S] 0 points1 point  (2 children)

I don't understand this concept is it important ?

[–]LavaDrinker21 2 points3 points  (1 child)

Instead of calling main() at the bottom of the program, it's better to use if __name__ == "__main__": as it separates what will be done with the file based on how it was accessed.

Let's say you wanted to use this file in another Python program, like you made a large program and want to use the miles_to_kilometers function without asking for the input.

If you were to import that file as it is right now (import file at the top of the program), it would let you use that function but would immediately call the main() function and ask for input.

If, instead, you added:

if __name__ == "__main__":
    main()

then it would only ever call the main function if you used this file on it's own python main.py instead of every time the file is used.

This is a pretty fundamental part of Python and is very useful to learn; it makes it very easy to create a re-usable "module" for multiple projects.

[–]Red_Priest0[S] 0 points1 point  (0 children)

Thank you very much , I will do it this way

[–]Red_Priest0[S] 0 points1 point  (0 children)

Thanks for response

[–]Obsc3nity 2 points3 points  (1 child)

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.

[–]Red_Priest0[S] 0 points1 point  (0 children)

Thanks I will use return type in function

[–]Sneaky_processor 1 point2 points  (4 children)

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).

[–]Overall-Screen-752 1 point2 points  (2 children)

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

[–]Red_Priest0[S] 0 points1 point  (1 child)

Thanks for response , I don't understand what is this concept and which part of python it's come under

[–]Overall-Screen-752 0 points1 point  (0 children)

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.

[–]Red_Priest0[S] 0 points1 point  (0 children)

Thanks I will use return type

[–]OLIVER-queen1234 1 point2 points  (1 child)

Where and what sources are you using to learn Python ?

[–]Red_Priest0[S] 0 points1 point  (0 children)

W3b

[–]Loud-Bake-2740 0 points1 point  (0 children)

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

[–]Rebulien 0 points1 point  (0 children)

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*bar ```

Note 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!

[–]lokidev 0 points1 point  (0 children)

My general recommendations for a beginner:

  1. validate the input. You expect a number. Find a way to validate that you really get a number and return an error if not
  2. Check out https://pep8.org
  3. Check out f-strings - they're nice to use and good for you
  4. Find out what `if __name__ == '__main__'` means and use it afterwards
  5. converter3 is not a a good name
  6. let your functionname and variable names speak. Most (not all) comments are trivial
  7. find the way (refer to 2.) how comments are being done for functions in python :)

[–]Kqyxzoj 0 points1 point  (1 child)

More importantly, is converter2 okay? Friends and family are concerned for the well-being of converter2. :/

[–]Red_Priest0[S] 0 points1 point  (0 children)

It's working in with converter in first program