Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm having a problem where I want the user to be able to input text to call functions. It works perfectly fine except for one thing. :/ Once something has been input nothing can be done afterwards. The only way to solve it is run the program again which is not convenient. I have spent a lot of time looking for an answer and need help. I also think other amateurs like me might be wondering this too.
An example of the code:
x = raw_input('test1')
if x == 'x':
print 'test2'
The result:
test1x
test2
x
'x'
As you can see it works once then stops working. For the record I'm using Python 2. Hope this can be solved :)
You need to use a loop if you want to program to keep running.
Here is a simple example:
while True:
n = raw_input("Please enter 'hello':")
if n.strip() == 'hello':
break
The program will keep running until you type hello
You can use the following function
def call():
input = raw_input('input: ')
if input == 'yes':
print 'yes'
call()
call()
last_data = ''
while last_data != 'yes':
input = raw_input('ENTER SOMETHING: ')
#do whatever you want with input
last_data = raw_input('DO YOU WANT TO QUIT? (yes/no): ')
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 do not know what I did wrong.
def main(): # store anything to then replay code.
import time
math = int(input("Hi there, what is 32 + 16? =")) # asking what 32+16 is.
if math == "48":
print("Correct!") #this is the if statement that isn't working, suppose to say correct if the input is 48.
else:
print("Not quite..") # this would come up instead of 'correct' if I would put 48.
time.sleep(2) # a delay
restart=input('Do you wish to start again? ').lower()
if restart == 'yes': # if the player wants to play the game again.
main() # to replay code.
else:
exit() # this wouldn't start the game again.
main() # this just starts main.
Change this
if math == "48":
to this:
if math == 48:
You convert the input to int already.
Wouldn't this be better served in a while loop as opposed to being enumerated at runtime?
To answer your question, you've already declared an integer, the quotes make python interpret it as a string.
You were super close but there are a few things that needed tweaking to get it working.
First, you will need to make sure that your main is defined correctly and remove the "**" from your code before the comments.
Second, the reason your code always fails the if statement check for if they got the correct answer is that you are casting the answer to an integer when you get the input, but then comparing it to a string version of 48, deleting the quotations around that should fix the issue as well.
I have implemented these and had the code working in python3 on my system.
Hope this helps (modified code below)
import time
def main():
math = int(input("Hi there, what is 32 + 16? =")) #
if math == 48:
print("Correct!") #This is where I made the change, removing the
quotes that surrounded the 48.
else:
print("Not quite..")#this would come up instead of 'correct' if I
would put 48.**
time.sleep(2)#a delay**
restart=input('Do you wish to start again? ').lower()
if restart == 'yes':#if the player wants to play the game again.**
main()#to replay code.**
else:
exit()#this wouldn't start the game again.**
main()#this just starts main.**
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
In my program, a user selects to have a value chosen at random. If they want to choose again at random how do I loop back a few lines to run that code again?
For context, it's a recipe program. If they don't like the recipe randomly chosen, they can pick at random again.
user_unhappy = True
while user_unhappy:
#do stuff
#do stuff
user_input = input("ask question")
#do stuff
ask_user_happy = input("happy with choice?")
if ask_user_happy.casefold() == "y" or ask_user_happy.casefold() == "ye" or ask_user_happy.casefold() == "yes":
user_unhappy = False
Loop may be what you want. P.S. It would be helpful to study programming by reading the textbook from the beginning to the end.
I think the solution to this would be to nest one loop within the other. The outer infinite loop for selecting the recipe and the inner loop for going through the lines of the recipe. If the user wants to choose a new recipe, you can break out of the inner loop and show the recipes list again.
while True:
# Show and select recipe.
for line in recipe_lines:
# Show recipe.
# Break if recipe change
# Break from outer loop if no new recipe is chosen.
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
I wrote the parser log_file. But i can`t understand why I can not call a function in this block.
Help me please.
Python is dependant on indentation. In order for the program to work you need to add the correct indentation to your code. That involves 4 spaces for each loop or flow control.
Here are a few that do:
def
if
while
for
Your problem is that Python does not know where to end the while loop without indentation.
Here is an example:
for in range(5):
print('Working')
if i == 4:
print('yes')
else:
print('no')
There are two ways to indent this.
How does Python know whether the if statement should be in the for loop or not? Both the following are valid with very different results:
for in range(5):
print('Working')
if i == 4:
print('yes')
else:
print('no')
OR
for in range(5):
print('Working')
if i == 4:
print('yes')
else:
print('no')
In the first the while prints out the message 5 times and then the if statement starts.
In the second the if is part of the while loop so it runs 5 times as well as printing the message.
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 6 years ago.
Question1 = input("We will start off simple, what is your name?")
if len(Question1) > 0 and Question1.isalpha(): #checks if the user input contains characters and is in the alphabet, not numbers.
Question2 = input("Ah! Lovely name, %s. Not surprised you get all the women, or is it men?" % Question1)
if Question2.lower() in m: #checks if the user input is in the variable m
print ("So, your name is %s and you enjoy the pleasure of %s! I bet you didnt see that coming." % (Question1, Question2))
elif Question2.lower() in w: # checks if the user input is in the variable w
print ("So, your name is %s and you enjoy the pleasure of %s! I bet you didnt see that coming." % (Question1, Question2))
else: #if neither of the statements are true (incorrect answer)
print ("Come on! You're helpless. I asked you a simple question with 2 very destinctive answers. Restart!")
else:
print ("Come on, enter your accurate information before proceeding! Restart me!") #if the first question is entered wrong (not a name)
Question3 = input("Now I know your name and what gender attracts you. One more question and I will know everything about you... Shall we continue?")
In order to get the right answer, I will first tell you what happens when I run it:
There are several scenarios but I will walk you through 2. When I run the program I am first asked what my name is, I can enter anything here that is alphabetic, then it asks me if I like men or woman, weird I know but it's a project I am working on, if I were to say 'men' or 'women' the program runs perfectly, but if I were to enter say... 'dogs' it would follow the else statement print ("Come on! You're helpless. I asked you a simple question with 2 very destinctive answers. Restart!") but then it continues the code and goes to the last line shown above "Now I know your name and what gender attract you...... blah blah". What I am trying to do is have it restart the script if you were to enter anything other than 'men' or 'women'.
I was told that a while statement would work, but I would need an explanation as to why and how... I'm new to Python in a sense.
That's what loops are for.
while True:
# your input and all that here
if answer == "man" or answer == "woman":
# do what you want if the answer is correct
break # this is important, as it breaks the infinite `while True` loop
else:
# do whatever you want to do here :)
continue
If you want the script to repeat indefinitely, you can simply wrap everything in a while loop like so:
while True:
# Do yo your logic here.
# Break out when a condition is met.
If you want to be more savvy with it you can use a method and have the method call itself over and over until it's done.
def ask_questions():
# Do your logic here
if INPUT_NOT_VALID:
ask_questions()
else:
# Continue on.
Also, search on this site for you text because I've seen about five or six questions all involving this programming scenario which means it has to be a common problem from a programming class (one I'm assuming you are in). If that's the case discuss with classmates and try to stimulate each other to find the solution instead of just posting online for an answer.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
question1:The if condition is not working. Every time it quits
question2:(for i in range(1, ret[0]+1):
NameError: name 'ret' is not defined)
Your indentation seems to be way off. It should be the same amount throughout the code. PEP 8 suggests four spaces for indentation.
You are probably receiving an IndentationError.
You seem to mention that "Every time it quits". This is expected. Your code, pp.quit() will quit the program if action does not equal either 'stat', 'list', or 'retr', which is what is happening.
Here is a simplified version of what you have:
action = ""
if action == "stat": # Not true, action == ""
# stuff
elif action == "list": # Not true, action == ""
# stuff
elif action == "retr": # Not true, action == ""
# stuff
else: # Looks like this is where we will end up
exit()
It's no surprise you are quitting every time, since you have hard coded a condition to make it quit every time.
You say if you remove the action = "" you get a NameError saying action is not defined... that is because you never set it to anything... I'm not sure what you expected the if block to do as written. You need something like this:
action = a_function_that_gets_info_from_user_and_returns_a_string()
This will set action to something that might pass your if block.
As a side note, you should't do screenshots for your questions. Instead, copy/paste; it's the polite thing to do. Now I have to hand type your code to illustrate what's wrong instead of copying it myself.