python version: 2.71
I get a syntax error whenever I try to run it with IDLE....
horsemen = ["war", "famine", "pestilence", "death"]
i = 0
while i < 4:
print horsemen[i]
i = i + 1
The error occurs between horsemen and the equal sign
Is there any reason you cannot use a for loop?
for god in horsemen:
print god
The code is fine (syntactically). You must have a problem with your python environment. Also make sure it is properly indented.
Related
I'm trying to run this simple code block and it keeps throwing a syntax error and it's not pointing out where it's occuring.
I'm running python in a console so it won't let me type past the if statement:
for p in arange(2,3,0.01):
l1 = Line(f(x,p))
l2 = Line(g(x))
if len(intersection(l1,l2) == 1:
You have a ")" missing here.
Change line 4 as following:
if len(intersection(l1,l2)) == 1:
It should fix your problem.
def day_display_control(entered_variable,controlling_number,return_message):
if entered_variable == controlling_number:
return(return_message)
I have been trying to find a problem here, as IDLE keeps on giving an indentation error saying expected an indented block yet I have found none so far, my indentation width is 4 and I tried using only tab as well, haven't found the solution, thank you in advance as this is probably a really basic question.
P.S:
I have also tried to debug the rest of the code without this line, yet while this gives the same error:
def day_display():
day_display_number = day % 7
day_display = day_diplay_control(day_display_number,0,Monday)
day_display = day_diplay_control(day_display_number,1,Thuesday)
day_display = day_diplay_control(day_display_number,2,Wednesday)
day_display = day_diplay_control(day_display_number,3,Thursday)
day_display = day_diplay_control(day_display_number,4,Friday)
day_display = day_diplay_control(day_display_number,5,Saturday)
day_display = day_diplay_control(day_display_number,6,Sunday)
Do not mind the quality of the code, the problem is that previous 'def's don't cause this to happen, such as;
def typeWait(message,delay):
message = str(message)
print(message)
sleep(delay)
P.P.S: I have just realized, as of yesterday, that python was not 3.6 anymore, but instead 3.7 alpha 2, which leads me to believe that this is either a new feature or a bug, I have found no articles about either of them yet, so if anyone knows what the problem is, I would appriciate it a lot.
I agree with #jasonharper. Check if you mistyped = instead of ==, and also if any of these are missing: ), ] or }. It might not be near the block of these functions.
def reverse_number(x):
return x[::-1]
number_of_sums = int(input())
for i in range(number_of_sums):
s1 = input().split()
print(int(reverse_number(s1[0])) + int(reverse_number(s1[1])))
I have the following code, which should be a solution to a SPOJ problem. It compiles fine and works for examples that I provide, but as soon as I submit it says that it found an EOF error.
It points me to line 5 in the code, and I believe I understand why. When I use input() I try to get the whole line, right? So, how would I go ahead to only get the number? If that is the problem of course, it might be something else that ruins the code.
def reverse_number(x):
return x[::-1]
try:
number_of_sums = int(input())
for i in range(number_of_sums):
s1 = input().split()
if len(s1) > 0:
print(int(reverse_number(s1[0])) + int(reverse_number(s1[1])))
except ValueError:
print("Invalid Input")
Now, if you run the program and submit it will throw error stating "Invalid Input". Apart from adding the try block, also included the condition check for s1 as you might encounter an "Index out of range" error if not given proper input.
It may be that the grader/engine that checks the code is running Python2, in which case you could change to:
number_of_sums = int(raw_input())
before submitting (and a similar change on line 8).
I'm getting started with Codechef. I submitted following code in python to solve this question.
The code below works fine with codechef's online (python) IDE as well as my local IDE. But, when I submit it on codechef, it results in Runtime Error (NZEC). Can someone explain to me the reason behind this?
withdrawCash = int(input())
totalCash = int(input())
if withdrawCash < totalCash and withdrawCash % 5 is 0:
totalCash = (totalCash - withdrawCash) - 0.5
print(totalCash)
The problem supplies both the inputs in a single line. Your code waits for input in 2 lines. Change it to:
withdrawCash,totalCash = map(int,raw_input.split())
NZEC (Non Zero Exit Code) occurs when your code doesn't return zero on exit. It can happen due to a number of reasons, but if splitting the input doesn't solve the problem, add an exception for EOFerror. Just write:
try:
withdrawCash = int(input().split()) # raw_input in the case of Python 2
except EOFerror:
print ("exit") # this is jst a pseudo statement `
Use indentation properly. I am currently using an android app of stack exchange in which writing code is not so easy. codechef is pretty poor when it comes to Python. Switch to any other lang for CP.
Try directly submitting the code without running it on Codechef ide because it with me also it showed the same but when I submitted directly I got submitted successfully. so Directly submit your code.
For the last half hour I've been trying to figure out what is wrong with this code. It should be very straight forward. I've practically copied it out of the documentation at this point. But no matter what I try I receive a syntax error.
Here's the code:
def addfiles(folder):
foldercont = [os.path.normcase(f) for f in os.listdir(folder)]
for x in foldercont:
if os.path.isfile(x) == True:
files.append(os.path.realpath(x)
if os.path.isdir(x) == True:
addfiles(os.path.realpath(x))
Whenever I run this, i receive the error
if os.path.isdir(x) == True:
^
SyntaxError: invalid syntax
However, if I write the equivlent code in the interactive interpreter it runs fine.
Can this method simply not be used in an if loop or something?
Thanks for the help. I'm getting really frustrated at this point... heh.
There's a parenthesis missing at this line:
files.append(os.path.realpath(x)
^
Python complains about the True: bit because it's expecting a statement like
(x if condition else y)
As jcomeau_ictx says, you should also leave out the == True when checking for booleans:
if x:
do_something
if not y:
do_something_else
you're missing a close parentheses on the previous line.