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
Trying to teach myself Python via Codecademy. However, when I reach this section, it keeps returning the same error, no matter what I do. Anyone else encounter this problem, or know how to fix it? I'm a noob to coding of any way shape or form.
A screenshot of the error can be seen here: https://drive.google.com/file/d/0B7fG5IDRoZ3cXzZxbnlpT3RheHc/edit?usp=sharing
answer = 2
def the_flying_circus():
if ______: answer + 5 = 7
print "This gets Printed!"
elif (answer < 5):
print "As does this!"
else end
The error is:
elif (answer < 5):
^
SynxtaxError: invalid syntax
The error is in your indentation.
You can't have an elif unless it follows an if block. Because your if statement is on one line, and your next print statement is on the next line, you don't meet this requirement. I also don't know what you're intending to do with your if ________: section.
Here's a fix:
answer = 2
def the_flying_circus():
if answer + 5 == 7:
print "This gets Printed!"
elif (answer < 5):
print "As does this!"
# ...
else: end you are missing a `:`
You have a lot of other syntax errors:
if ______: answer + 5 = 7
You cannot assign to an operator.
Unless end is a variable defined somewhere using else: end is not python syntax
Not sure what you want but from your code this seems more logical:
answer = 2
def the_flying_circus():
if answer + 5 == 7:
print "This gets Printed!"
elif answer < 5:
print "As does this!"
else:
return answer
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 5 months ago.
Improve this question
I am a total beginner working on an introductory python class but I am getting syntax errors every time I make an if statement. Can anyone help with this?
Here is my code:
def report_size(n: int) -> str:
"""Return 'small' if n is between 0 and 20 inclusive,
'medium' if n is between 21 and 40 inclusive,
and 'large' if n is 41 or greater.
Precondition: n >= 0
>>> report_size(4)
'small'
>>> report_size(24)
'medium'
>>> report_size(45)
'large'
"""
If n <= 20:
return 'small"
elif 20< n <= 40:
return 'medium"
else:
return 'large'
if should not be capitalized.
Also, the apostrophes you use for strings are inconsistent. If you open a string with ' you have to close with ' and if you open with " you should close the string with ".
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
Check out the screenshot and help this newb with why i'm getting this syntax error with the for loop even though im following the right syntax.
The code :
elif choice == 'AVERAGE':
import statistics
lst = []
n = int(input('Enter number of values to calculate mean of: ')
for i in range(0,n):
ele=int(input())
lst.append(ele)
The Error : Invalid Syntax for the ':' after 'range(0,n)'
You are spacing the items inside the for loop with double Tab, the indentation should be either 4 spaces or a single tab.
And you are missing a parenthesis closing in the n input line
See the modified code below.
elif choice == 'AVERAGE':
import statistics
lst = []
n = int(input('Enter number of values to calculate mean of: '))
for i in range(0,n):
ele=int(input())
lst.append(ele)
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
I have been trying to make a simple py script that will check if the result is different. It should check for password and one number from 0-100, and there are 3 possible answers:Wrong, Try again
and the last one which I need(and don't know it, so I am trying to pull that out).
This is intended to be like a script for brute forcing.
p="Wrong!"
a="Try again."
for i in range(00, 100):
print("SuperPassword " + str(i))
if not(p) and not(a):
break
print i
break
else:
continue
It just prints all numbers and doesnt stop till it end(it should stop at right digit password), and how to tell python to print 00 as password since it prints 0 no matther what I type(for e.g. for i in range(00,100) it prints 0,1,...)
Thanks in advance
I think you're looking for a nested for loop...
def brute_force():
for x in range(10):
for y in range(10):
p = '%d%d' % (x, y)
print p
if p == '87':
print 'Right!'
return
print 'Wrong!'
given that we have little information on the question. I suggest that you start with this.
p = 0
for i in range(0, 100):
print("SuperPassword " + str(i))
if p == i:
break
you should be comparing p (i guess that it is password) with i.
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 4 years ago.
Improve this question
I have the following program to find out the leap year
year = int(input("Enter a year: ")
if (year % 4 == 0) and (year % 100 == 0) and (year % 400 == 0):
print("True")
else:
print("False")
However, I'm getting following syntax error
File "is_leap.py", line 2
if (year % 4) and (year % 100) and (year % 400):
^
I do not understand why it is showing syntax error in :
P.S: I am newbie to Python and programming, and I knew there are solutions in online to figure out leap year. But I'm trying to figure this in my way.
I'm trying to understand why it's showing this syntax error while the "if" condition should have : in Python.
That line itself is fine.
You're missing a closing parenthesis on the line before it:
year = int(input("Enter a year: ")
^^^^
P. S. Not directly related to your question, but to make the leap calculation work you'll need to invert some of the conditions in that if statement -- I'll let you figure out the details yourself.
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
I have a case where I have to use if statement learning python:
if ([Team1_matches[0]>Team2_matches[0] and Team1_matches[1]>Team2_matches[1] and Team1_matches[2]>Team2_matches[2] and
Team1_matches[3]>Team2_matches[3] and Team1_matches[4]>Team2_matches[4]]):
winner="Team 1"
elif ([Team2_matches[0]>Team1_matches[0] and Team2_matches[1]>Team1_matches[1] and Team2_matches[2]>Team1_matches[2] and
Team2_matches[3]>Team1_matches[3] and Team2_matches[4]>Team1_matches[4] ]):
winner="Team 2"
else:winner="it was a draw or something went wrong"
It's always returning team 1 for some reason due to my poor combination, please advice how I can achieve the real value for winner, without having to if for many lines, if this is programmatically correct I will do it, just need advice
Without the braces
if Team1_matches[0]>Team2_matches[0] and Team1_matches[1]>Team2_matches[1] and Team1_matches[2]>Team2_matches[2] and
^
SyntaxError: invalid syntax
I am getting the input this way:
Team1_matches[0] = input("Enter the score that team 1 got on match 1? ")
As Rawing said in the comments, you have unneeded braces around your checks and non-empty lists are considered True. See the Python documentation for more info.
Try this:
Team1_matches=[1,2,3,4,5]
Team2_matches=[5,5,5,5,6]
if (Team1_matches[0]>Team2_matches[0] and Team1_matches[1]>Team2_matches[1] and Team1_matches[2]>Team2_matches[2] and Team1_matches[3]>Team2_matches[3] and Team1_matches[4]>Team2_matches[4]):
winner="Team 1"
elif (Team2_matches[0]>Team1_matches[0] and Team2_matches[1]>Team1_matches[1] and Team2_matches[2]>Team1_matches[2] and Team2_matches[3]>Team1_matches[3] and Team2_matches[4]>Team1_matches[4]):
winner="Team 2"
else:
winner="it was a draw or something went wrong"
print(winner)
>>> Team 2
The solution using built-in all function:
Team1_matches = [1,2,3,4,5]
Team2_matches = [2,3,4,5,6]
r = range(0, len(Team1_matches)) # range size
if all(Team1_matches[i] > Team2_matches[i] for i in r):
winner="Team 1"
elif all(Team2_matches[i] > Team1_matches[i] for i in r):
winner="Team 2"
else:
winner="it was a draw or something went wrong"
print(winner) # outputs: "Team 2"