Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
This is what I've input:
def greater_less_equal_5(answer):
if 6 > 5:
return 1
elif 4 < 5:
return -1
else:
return 0
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)
and gave me this note
Oops, try again. It looks like your function output 1 instead of -1
when answer is 3. Make sure you filled in the if and elif statements
correctly!
and this is what came up in the upper right display:
>1
>1
>1
>None
No matter how I change around the numbers and the >/< I've even tried == and != it still outputs 1 1 1 None.
I've searched around for any possible tips and seen others stuck on the same problem as me and when I tried their solves I then get:
def greater_less_equal_5(answer):
if > 5:
return 1
elif < 5:
return -1
else:
return 0
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)
and the output is:
File "python", line 2
if > 5:
^
SyntaxError: invalid syntax
Is this test rigged to always output a failed result to make me pay for pro and ask for their help?
And the hint given for this is:
Make sure the if and elif statements end with colons :
Your code should look something like:
if EXPRESSION:
do something
elif OTHER EXPRESSION:
do something
else:
do something
Am I just missing something horribly basic?
You are indeed missing something basic - namely, that the output of your function doesn't depend on answer at all. No matter what you feed in as answer, because 6 > 5 is always True, it will always return the result of that case.
What you need is
def greater_less_equal_5(answer):
if answer > 5:
return 1
elif answer < 5:
return -1
elif answer == 5:
return 0
You're missing 'answer' variable for expression, which you pass into your function
def greater_less_equal_5(answer):
if answer > 5:
return 1
elif answer < 5:
return -1
else:
return 0
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)
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 last month.
Improve this question
I'm a Python noobie, please have mercy.
I tried to create a code to test if a given number is a prime number. I tried the %, sqrt, and n methods. All ended with the same result: when I try to test the code for numbers greater than 4, the code returns NULL and says "Process finished with exit code 0"
Can you please help me figure out what I'm doing wrong?
Here's the code I wrote (this is the 'n' method where I apply % to numbers all the way up to n; I know it's not the most efficient, but I'll fix that after I make sure it works):
x = 5
if x > 1:
for i in range(2, x):
if (x % i) == 0:
print("NOT prime")
break
else:
print("YES, it's prime")
else:
print("NOT prime, because the number is < 1")
break should be indented once more, so it does not break the loop when it is not a prime.
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 3 months ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm trying to stop my program when the Warrior are Priest are both with 0 or < 0 or the vampire goes to 0 our < 0 if not they will not advance for the next round and just finish the battle that way, and set up a winner.
while (warrior[0] and priest[0]) > 0 or vampire[0] > 0: #Loop until all of the group die or the enemy dies
first_turn() #Execute the first turn
if (warrior[0] and priest[0]) > 0 or vampire[0] > 0:
second_turn() #Execute the second turn
if (warrior[0] and priest[0]) > 0 or vampire[0] > 0:
third_turn() #Execute the third turn
if (warrior[0] and priest[0]) > 0 or vampire[0] > 0:
initiative_phase()
else:
break
I've tried the way it is above, but I'm not catching why is it not stopping.
Change the while condition to:
while (warrior[0] > 0 or priest[0] > 0) and vampire[0] > 0:
We added parenthesis Because precedence of logical 'and' is greater than the logical 'or'.
If warrior[0]>0 is true it does not consider whether vampire[0]>0 condition at all.
Seems like you need parentheses.
while ((warrior[0] > 0 or priest[0] > 0) and vampire[0] > 0): #Loop until all of the group die or the enemy dies
execute_turn() #Execute the turn
else: #someone died
break
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
while count > 0:
if count = 0:
return n
elif count < 0:
print(" ") # prints empty if n is below 0
else:
count = count - 1
collect += math.ceil((n - 5)/2)
return collect
The inputs are (1003, 3) - result is 499, which means it just loop once and subtracts 5 and then divides by 2, then it stops. Anyone know why?
Your inner conditionals don't make sense with the while. And you have a return statement in the loop, so yes, it only looped once.
Start with this
import math
n, count = (1003, 3)
print("N = " + str(n))
while count > 0:
n = math.ceil((n - 5) / 2) # Update this to do whatever your logic is
print(count, n)
count -= 1
if n < 0:
print("N is negative")
else:
print("N = " + str(n))
You have several problems.
First, your syntax isn't indented evenly.
Second, your if statement have = instead of ==. The first one for assigning values to variables, the second is for checking equality.
Third, you have a return statement that's going to exit from whatever function this loop is inside.
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
whenever i try to use else and elif i get that error .
x=4
if x>0:
print("positive")
elif x=4:
print("equal")
else:
print("negative")
Msg Error :
File " '<'stdin'>' ", line 1
elif x=4 :
^
SyntaxError: invalid syntax
File "'<'stdin'>'", line 1
else:
^
SyntaxError: invalid syntax
= is an assignment operator.
== is a comparison operator.
You need to use the comparison operator in the elif statement like so:
x = 4
if x > 0:
print("positive")
elif x == 4:
print("equal")
else:
print("negative")
That is because you are using assignment operator = instead of equality operator ==.
x=4
if x>0:
print("positive")
elif x==4:
print("equal")
else:
print("negative")
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
try:
f = int(factor)
if (factor == 0):
print("No factors present to derive");
elif (factor != int):
print ("Numbers only please!");
elif (factor >> 4):
print("Four maximum factors!");
else:
f = int(factor);
if f == 1:
coefficientone = raw_input("What is the coefficient on the first term?")
try:
coef1 = int(coefficientone)
if (coef1 == 0):
print "No coefficient present, please re-enter polynomial"
elif (coef1 != int)
print "Numbers only please!"
else:
coef1 = int(coefficientone)
This returns a syntax error on the if f == 1: line. Please help! From what I have read on this site and most other sites that line looks to be correct. I would appreciate any help on any other portion of the code, too, for this is my first time working with python. Thanks in advance.
If you're going to add a try block, you have to have a matching except, to handle the exception you're catching.
If you don't know why you're adding the try block, it's best to simply remove it, as to not mask potential errors. It's best to only except specific error types.
Here's the fixed code. The changes are noted with comments.
try:
f = int(factor)
if (factor == 0):
print("No factors present to derive");
elif (factor != int):
print ("Numbers only please!");
elif (factor >> 4):
print("Four maximum factors!");
else:
f = int(factor);
except Exception as e: # Add this
print 'ERROR: {0}'.format(e) #
if f == 1:
coefficientone = raw_input("What is the coefficient on the first term?")
try: # Un-indent this
coef1 = int(coefficientone)
if (coef1 == 0):
print "No coefficient present, please re-enter polynomial"
elif (coef1 != int)
print "Numbers only please!"
else:
coef1 = int(coefficientone)
except Exception as e: # Add this
print 'ERROR: {0}'.format(e) #
Finally, it seems like you're planning on asking the user for as many coefficients as he specifies. For this, you'll probably want to consider using a for loop to handle the input, and a list to store the values. I'll leave the implementation details up to you. There are plenty of resources out there.
#an error that I am seeing is that factor != int should be:
elif type(factor) != int:
#and adding to the try I think that you will also need a except ValueError: