Why my Python print command not working after "if" function?
Question:
Why my code doesn’t write "good job"? It only jumps after "inp_user" and "inp_pin" to "question" imput.
…and loog not working too 😀
username = "boy"
pin = 123
inp_user = input("User: ")
inp_pin = int(input("Pin: "))
def loop():
if inp_user == username and inp_pin == pin:
print("Good Job")
else:
print("Bruh")
question = input("again?: ")
def second():
if question == "Yes":
loop()
else:
exit
Answers:
You put the check code in functions, but you’re never actually invoking those functions. So you’ve defined two functions, and that’s it. The interpreter never actually steps through the inp_user
and inp_pin
checks.
And calling a function loop
doesn’t actually do anything if you don’t… loop.
Finally exit
is a function, just using it won’t do anything.
your code won’t work because you don’t call the cunctions you have defined
Try This:
username = "boy"
pin = 123
while True:
inp_user = input("User: ").strip()
inp_pin = int(input("Pin: ").strip())
if inp_user == username and inp_pin == pin:
print("Good Job")
else:
print("Bruh")
question = input("again?: ")
if question.strip() != "Yes":
break
It’s good way to design your code according your task.
It seems your desire is to make form that takes a two arguments, and if it’s correct, it writes something. Otherwise, user gets another try and returns to first step.
So first step is finding place for our ,let’s say, static variables, correct variables. I think the better way in such simple task is passing this variables into function.
username = "boy"
pin = 123
def loop(): # <-- no argumets, which leads to useless function. Where should it take inputed data?
if inp_user == username and inp_pin == pin:
print("Good Job")
else:
print("Bruh")
Let’s correct this:
inp_user = input("User: ") # <- Now it asks user to give a data
inp_pin = int(input("Pin: "))
def loop(inp_user, inp_pin): # <-Now it has arguments
username = "boy"
pin = 123
if inp_user == username and inp_pin == pin:
print("Good Job")
else:
print("Bruh")
loop(inp_user, inp_pin) # <- NOTE THAT FUNCTION MUST BE CALLED
The next part is to create something that gives another attempt, otherwise it’s failed. And your question must be something like: How I can terminate execution? It leads you to sys library, specifically to sys.exit()
. Now you have everything you need to build this.
def second(): <- NO ARGS
if question == "Yes": # <- WHAT IS THE QUESTION?
loop() # <- What should I do???
else:
exit # <- WHO I AM?
Note that in python function can take varibles from outer space only via argumnets in function.
It’s a good practice to build all bricks and then build a programm. So we need another function, that gives a second try if answer is YES.
def second(question_data):
if question_data == "YES":
return True
else:
return None
And entire code:
import sys
def second(question_data):
if question_data == "YES":
return True
else:
return None
def loop(inp_user, inp_pin): # <-Now it has arguments
username = "boy"
pin = 123
if inp_user == username and inp_pin == pin:
print("Good Job")
else:
print("Bruh")
question = input("again?: ")
future = second(question)
if future:
inp_user = input("User: ")
inp_pin = int(input("Pin: "))
loop(inp_user, inp_pin )
else:
sys.exit()
inp_user = input("User: ")
inp_pin = int(input("Pin: "))
loop(inp_user, inp_pin)
Why my code doesn’t write "good job"? It only jumps after "inp_user" and "inp_pin" to "question" imput.
…and loog not working too 😀
username = "boy"
pin = 123
inp_user = input("User: ")
inp_pin = int(input("Pin: "))
def loop():
if inp_user == username and inp_pin == pin:
print("Good Job")
else:
print("Bruh")
question = input("again?: ")
def second():
if question == "Yes":
loop()
else:
exit
You put the check code in functions, but you’re never actually invoking those functions. So you’ve defined two functions, and that’s it. The interpreter never actually steps through the inp_user
and inp_pin
checks.
And calling a function loop
doesn’t actually do anything if you don’t… loop.
Finally exit
is a function, just using it won’t do anything.
your code won’t work because you don’t call the cunctions you have defined
Try This:
username = "boy"
pin = 123
while True:
inp_user = input("User: ").strip()
inp_pin = int(input("Pin: ").strip())
if inp_user == username and inp_pin == pin:
print("Good Job")
else:
print("Bruh")
question = input("again?: ")
if question.strip() != "Yes":
break
It’s good way to design your code according your task.
It seems your desire is to make form that takes a two arguments, and if it’s correct, it writes something. Otherwise, user gets another try and returns to first step.
So first step is finding place for our ,let’s say, static variables, correct variables. I think the better way in such simple task is passing this variables into function.
username = "boy"
pin = 123
def loop(): # <-- no argumets, which leads to useless function. Where should it take inputed data?
if inp_user == username and inp_pin == pin:
print("Good Job")
else:
print("Bruh")
Let’s correct this:
inp_user = input("User: ") # <- Now it asks user to give a data
inp_pin = int(input("Pin: "))
def loop(inp_user, inp_pin): # <-Now it has arguments
username = "boy"
pin = 123
if inp_user == username and inp_pin == pin:
print("Good Job")
else:
print("Bruh")
loop(inp_user, inp_pin) # <- NOTE THAT FUNCTION MUST BE CALLED
The next part is to create something that gives another attempt, otherwise it’s failed. And your question must be something like: How I can terminate execution? It leads you to sys library, specifically to sys.exit()
. Now you have everything you need to build this.
def second(): <- NO ARGS
if question == "Yes": # <- WHAT IS THE QUESTION?
loop() # <- What should I do???
else:
exit # <- WHO I AM?
Note that in python function can take varibles from outer space only via argumnets in function.
It’s a good practice to build all bricks and then build a programm. So we need another function, that gives a second try if answer is YES.
def second(question_data):
if question_data == "YES":
return True
else:
return None
And entire code:
import sys
def second(question_data):
if question_data == "YES":
return True
else:
return None
def loop(inp_user, inp_pin): # <-Now it has arguments
username = "boy"
pin = 123
if inp_user == username and inp_pin == pin:
print("Good Job")
else:
print("Bruh")
question = input("again?: ")
future = second(question)
if future:
inp_user = input("User: ")
inp_pin = int(input("Pin: "))
loop(inp_user, inp_pin )
else:
sys.exit()
inp_user = input("User: ")
inp_pin = int(input("Pin: "))
loop(inp_user, inp_pin)