Getting an invalid syntax <Unknown, line 6> error for print function w/ Pylint on VSCode - python-3.x

For my weekly course assignment, we're tasked w/ creating simple user entry points for a fictional employee. I ran the code once and everything worked out as I needed it to w/ it asking the user to input data, once input it presented it w/ a title. I forgot to add a field for the employeeName and the "----" lines, so I added all of that in, copy and pasting some of it to get done as the code is highly similar. I then got the syntax error. I checked my spelling for print, deleted the print function, and then used VS Code intellisense to autofill the function, no changes to the error, went through the line to see if I forgot something or added extra nonsense. I looked through similar posts, but they aren't quite getting at my specific issue from what I can tell. Any help is much appreciated.
Regards
# user input for an employee's SSN
print("Please enter your SSN #:", end=' ')
employeeSSN = input(str())
print('SSN:', employeeSSN)
print("PLease enter your SSN #:", end=' ')
^
SyntaxError: invalid syntax
Expected results should print the string inside quotations and allow for user input.

I found my own error after some more checking and testing. I forgot to put in an ending parenthesis for line 4 of my code. I found it by taking away the last set of things I worked on and retesting to a good point, then slowly re-adding in the rest. I noticed the missing parenthesis in that process. I wish the error would have pointed my towards that missing part in line 4 then on line 6 and the function. Had me thinking something totally different.
Example of code mess-up
print("----------------------------", employeeName, "----------------------------"
How I fixed it
print("----------------------------", employeeName, "----------------------------")
Sometimes, its the simplest things right in our face. Hopefully this helps someone else.

Related

explain what is missing from 10print

10 PRINT CHR$(205.5+RND+(1));:GOTO 10
I seem to be the only person ever to not be able to do this. i have written all kinds of BASIC but I can't do anything with this. What am i missing?
I get "SYNTAX ERROR IN 10"
i can't get it to work And really, why would it? 205 is a memory address and so the value of 5 in and we have random number thing going but where does the pattern come from. Don't we need? "/" "\ ".
205.5 produces these/??? that doesn't make any sense. I must have an extreme misunderstanding of computers and BASIC that curiously doesn't prevent me from creating functioning programs.
I also program in C and I am still completely clueless. never seen anything like this work seems like there is a hidden line 1,2,3,4,5,6,7,8,9, that wasn't listed so I don't know what the rest of the code is???
I've tried on the 64mini and several emulators and they all give the same error.
Did I have a stroke and need to goto the hospital? maybe I can no longer discern what I see on the screen Lol.
10 PRINT CHR$(205.5+RND+(1));:GOTO 10
As pointed out by teapot418, you have a typo in your code. The exact code is:
10 PRINT CHR$(205.5+RND(1)); : GOTO 10
CHR$ creates the ASCII character for the given number. (What you wrote about reading from address is PEEK.)
CHR$(205) gives you the "\" and CHR$(206) the "/". The floating part of the number gets ignored. Because of the randomness, you get random sequences of CHR$(205) and CHR$(206) which creates the maze. The ; at the end of PRINT avoids a newline.

I get a TypeError in code typed "list indices must be integers or slices, not str" and no error in same code copied and pasted. Why?

I make a war card game. I wrote first code myself and it showed error below, then from frustration I typed from the answer of my course. AND IT SHOWS the same message:
if card1["Power"] == card2["Power"]:
~~~~~^^^^^^^^^
TypeError: list indices must be integers or slices, not str
On other hand I have a file with the same code but pasted from the answer sheet. And it works fine.
This is my git repository. https://github.com/Mind-revolt/War-card_game.py
How do I make this typed file work without copying and pasting?
Do you see where I've made a mistake??
Thanks guys!
I tried typing the code myself first without looking in the answers. Then after a while of frustration, I typed the "right" code lines. And it doesn't work at the same line as it did before I typed from the answer sheet. I looked at the two files many times, and I didn't see a mistake. It should work. Then why it doesn't. Maybe I'm blind I don't know.

Confusing indentation issue

I'm using an API from this GIT repository to control a JDS6600 signal generator.
Now some of the commands work & some dont due I think to the unit I'm using being slightly different to the one used by the author of the API.
So to fix this I tried to see what data the device was sending back before an error is raised. I thought a simple print(data) would do it but I keep getting an indentation error, sorry screen shots but I think you get a better idea from a picture in this case.
First with the newly added print(data), note little red cross on the left
Second without,
So how come I'm getting this error? No matter where I place that print(data) within the method I get an indentation error & the code falls over.
EDIT: The problem was fixed running reindent on the offending file, which converts all the tabs to spaces. As pointed out by Tyberius in a comment below the file was a mix of tabs & spaces. It seems Spyder has a unique attitude to such cases.
You're seeing an error highlighted by the IDE (lint error), not an actual python error (you would get it after running and seeing a traceback).
In general anything after : is expected to be in the next line and IDE is probably making that assumption.
The general style guide is defined by PEP-8 and it covers compound statements. The way you're trying to use is generally considered within the "rather not" section.
So really it should be
if a not in (0, 1):
raise RuntimeError(a)
print(data)
If you would use something like black autoformatter it would sort it out for you.
The line will actually fail. For example, look below:
if a in [0.5,0.4]:
raise TypeError(a)
print("what")
File "<stdin>", line 3
print("what")
^
SyntaxError: invalid syntax
Now, why does this happen?
answer is mentioned here. Basically, as you define the error in the previous line, the error gets caught in the print. For example, it is technically similar to the following:
>>> while True print('Hello world')
File "<stdin>", line 1
while True print('Hello world')
^
SyntaxError: invalid syntax
So that is why, your print is raising the error instead of the previous line. Also, there is no meaning of printing after you raise the error, as it will never get executed as the error will halt the execution. So the solution will be to put the print statement before the raise.

Python script in Colab keeps throwing error message: IndentationError: unindent does not match any outer indentation level

I'm currently using Google Colab in order to take advantage of its free GPU. I was trying to modify a code that I copy and pasted from machinelearningmaster.com. However, whenever I try to add a new code line, for example "print("some words"), I get an indention error.
I have tried adding tabs or spaces before the print call but I still get the error. for example:
space,space,print("some words")
tab, tab ,print("some words")
I have also checked the colab editor settings, currently the indention width setting are set to two spaces.
The first three lines are part of the original code, the print statement is
my addition. I copy and pasted this directly from the colab editor. In Colab all four lines are aligned. As you can see here only the first three lines are aligned. I don't know what's going on.
img_path = images_dir + filename
ann_path = annotations_dir + image_id + '.xml'
count=count+1
print("this is count: ", count)
I expected this to print the value of count, instead I get an error message telling me:
IndentationError: unindent does not match any outer indentation level
Okay, after much searching and frustration, I have an idea of what went wrong, but even better, a solution to fix it.
It appears that the Google Collaborator (Colab) editor does not have a way to set it for tabs "\t" versus space (space-bar entries). From the settings tab on the cell you can set the width of the tab from 2 to 4, but these will be interpreted as 2 to 4 space-bar entries. Usually, this isn't a problem. However, if you're like me and you want to test out code from the web, or be lazy and just copy paste from your editor, problems can arise.
Here's how I fixed it. Before pasting the copied code into Colab, first put it into notepad++. Go to View> Show Symbols >Show All Characters, click on this, you should now be able so see all the characters in the code. Find a tab, it will look like an arrow pointing to the right -->, right click and copy it. Open Search> Find, open the Replace tab. Depending on your version of notepad++ the tab you copied will automatically be entered and the replace will already be set to four spaces. Hit "Replace all". This will automatically replace all tabs with equivalent spaces. Copy the code from notepad++ back to Colab. Now there will be no more conflicts.
I think using a simple find and replace tool will just work fine. I also came across this error recently in Colab and I went through #Rice Man solution. The only difference was I used Libre office writer instead of Notepad++. I also found this tool to be helpful. I am not proficient in using Colab but this solution worked for me.
Another quick fix that worked for me related to this question.
I was trying to run a python script in colab and faced this error though the line seems at an appropriate indentation in that script.
I checked with the !cat filename.py cmd, and found out that the actual indentation appears different than it is in the script (hence the error).
Taking that unindented line (according to the colab) at the start of the line and using space afterward fixed the error.
I used this website to fix the error.
Copy your code to the site, then click beautify button on top left. This will remove indention errors.
If you want to know where the indention error is coming from, use #Prachi answer.

VBA string comparison failure

i met interesting issue when im comparing two strings. Im reading data from file and everything works well. But then co-worker send me input file, which is just CTRL+C and CTRL+V of working file. And then miracle happend! VBA is so confused, that cant compare two simple strings and i fell of chair.
If you take a look at image you can see that comparison passed if condition where are two same strings, but it should not. Im a bit confused how this can happen.
So met someone something like this? Im realy start thinking about something like machine revolution from Terminator. (files are both saved in notepad++ and there are no strange characters or something like that)
Progress update
So i tried hints from guys in comments below. and ended with something like this
If CStr(Trim(rowArray(4))) <> (CStr("N/A")) Then
Contentent of rowArray(4) is still "N/A" string as on picture above and excel still thinks this strings arent same. I also saved file in pspad, netbeans, and normal notepad and issue is still same.
Use the immediate window to test the contents of the variable:
For i = 1 To Len(rowArray(4)): Print Asc(Mid(rowArray(4), i, 1)): Next
This will print the ASCII value of each character in the string - you can use this to determine what the extra character(s) are causing the issue.

Resources