Syntax error during running my Python code - syntax-error

Just started learning Python as beginner and I found it fun. But it gave me strange errors mentioned below. I'm learning from a book and the code there is written as:
fish="basss"
if fish=="bass":
print('super')
else:
print('bla')
It gave me a syntax error and I couldn't understood the reason. I have written my code as same as written in book. I've searched in google, but couldn't find anything. I'm using the latest version of python and I typed this in Python shell.

Python is sensitive to indentation. Your code should be indented like this:
fish="basss"
if fish=="bass":
print('super')
else:
print('bla')

because your else block isn't aligned with your if block, try un-indenting the else.
fish = "basss"
if fish == "bass":
print('super')
else:
print('bla')

Python differentiates between code blocks by indentation. It is very important to have correct indentation in your code.
if fish=="bass":
print('super')
else:
print('bla')
if and else have to be at the same level of indentation. All statements to be executed within each of those conditionals have to be indented again.

There is an indentation error , you have to do like this,
fish="basss"
if fish=="bass":
print('super')
else:
print('bla')
Always else block should be under the same indentation level of if or elif statements.

You have to type it on multiple lines. The if and else statements cannot be on the same line. This causes an error, and so they must be typed in separate lines.

Related

Binarization in Spyder(Python 3.9) code using sk-learn(preprocessing) and NumPy

I attach a screenshot of the code
I am new to Python and am currently studying artificial intelligence, working in Spyder(python 3.9)
After executing the code, I expected this output
Binarized data:
[[1.о.1.]
[о.1.о.]
[1.о.о.]
[1.о.о.]]
In Python it is important to write one command in one line:
data_binarized = preprocessing.Binarizer(your_code)
If you want to write it in two lines, you can use implicit line continuation (Possible only inside parentheses, brackets and braces):
data_binarized = preprocessing.Binarizer(
your_code)
As an second possible option you can use the backslash (explicit line continuation):
data_binarized =\
preprocessing.Binarizer(your_code)
For more information about this look at this answer:
https://stackoverflow.com/a/4172465/21187993

Scripting languages execute the line which has an error and don't output anything or it don't execute it at all?

Do scripting languages execute the line which has an error and don't output anything or it don't execute it at all?
Like what is the process when scripting languages have an error line in the code?
I know it doesn't execute it right or wrong?
That depends on the nature of the error. (It also might depend on the language or interpreter.)
If the line has a syntax error (or other error that the interpreter treats similarly), the interpreter will almost certainly not execute the line. In fact, it might not execute any lines. (It's possible that an interpreter could 'repair' a syntax error and then execute the line, but I don't think that's common.)
If the line has a different kind of error, the interpreter will execute the line until it encounters the error.

PyCharm Selenium not openening Chrome, "Process finished with exit code 0" [duplicate]

I am new to PyCharm and I have 'Process finished with exit code 0' instead of getting (683, 11) as a result (please see attachment), could you guys help me out please? Much appreciate it!
That is good news! It means that there is no error with your code. You have run it right through and there is nothing wrong with it. Pycharm returns 0 when it has found no errors (plus any output you give it) and returns 1 as well as an error message when it encounters errors.
Editors and scripts do not behave like the interactive terminal, when you run a function it does not automatically show the the result. You need to actually tell it to do it yourself.
Generally you just print the results.
If you use print(data.shape) it should return what you expect with the success message Process finished with exit code 0.
exit code 0 means you code run with no error.
Let's give a error code for example(clearly in the below image): in below code, the variable lst is an empty list,
but we get the 5 member in it(which not exists), so the program throws IndexError, and exit 1 which means there is error with the code.
You can also define exit code for analysis, for example:
ERROR_USERNAME, ERROR_PASSWORD, RIGHT_CODE = 683, 11, 0
right_name, right_password = 'xy', 'xy'
name, password = 'xy', 'wrong_password'
if name != right_name:
exit(ERROR_USERNAME)
if password != right_password:
exit(ERROR_PASSWORD)
exit(RIGHT_CODE)
I would recommend you to read up onexit codes.
exit 0 means no error.
exit 1 means there is some error in your code.
This is not pyCharm or python specific. This is a very common practice in most of the programming languages. Where exit 0 means the successful execution of the program and a non zero exit code indicates an error.
Almost all the program(C++/python/java..) return 0 if it runs successful.That isn't specific to pycharm or python.
In program there is no need to invoke exit function explicitly when it runs success it invoke exit(0) by default, invoke exit(not_zero_num) when runs failed.
You can also invoke exit function with different code(num) for analysis.
You can also see https://en.wikipedia.org/wiki/Exit_(system_call) for more details.
What worked for me when this happened was to go to
Run --> Edit Configurations --> Execution --> check the box Run with
Python Console (which was unchecked).
This means that the compilation was successful (no errors). PyCharm and command prompt (Windows OS), terminal (Ubuntu) don't work the same way. PyCharm is an editor and if you want to print something, you explicitly have to write the print statement:
print(whatever_you_want_to_print)
In your case,
print(data.shape)
I think there's no problem in your code and you could find your print results (and other outputs) in the tab 5: Debug rather than 4: Run.
I just ran into this, but couldn't even run a simple print('hello world') function.
Turns out Comodo's Firewall was stopping the script from printing. This is a pretty easy fix by deleting Python out of the Settings > Advanced > Script Analysis portion of Comodo.
Good Luck
I had same problem with yours. And I finally solve it
I see you are trying to run code "Kaggle - BreastCancer.py"
but your pycharm try to run "Breast.py" instead of your code.
(I think Breast.py only contains functions so pycharm can run without showing any result)
Check on tab [Run] which code you are trying to run.
Your starting the program's run from a different file than you have open there. In Run (alt+shift+F10), set the python file you would like to run or debug.

This code is directly from a textbook for Cisco Devnet yet presents a syntax error in python 3.8.1 shell. Why? I tried viewing text in notepad++

while True:
string = input('Enter some text to print. \nType "done" to quit>')
if string == 'done':
break
print(string)
print('Done!')
SyntaxError: invalid syntax
image of issue
Image after idz's suggestion
I think your problem is that you wrote:
while true:
instead of
while True:
However, if you are using Python 2 you should be aware that input will attempt to evaluate what you type in as Python. Depending on what your aim is, you may want to use raw_input instead. This is not an issue if your are using Python 3.

Output to stderr in REBOL2?

I am trying to get my CGI scripts running on my web host (which runs on FreeBSD). To debug why I keep getting the dreaded "premature end of script headers" error, their support recommended that I redirect all my output to stderr, rather than printing it. Looking up how to do this, I came across a very old RAMBO ticket about it, but it looks like it was never implemented.
Per some of the answers to this question, it seems like I should be able to do a call {echo Hello, world >&2} to achieve this, but it doesn't work.
How can I write to stderr in REBOL2?
For my CGI-specific scenario, I have a truly awful workaround. Since writing to stderr in Perl (with which I am entirely unfamiliar) is a one-liner, I'm currently calling the REBOL script from Perl and printing its output to stderr from there:
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
# Note the backticks
my $the_string = `/home/public/rebol -csw test-reb.cgi`;
print STDERR $the_string;
This webpage has some suggestions http://www.liquidweb.com/kb/apache-error-premature-end-of-script-headers/
to solve your real problem. Perhaps you did not have the headers printed as first thing in your script, this must be the first thing to do. Maybe the rights are not sufficient, or the .r file type was not properly added in your .htaccess as cgi able file. Your (correct!) rebol core exe has not the correct rights. Or your script ends up in an endless loop?
Some hints to redirect errors for Rebol cgi script:
http://www.rebol.com/docs/core23/rebolcore-2.html#section-6.2
Better late than never... I've just implemented it for Rebol3 in my Rebol fork.
https://github.com/Oldes/Rebol-issues/issues/2468
The syntax will be probably changed a little bit, because I don't like that the system console port is named input, although it is not just for the input.
So far it is:
print 1 ;<- std_out
modify system/ports/input 'error on
print 2 ;<- std_err
modify system/ports/input 'error off
print 3 ;<- std_out