How to loop back a few lines? [closed] - python

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.

Related

Replace while true loops [closed]

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 last month.
Improve this question
I was the other day trying to create on python a little program who would detect if the key x is pressed, if yes the program continue and do it's task. i found the way to do it with the package keyboard :
while True :
if keyboard.is_pressed("x"):
...
But it appears that the while true loop after a long time, start bugging, and that brought to ask you, if it is possible to do a loop or something to detect a key press without causing lags like the window system events when you click on a shortcut then a program appears.
Thank you.
The keyboard documentation specifically says NOT to use the method you are using:
import keyboard
# Don't do this!
#
#while True:
# if keyboard.is_pressed('x'):
# print('x was pressed!')
#
# This will use 100% of your CPU and print the message many times.
# Do this instead
while True:
keyboard.wait('x')
print('x was pressed! Waiting on it again...')
# or this
keyboard.add_hotkey('x', lambda: print('x was pressed!'))
keyboard.wait()
I would recommend reading the documentation for your specific needs.
Note:
If you are looking to bind the specific key to a function, you could do something like this:
import keyboard
def on_x_key_press():
print("'x' key pressed")
keyboard.on_press_key("x", on_x_key_press)
Now your computer runs through that loop as fast as it can. I think you could add a limit on how many times the keyboard press is checked.
import time
while True:
if keyboard.is_pressed("x"):
...
time.sleep(0.001) # the program just sleeps for 0.001 seconds
If the program still lags, try to increment the value of seconds to wait (for example: from 0.001 to 0.005).

How can I debug an if statement that isn't working? [closed]

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.**

I can't figure out why this code is an infinite loop [closed]

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 2 years ago.
Improve this question
Here is my code that I ran in spyder:
#continue keyword
i=0
while i<10:
if i==5:
continue
print(i)
i=i+1
#dont forget to increment i
#otherwise it becomes infinity loop
first it produced output like
then I stoped the current running program
with KeyboardInterrupt
question is why my loop runned infinitly
After i becomes 5, it never gets incremented, It will become infinte loop.
If you want to print everything else other than 5
i=0
while i<10:
if i!=5:
print(i)
i=i+1
The continue statement is used to skip the rest of the code inside a
loop for the current iteration only. Loop does not terminate but
continues on with the next iteration.
https://www.programiz.com/python-programming/break-continue
So.. i is always 5, because continue makes python always skip i = i + 1

Trouble with call function [closed]

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.

Can only input one thing in Python [closed]

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): ')

Categories

Resources