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'm completely new to programming and am trying to write a function that takes a yes or no question. However, when I run the below it always seems to complete with my variable as False. There has to be something simple here that I'm missing. I'd love to hear any thoughts / feedback / improvements to it. Thank you!
def yes_or_no(question):
answer = input(question).lower().strip()
print("")
while not(answer == "y" or answer == "yes" or \
answer == "n" or answer == "no"):
print("\nSorry, only Y or N please.")
answer = input(question).lower().strip()
print("")
print(answer)
if answer == 'y' or answer == 'yes':
answer = True
else:
answer = False
You forgot to return the answer, so that function returns None by default. Add return answer at the end of the function.
Related
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 very new to Python, I have searched and cannot find answer. Thank you in advance.
I purposely type the incorrect answer and it responds properly - prompts me to give input again.
I now type the correct answer and it responds with error (my code is below error)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in
54 print("Enter Y/N:")
55 answer = input()
---> 56 answer = input.upper()
57
58 print("Great! Lets get started.")
AttributeError: 'function' object has no attribute 'upper'
`# intro text
print("Are you ready to create a super hero with the super hero generator 3000?")
# Ask for input / use input() to take it / use upper() to adjust text
print("Enter Y/N:")
answer = input()
answer = answer.upper()
# adding while loop to condition input(answer)
while answer != "Y":
print("Sorry, but you have to choose Y to continue.")
print("Enter Y/N:")
answer = input()
answer = input.upper()
print("Great! Lets get started.") ```
Change your code to:
while answer != "Y":
print("Sorry, but you have to choose Y to continue.")
print("Enter Y/N:")
answer = input()
answer = answer.upper()
input() function has no function upper(), you want to call upper() on answer.
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 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 6 years ago.
Improve this question
def shut_down(s):
if s == "yes":
return "Shutting down"
elif s == "no":
return "Shutdown aborted"
else:
return "Sorry"
Well, you could try putting an indented block, as it says it is expecting an indented block.
def shut_down(s):
if s == "yes":
return "Shutting down"
elif s == "no":
return "Shutdown aborted"
else:
return "Sorry"
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 8 years ago.
Improve this question
I'm writing a simple text-based game and I've run into an error here.
def lamp():
print "You pick up the lamp and examine it."
print "It looks like an ordinary gas lamp."
print "What do you do?"
lamp_action = raw_input("> ")
if "rub" in lamp_action:
rub()
elif "break" or "smash" in lamp_action:
print "The lamp shatters into pieces."
dead("The room disappears and you are lost in the void.")
else:
lamp()
If I comment out the elif part, Python gives an invalid syntax error on else. If I leave the elif part in, the program will run without an error, but even typing something random like "aaaaaa" will follow the elif action.
I also doesn't work if I replace the else section with something like this:
else:
print "That's not a good idea."
lamp()
or like this:
else:
dead("That's not a good idea.")
Where dead is:
def dead(why):
print "%s Game over." % why
exit(0)
What did I miss?
"break" or "smash" in lamp_action is interpreted by Python as testing "break", then testing "smash" in lamp_action. Since "break" is a nonempty string, it is always interpreted as "true", so the elif is always taken.
The correct form is
elif lamp_action in ('break', 'smash'):
i.e. testing if the action is in a list of possibilities.