I have a question for Python def.

Asked 2 years ago, Updated 2 years ago, 13 views

Hello, I am a student who has been learning Python for about a month.

We are making a program that calculates the sensory temperature by receiving the temperature and wind elements.

I'm going to use the def statement for input and calculation, but the code below was the best that came to my mind.


def input():
    temperature = int(input('Enter temperature:')
    wind = int (input('Enter wind speed:')
    return(temperature, wind)

def calculation(temperature, wind):
    wind_chill = 35.74 + 0.6215 * temperature - 35.75 * wind ** 0.16 + 0.4275 * temperature * wind ** 0.16
    return(wind_chill)

calculation(input())

print ('Feeling temperature is', wind_chill, end=')

I planned it and turned it around, and I thought it was a mess.

I want to receive two return values through the function and use those two values as separate variables (?) in the other function.

Please give me some advice. Thank you.

python

2022-09-22 10:44

1 Answers

Hello!

I think we can receive one parameter from calculation as below and distribute it into two again!

defuser_input():
    temperature = int(input('Enter temperature:')
    wind = int (input('Enter wind speed:')
    return temperature, wind

def calculation (arg): # Take one here and arg actually looks like this
    temperature, wind = arg #Here! We're going to share it with two armies, temperature, wind, and destroy it!
    return 35.74 + 0.6215 * temperature - 35.75 * wind ** 0.16 + 0.4275 * temperature * wind ** 0.16

print(f's temperature is {calculation(user_input())} for the first time.')

The input() function you created through def was changed to user_input() because it was an existing function.

I used the fstring because there was a part called '''.

For reference1)

defuser_input():
    temperature = int(input('Enter temperature:')
    wind = int (input('Enter wind speed:')
    return temperature, wind

def calculation(t, w):
    return 35.74 + 0.6215 * t - 35.75 * w ** 0.16 + 0.4275 * t * w ** 0.16

temp , wind = user_input()
result = calculation(temp, wind)
print(f's temperature is {result}.')

For reference2)

t, w = int (input('temperature:')), int(input('wind speed:'))
print(f's temperature is {35.74 + 0.6215 * t - 35.75 * w ** 0.16 + 0.4275 * t * w ** 0.16}.')

I think it's okay to do it like reference number one, rather than going from function to function Good luck!


2022-09-22 10:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.