noob question local/global variable is not being called
Question:
program asks for input
if input is odd it prints weird
if input is even it runs variable checks
check "checks" which range input falls into and prints text according to the input
—
the code works or odd inputs but nothing is printed out for even inputs
if __name__ == '__main__':
n = int(input().strip())
def check():
if n > 20:
print ('Not Weird')
elif n >= 6 and n <= 20:
print ('Weird')
elif n >= 2 and n <= 5:
print ('Not Weird')
if n % 2 == 1: print("Weird")
elif n % 2 == 2: check
what am i doing wrong
what did i try
added () after check
check()
Answers:
def
is a function, and a function must be called so you must use check()
otherwise it won’t execute.
The second error is checking the parity of the number. If you want to check if the number is even then you must do if n % 2 == 0:
. The %
sign returns the remainder of the division, in this case by 2. Therefore if a number is even, say 6, 6 % 2 is 0.
Another note, code written with no indentation will execute every time, either by running the script or by importing it from another script. However, code inside the block: if __name__ == '__main__':
is executed only when the current script is run by python (not when imported). So don’t reference variables defined inside the if __name__ == '__main__':
(in your case the variable n
) outside of that block.
The function call is necessary when you want to use it. And, the function is called using the parenthesis ()
. As @Fabio pointed out the %
represents a modulus that gives you the remainder when divided. 3%2 = 1
and 4%2 = 0
To know more about modulus check this Modulo operator
def check():
if n > 20:
print ('Not Weird')
elif n >= 6 and n <= 20:
print ('Weird')
elif n >= 2 and n <= 5:
print ('Not Weird')
if __name__ == '__main__':
n = int(input().strip())
if n % 2 == 1: print("Weird")
else: check()
program asks for input
if input is odd it prints weird
if input is even it runs variable checks
check "checks" which range input falls into and prints text according to the input
—
the code works or odd inputs but nothing is printed out for even inputs
if __name__ == '__main__':
n = int(input().strip())
def check():
if n > 20:
print ('Not Weird')
elif n >= 6 and n <= 20:
print ('Weird')
elif n >= 2 and n <= 5:
print ('Not Weird')
if n % 2 == 1: print("Weird")
elif n % 2 == 2: check
what am i doing wrong
what did i try
added () after check
check()
def
is a function, and a function must be called so you must use check()
otherwise it won’t execute.
The second error is checking the parity of the number. If you want to check if the number is even then you must do if n % 2 == 0:
. The %
sign returns the remainder of the division, in this case by 2. Therefore if a number is even, say 6, 6 % 2 is 0.
Another note, code written with no indentation will execute every time, either by running the script or by importing it from another script. However, code inside the block: if __name__ == '__main__':
is executed only when the current script is run by python (not when imported). So don’t reference variables defined inside the if __name__ == '__main__':
(in your case the variable n
) outside of that block.
The function call is necessary when you want to use it. And, the function is called using the parenthesis ()
. As @Fabio pointed out the %
represents a modulus that gives you the remainder when divided. 3%2 = 1
and 4%2 = 0
To know more about modulus check this Modulo operator
def check():
if n > 20:
print ('Not Weird')
elif n >= 6 and n <= 20:
print ('Weird')
elif n >= 2 and n <= 5:
print ('Not Weird')
if __name__ == '__main__':
n = int(input().strip())
if n % 2 == 1: print("Weird")
else: check()