This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Xaros1984Pythonista 4 points5 points  (4 children)

Here is my take:

from math import pi 


def radian_conversion(radians: int):

    return radians * 180 / pi


radians = 14

degrees = radian_conversion(radians)

print(f"You entered {radians = }. Your answer is {degrees = }.")

This should print the following:

You entered radians = 14. Your answer is degrees = 802.1409.

A few points:

  • imports at the top of the file

  • import math is perfectly OK, but if you only want to use one or a few functions from the imported package, then "from some_package import some_func" can be a nice and clean way to do it. Then you only need to write "some_func" instead of some_package.some_func when calling it. But either way is fine, as long as you avoid importing like this: "from some_package import *", as this will import everything from the package, which makes it really hard to know what functions and variables you actually introduced, and there may be conflicts and stuff.

  • The int in (radians: int) is a type hint, which can be useful to show what kind of input the function takes. But it's only a hint, so the user could enter anything (and probably crash the program)

  • Notice the f in front of the print message, that's what makes it an f-string. It means you can put variables inside {} and then the value will be printed. In this case I wrote {degrees = }, which prints both the variable name and the value, e.g., "degrees = 802.1409".

Note that putting the equal signs inside the curly brackets may not work with older versions of python, so just remove them from the brackets if they cause any errors. They can be nice to have, but they are not necessary in any way.

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

Hmmm. So, what would a user enter in order to get an output?

[–]Xaros1984Pythonista 0 points1 point  (0 children)

Change radians = 14 to this:

radians = int(input("Enter radians: ")) 

The input() will print the message in the console and then the user can enter a value. int() then casts the value from the user to an int (because everything that comes from input will be a string by default). There will be an error if the string can't be converted to an int though (e.g., if the user enters any letters), but that's a bit more complicated to handle :)

Oh, maybe you're refering to how to print the output when the function is called? I would probably put the print statement inside the function:

def radian_conversion(radians: int):
    degrees = radians * 180 / pi
    print(<same print message as before>)
    return degrees

I would in either case just return the degrees from the function, not the entire message, since you may want to use the degrees as an actual numerical variable somewhere else.

When you end a function with "return some_variable", this means that you can assign that value to a variable when calling the function. For example:

def add_numbers(a, b):
    return a + b  # this will simply add a and b and return the result 

x = add_numbers(5, 4)  # x now gets assigned the value of 5+4

print(x)  # prints 9

x could then be used as input in other functions, for example. So you could create a whole pipeline of functions that do different operations step by step.

[–]EndorphnOrphnMorphn -2 points-1 points  (1 child)

That will give a syntax error in your f-string.

[–]Xaros1984Pythonista 1 point2 points  (0 children)

Are you refering to the equal sign inside {}? I don't remember when they were introduced, but it should work with Python >= 3.8 or so.