Trouble with call function [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 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.

Related

How to loop back a few lines? [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
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.

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

unexpected Indent and break out of loop in game code [closed]

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 am new to python as you may be able to tell with this question. I am currently building a Rock, Paper, Scissors game to later include into a bigger program i am working on in python 3.4. the problem i am having is in the code listed below.
def computerPlayer(): #randomly selects a rock paper or scissor for computer hand
c = random.randint(0, 2)
if c==0:
y=('rock')
if c==1:
y=('scissors')
if c==2:
y==('paper')
return y
in front of the bottom line return y i am getting a unexpected Indent error, i have tried correcting this over the past day now with no results, if i move it forward i get 'return' outside function, but when i move it back i get the unexpected indent, I am honestly at a complete loss here and im not sure where to go. Any help is great thanks.
the above problem is now fixed, but i know have a break outside of loop error. it is appearing at the end of my code now. any help is great thank you.
again = raw_input('do you wish to try again? (yes\no)\n :') #Ask the user if they want play again
if again == ('yes') or again == ('sure') or again == ('okay'):
print ('')
elif again == ('no') or again == ('nah') or again == ('nope') or again == ('screw you') or again == ('screw it'):
print ('FINE THEN!!! =^( \n (Enter>>>game()<<< if you change your mind)')
#breaks the loop
break
game()
Try this:
def computerPlayer():
'''
Randomly selects a rock paper or scissor for computer hand
'''
c = random.randint(0, 2)
if c == 0:
y = ('rock')
if c == 1:
y = ('scissors')
if c == 2:
y = ('paper')
return y
Indentation is important in python, it shows where your methods and control flows start and end. In your previous code, the if statements were not indented under the method and so python could not tell that it was apart of the computerPlayer() function.
According to PEP8 ( a style guide for python ) proper indentation is 4 spaces. For more information on PEP8 and its view on indentation, check here:
http://legacy.python.org/dev/peps/pep-0008/#indentation

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

Problems about python if condition [closed]

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.

Categories

Resources