Is there something about the os module I'm not getting? - python

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.

Related

Don't understand why I get an "Expected an indented block" error with Pyomo

I would like to use the following code from Pyomo Accessing solver status and termination conditions
results = opt.solve(instance) # Solving a model instance
instance.load(results) # Loading solution into results object
if (results.solver.status == SolverStatus.ok) and (results.solver.termination_condition == TerminationCondition.optimal):
# Do something when the solution in optimal and feasible
elif (results.solver.termination_condition == TerminationCondition.infeasible):
# Do something when model in infeasible
else:
# Something else is wrong
print “Solver Status: ”, result.solver.status
Hoever, I get an error saying Expected an indented block at the elif. When inserting an indented block, I get the error Invalid syntax. I posted a screenshot of both cases. I do not understand why I get this error? I just copied and pasted the code from the official pyomo website. Do you have any idea why I am getting this error and how I can get rid of it?
You likely need to have at least 1 line of executable code within each if or elif block. Right now, you just have a comment line.
While you are "shelling out" the program, just put the command pass in each block and see if that helps. So:
if (something >= something_else):
# do something
pass
else:
# do the other thing
pass
....
When code is laid out using whitespace like python, you need something actually in the block to show that it's there. A comment isn't enough as these are ignored.
Your code currently looks like:
if ... :
# comment where block should be
elif ... :
print "something"
The comment doesn't count as an indented block.
If you really have no code to put in there yet, you can use the no-op statement pass:
if ... :
# todo
pass
elif ... :
print "something"

Can anyone help this beginner figure out this basic Python issue?

I'm new to Python and started practicing and have been doing okay until I got stuck on this problem. I think the 'else' statement is the issue but I've tried many variations and can't figure it out. Any advice is most welcome to this noob. Here is a screenshot of the error and code. Thanks! https://i.stack.imgur.com/hLzrf.png
use keyword elif instead of else. else does not need any expression
Use elif instead of else because else doesn't use expressions after it and put else after the elif containing a error message or something. You cannot put something like this:
if a > b:
elif a < b:
because there is no else statement for if a and b are equal.
You are using the else statement in the wrong way, because it cannot contain a condition. If you want to add a second condition use elif instead. And use else only when you want to define a statement to execute when no other if statement was True.
if 1 == 1:
# your statement
elif 1 == 2:
# your other statement
else:
# your statement if none of the above was true

Indentation Error in the Usage of 'def' in Python 3.7

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.

EOF error python 3

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

Syntax Error with lists in Python

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.

Categories

Resources