This is a part of my code. I want to get the value of x to work with. But it’s showing that x is not defined. Please solve. | Sololearn: Learn to code for FREE!

0

This is a part of my code. I want to get the value of x to work with. But it’s showing that x is not defined. Please solve.

https://code.sololearn.com/cuR12GBoyorv/?ref=app

6/23/2021 10:07:26 AM

Soumaya Islam Rimi

11 Answers

New Answer

+1

leap_year=int(input()) def previous(year): for i in range(year-4,year): if (i%4)==0: if (i%100)==0: if (i%400)==0: x=i #use =, not == break else: pass else: x=i break else: pass return x #ident to out of loop print(previous(leap_year)) #use x=I, not x==I. and print returned value.

+1

Soumaya Islam Rimi 3 mistakes there I wrote comments about it. x==I compares x with I. x=I will assign I to x. You written return x in else part. But you need it in out of loop. This returned value is sent to function calling statement. So catch and display like print(previous(leap_year))

+1

Assign returned value in calling function and use it. x = previous(leap_year) #this x variable is different from the x variable in function . But you have same values after calling...

+1

Thank you so so much..really that was the problem... Now you fixed it...

+1

You're welcome...

0

Thanks.. Can you elaborate

0

Is that clears or you asking something else part..?

0

How can i use the value of x in another function.. i called this function inside another..and than used x but that's the error

0

""" Soumaya Islam Rimi there are some corner cases where your code will not work, even with Jayakrishna🇮🇳 correction ^^ to cover all cases, your code must not limit test to a four years range values, as some leap years are skipped by more than 4 years: 1900 is not a leap year, so if you input 1901 to 1903 in your program, you will get an error because x was not defined (range tested start from 1897 to 1899, but previous leap year is 1896) instead, rather test years by decrementing until you got a leap year: """ leap_year=int(input()) def previous(y): while True: y -= 1 if not y%400 or (y%100 and not y%4): return y print(previous(leap_year)) """ should your function return the actual argument if it's a leap year, or are you expected the previous leap year except the argument? in first case, move the decrementation line after the test, to first test the argument given ;) """

0

visph yes..I just removed errors. Because test cases passed i didn't check logic. But it's giving output 1896 for input 1900 . But first time hearing "some leap years skipped more than 4yrs." Yes it's error for 1901. I doubted it that it's x defined in if part then how works for all cases but heard reply it passing so stopped hunting.. Actually it's a single function from total program . Op shared details in message. I tried there it's corrections to work for full code.. I shared all via DM

0

Jayakrishna🇮🇳 I didn't say that 1900 doesn't work (as tested range start from argument-4)... if purpose was to exclude argument ^^ "some leap years skipped more than 4 years" is obvious given the rules: leap year if divisible by 400 or (by 4 and not by 100)... so every years divisible by 100 except those divisible by 400 are not leap years (exception to the divisible by 4 rule) ;P