python 3 basic string syntax error - python-3.x

I sincerely apologize as this question has been asked repeatedly and i understand that the answer is that in python 3 that the print has been made a function and that the string has to be encapsulated within parentheses. However, in IDLE, there is only 1 line of code and i have tried this one line with both single and double quotations and achieved the same "syntax error" Any help would be greatly appreciated.
If i run the same code in the shell, i have no issues!
Even if i use the basic help() command i get a "syntax error"

In a python file you don't need any of the things that are printed when the shell starts up. Your problem isn't with the print or the help, it's with the format of the file. Take out everything before the help() and that should fix your problem.

Related

Bash script throws syntax error unless echo statements are commented & uncommented

I have a simple bash script template file that I use to generate new bash scripts from python. Each time I change some values in a variable and then create a new copy of the template from python. After saving the file, I give it executable permission. Let me mention it now that I'm using pycharm as the editor. But when I run this newly generated bash script from the terminal it always gives me the following syntax error:
./job_load_8.sh: line 4: syntax error near unexpected token `('
When I open this newly created bash script file in pycharm and comment all echo statements and save the changes, then the script doesn't throw any errors. And when I go back and uncomment all my previously commented statements, then the script works exactly as expected. Clearly, pycharm is doing some auto-formatting that I'm missing. What is happening here ?
Edit: After many people pointed out that I haven't delved sufficient information, I'm giving more details now. Although, I have solved my issue, I'm mentioning more details in case someone else faces this issue and wants an answer.
Here is the python code that inserts certain lines in the bash script:
# at line number 11, insert a variable GPU_FLAG
lines.insert(11, "gpuFlag = " + str(gpu_flag) + "\r")
# at line number 12, insert a variable NUM_ITER
lines.insert(12, "gpuFlag = " + str(gpu_flag) + "\r")
# get the last set of spot file names from the cohort and save them in a list called spot_file_names separated by commas and add a newline
lines.insert(13, "spotFileNames = [" + ", ".join(["'" + spotFileNames[spot_cohort*(i//spot_cohort)+k] + "'" for k in range(i%spot_cohort+1)]) + "]\r")
And shown below is the result of diff between the two scripts, i.e., before and after commenting and uncommenting.
Answering here even though I have solved my issue in case someone in the future may be in need of a quick fix. As you can see that there are these extra ^M characters in the original which was not there in the fixed bash script. I have no idea where they come from. But once I remove them, my issue is solved. I don't know how to remove them from the python script so now I just insert after every two lines and that solves my issue. Somehow pycharm (practically every other editor) does this automatically when a file is saved after commenting and uncommenting. At least that's what I'm guessing is happening. Nevertheless, my issue is now solved and I've told why. Hope this helps.

Strange SyntaxError with if statement in python 3?

I keep trying to run a code for a game, but I keep getting a SyntaxError on my if statement, and in the terminal, it keeps pointing at the colon. I am not sure why, and I am just a beginner so I understand if it is weird. Note that the code, has no indentation problems, it is just the copy and pasting.
I've tried using integers and strings, but nothing works. I am not sure what to do.
while monster.enemyhealth>0:
print ("[Your Turn]")
print ("(1) Smash")
print ("(2) "+player.secondary_attack)
print ("(3) "+player.tertiary_attack)
print ("(4) Backpack")
print ("(5) Escape Battle")
battleinput=int(input(">>>")
if battleinput==1:
monster.enemyhealth-player.primary_attack.damage=monster.enemyhealth
print ("You did "+smash.damamge+" to the monster!")
player.powerpoints-player.primary_attack.powerpointcost=player.powerpoints
player.health-monster.attackpower=player.health
print ("The monster did "+str(monster.attackpower)+" to you!")
And here's what the error is:
File main.py, line 140
if battleinput==1:
↑
SyntaxError: invalid syntax
I expect it to just go over, and the program to run normally.
Are you used fixed spaces for indentation?
while conditions:
statements
if conditions:
statements
it seems you have not true indentation where use if

Python If else syntax

I am struggling with If Else statement in Python 3.5.2 which I recently installed. I am using IDLE to run my code.
Here is my code below (screenshot).
I get syntax error as soon as I press "Enter" key after "Else". I have tried playing with indentation, colon, etc but I am getting error on all combinations I have tried so far. I have seen similar questions (for prior version of python) here as well as on Quora but none of those solutions are working for me. Please help! (I am using MacBook - in case that changes anything).
Take the example of #3:
Python is seeing the code as
if x==10:
print("X is 10")
else:
The indentation on this is clearly messed up. But it looks right to you, because the >>> of the prompt is pushing the first line over a few characters, making it look like it is right. But even though it looks that way in the shell, python still sees if x==10: as being in col 0, and thinks that print("X is 10") is indented by 8 spaces and else: is indented by 4 spaces.
When you typed it in to the edit window, the indentation actually turned out differently, like so
if x==10:
print("X is 10")
else:
which is actually correct (ignoring the fact that there is nothing under else).
Dont indent(tab) 'else:' I think the cause is IDLE's ">>>" prompt is not aligned unlike in terminal
3rd example is almost the right one, but you must provide something on the else branch, either some code or pass (or just drop the else entirely)
EDIT:
After being explained the problem is with the IDLE interactive shell in specific, I tried it myself and the following code works:
This too (so the indentation seems to work on the if branch itself):
And this:
The shell automatic indentation is quiet confusing. not to say buggy.
It is clearer on plain python interactive mode.
The >>> adds a visual indentation, but there is no indentation there. The if body is indented by it's previous plus one, though this comes out as two indentations, it is allowed and causes no trouble.
The else statement needs to have the same indentation as the if - 0 indentation (and not one as the visalization hints). So you actually need to delete the automatically added indentation, of the else statement all the way back to the beginning.
Using Python 3.5.2 but not idle, this what it looks like when your code is run successfully:
>>> x = 8
>>> if x==10:
... print('X is 10')
... else:
... print('Not')
...
Not
>>>
The correct form is else: with the colon. This corresponds to your third attempt for which the error that see complains about indentation. I can't see what indentation you entered but change that until you figure out what idle wants.
"elif" is a shorter way of saying "else if". if the "if" statement isnt true, "elif" can be used to tell the computer what to do if the conditions are fulfilled

Python error expected an indented block

Hi again And sorry for my Mistake in Describe my problem.
this my little function so i saved in a file name nester.py
def print_lol(the_list):
for each_item in the_list:
if isinstance(each_item,list):
print_lol(each_item)
else:
print(each_item)
and when i want to use it and press F5. I faced with above error in Python 3.4.3 Shell.
You are mixing tabs and spaces in your code. All blocks should be indented by 4 spaces.

PHP heredoc parse error [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
This produces output page OK
$mystring = "<<<EOT";
Replacing it with the following produces
Parse error: syntax error, unexpected $end in file.php on line 737
$mystring = <<<EOT
This is some PHP text.
It is completely free
I can use "double quotes"
and 'single quotes',
plus $variables too, which will
be properly converted to their values,
you can even type EOT, as long as it
is not alone on a line, like this:
EOT;
Any ideas as to what is causing the parser to choke?
I'm using PHP 4.4.7.
It is only on one file that this behaviour happens all others follow the PHP defined functionality.
What I am trying to recitify is what could be possibly wrong in the proceding lines so that the PHP parser shows in this failure.
John
changed file contents to :-
<?php
$mystring = <<<WHATEVER
This is some PHP text.
WHATEVER;
?>
result =
Parse error: syntax error, unexpected $end in file.php on line 5
Any clues
EDIT
original error was to do with T_ENCAPSED_AND_WHITESPACE this can be caused with jQuery for example "if(x == y){$('#my_image').hide():}" is within the heredoc the bigram "{$ will start the parser looking for php variable for substitution.
EDIT
2 good responses.
1) Ch4m3l3on - "<?php" vs "<?" handling.
2) The Disintegrator - <q>had a similar problem with a stupid program that insisted in putting the BOM in a utf-8 file (ignoring preferences)</q>.
EDIT
1) Replacing all content with a single block didn't fix the problem or give any other pointers.
2) No BOM (Byte Order Mark), pity as this or similar majic characters would have explained all symptoms perfectly.
you have to place your ending heredoc at the beginning of line. if you use some IDE that have indentation, remove them! your ending heredoc must be vertically in the same line as your ending php tag(
Make sure that there is nothing after the 'WHATEVER;'. Even a space will give a parse error.
I would delete the line and retype it, hitting <enter> immediately after typing the semi-colon.
make sure that EOT; is really at the begin of the line.
if ($muh="kuh") {
$foo = <<<EOT
some text text text
EOT;
}
What if you try:
$mystring = <<<'EOT'
...
EOT;
(Notice the single quotes around the first EOT)
I just copy/pasted that into a file and it ran without errors. (PHP 5.2.8 (cli) (built: Feb 6 2009 12:33:08))
So the problem is likely something near that code, but not something you included in the question.
Either that, or a change in PHP since your version was built.
You probably should check if you have any unclosed curly bracket (or brace, can't remember).
For example:
<?php
while(true) {
echo "Something\n";
?>
Will produce that error.
I don't have the rep for an uptic, but Ch4m3l3on and Pedro Cunhna are right... an incorrect heredoc probably won't cause that unexpected $end error, but an unclosed curly brace definitely would.

Resources