Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
When I run this easy program and want to exit it, I need to type exit 2 times (at line 7).
def registration():
def username_registration():
entering_username = True
while entering_username:
print("Type exit to leave the registration.")
usernam_from_registration = input("Enter username: ")
if usernam_from_registration == "exit":
break
lenght_usernam_from_registration = len(usernam_from_registration)
if lenght_usernam_from_registration > 15:
print("too long")
else:
return usernam_from_registration
username_registration()
print(username_registration())
registration()
Why is this and how can I make it so I only need to write it one time?
This is because you are calling the username_registration() function twice.
username_registration()
print(username_registration())
The first time you call it, nothing happens because you are not doing anything with the result.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have tried everything I could before asking this question... I am making a little text adventure game, really simple and the only code in it is basically just input(). But when they do something wrong, I want to add an else: and print("Thats not valid!") and go back to example = input(), but my else statement is printing and re-doing input with a correct statement, so its still executing else without any error, can anyone help? I don't know why its doing this and all the examples I have seen this is valid code, and my else statement just executes without checking if it was wrong...
```python
if prompt1 == "y":
print("Lets start")
else:
prompt1 = input()
It then executes input again...
If that's the exact code the indention is wrong.
The if and else of same condition should have same indention:
if condition:
do this
else:
do this
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am a beginner learning Python and wrote the following codes. To control the loop flow I tried using a variable "test", but the loop never stopped. Appreciate if any thoughts.
test=False
while test==False:
a=input("Please enter an even number to make test True: ")
if int(a)%2==0:
test==True
print("test is now True")
else:
print("Please try again!")
test=False
while test is False:
a=input("Please enter an even number to make test True: ")
if int(a) %2 == 0:
test = True
print("test is now True")
else:
print("Please try again!")
Fixed the test == true, should've been test = true :) have fun learning
heres the right code:
test=False
while test==False:
a=input("Please enter an even number to make test True: ")
if int(a)%2==0:
test=True
print("test is now True")
else:
print("Please try again!")
here's what you did wrong:
in the 5th line test==True the == checks if that's what the variable true stores, it doesn't set it; so this can be simply fixed by test=True
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to make a calculator. I have the code that does the operations working, but I am trying to allow the user to keep the calculator running without having to rerun the code. i an trying to assign a Boolean to variable, but python keeps telling me there is a name error, which is the the variable is not defined. Can you please help me?
Thank you in advance.
I have tried to change the available name but it doesn't do anything.
The code stopped working when it got to the while run == true line.
else:
print('Invalid operator, please run code again')
run = True
while run == True:
print(' do you need another problem solved? y/n')
if input() == y:
run = True
elif input() == n:
run = False
I expected the code to ask me if I need another problem solved, but there is a name error.
If the error is complaining about y or n it's because you need to surround it with quotes
if input() == "y":
run = True
Also, run == True is not needed
while run:
does the trick
else:
print('Invalid operator, please run code again')
run = True
while run:
print(' do you need another problem solved? y/n')
inp=input()
if inp == 'y':
run = True
elif inp == 'n':
run = False
you have used input 2 times so your code will wait 2 times for input, use input() once.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Im trying to code in python a function that reacts to a input
example
Action = input("Input:")
function commands():
if action == Commands:
print("Commands so far")
but I don't know how to get the function to work.
any help?
Is this what you are asking for?
def react(action):
if action == 'act':
print('act given')
inp = input("Type action: ")
react(inp)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Python Question: i need to run a program that asks for a password but if the wrong answer is input three times the user is thrown out of the program i can run it in a while loop but cant get it to quit if the wrong password is entered.
Thanks for your help
Adding an approximation of how I'd do it, in the absence of an example containing the problem. else on a for loop will only execute if you did not break out of the loop. Since you know the max number of times to run the loop is 3 you can just use a for loop instead of a while loop. break will still break you out early.
for _ in range(3):
if raw_input("Password:") == valid_passwd: # really should compare hashed values (as I shouldnt have passwords stored in the clear
print "you guessed correctly"
break
print "you guessed poorly"
else:
print "you have failed too many times, goodbye"
sys.exit(1)
# continue on your merry (they got the right password)
How about sys.exit()
>>> import sys
>>> guess = False
>>> if guess:
... pass
... else:
... sys.exit()
http://docs.python.org/3/library/sys.html