Python-Is-Easy icon indicating copy to clipboard operation
Python-Is-Easy copied to clipboard

Why the code is not running :

Open vishalrao009 opened this issue 5 years ago • 7 comments

vishalrao009 avatar Feb 04 '20 03:02 vishalrao009

The below code is for python3.

temperature=20 thermo=15 def increase_temp(): if temperature>=15: thermo+=5
print(thermo) return thermo print(increase_temp())

if I pass the argument in function it works.

vishalrao009 avatar Feb 04 '20 03:02 vishalrao009

local variable is referenced before assignment, maybe something like this is a solution?

temperature=20 thermo=15 def increase_temp(): if temperature >= 15: global thermo thermo = temperature + 5 return thermo print(thermo)
print(increase_temp())

babulasi avatar Feb 04 '20 09:02 babulasi

It will work but, 'thermo' seems to be a global variable as it was defined before function.

On Tue, Feb 4, 2020 at 3:05 PM gr8cod3 [email protected] wrote:

local variable is referenced before assignment, maybe something like this is a solution?

temperature=20 thermo=15 def increase_temp(): if temperature >= 15: global thermo thermo = temperature + 5 return thermo print(thermo) print(increase_temp())

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/pirple/Python-Is-Easy/issues/2?email_source=notifications&email_token=AKQLUR5VU4KRPFAXRBGYQRTRBEZFZA5CNFSM4KPP6PG2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEKW6C2Q#issuecomment-581820778, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKQLURZSTSLULQ3SIID4K63RBEZFZANCNFSM4KPP6PGQ .

vishalrao009 avatar Feb 04 '20 10:02 vishalrao009

temperature=15 thermo=15 def increase_temp(): if temperature >= 15: thermo = temperature + 5 return thermo print(thermo) else: message = "Everything is OK" return message print(increase_temp())

ok, I didn't drink coffee...now, I think this is it :)

babulasi avatar Feb 04 '20 10:02 babulasi

Yeah, that works too. Is it that the global variable (Thermo), if once updated inside the function, becomes a local variable?

On Tue, Feb 4, 2020 at 3:51 PM gr8cod3 [email protected] wrote:

temperature=15 thermo=15 def increase_temp(): if temperature >= 15: thermo = temperature + 5 return thermo print(thermo) else: message = "Everything is OK" return message print(increase_temp())

ok, I didn't drink coffee...now, I think this is it :)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/pirple/Python-Is-Easy/issues/2?email_source=notifications&email_token=AKQLUR652EBHMYKWFBBCSQTRBE6UJA5CNFSM4KPP6PG2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEKXCWUY#issuecomment-581839699, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKQLUR7C55A5CVICW3LRHODRBE6UJANCNFSM4KPP6PGQ .

vishalrao009 avatar Feb 04 '20 14:02 vishalrao009

global variables can be used inside or outside of functions and stays global

On Tue, 4 Feb 2020, 15:58 vishalrao009, [email protected] wrote:

Yeah, that works too. Is it that the global variable (Thermo), if once updated inside the function, becomes a local variable?

On Tue, Feb 4, 2020 at 3:51 PM gr8cod3 [email protected] wrote:

temperature=15 thermo=15 def increase_temp(): if temperature >= 15: thermo = temperature + 5 return thermo print(thermo) else: message = "Everything is OK" return message print(increase_temp())

ok, I didn't drink coffee...now, I think this is it :)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub < https://github.com/pirple/Python-Is-Easy/issues/2?email_source=notifications&email_token=AKQLUR652EBHMYKWFBBCSQTRBE6UJA5CNFSM4KPP6PG2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEKXCWUY#issuecomment-581839699 , or unsubscribe < https://github.com/notifications/unsubscribe-auth/AKQLUR7C55A5CVICW3LRHODRBE6UJANCNFSM4KPP6PGQ

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/pirple/Python-Is-Easy/issues/2?email_source=notifications&email_token=AONAFJR5ISSUUAYH67BXQ73RBF673A5CNFSM4KPP6PG2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEKX5OOI#issuecomment-581949241, or unsubscribe https://github.com/notifications/unsubscribe-auth/AONAFJX6DU74KSMHCLO3E6LRBF673ANCNFSM4KPP6PGQ .

babulasi avatar Feb 04 '20 15:02 babulasi

Once you change the value of a global variable inside a function, it's no longer a global variable but a local variable.

edzont avatar Oct 05 '20 17:10 edzont