How to use get keyboard input using Mac? (Python3) - python-3.x

I want to try and get a keyboard input and use it in a conditional statement; such as if 'a' is pressed do function, if 'b' is pressed do function b. The problem is, whenever I get keyboard output, this error gets displayed. raise OSError("Error 13 - Must be run as administrator")
OSError: Error 13 - Must be run as administrator. I looked at some other posts, but I couldn't find a working solution. [Btw I am using the keyboard module (But it makes the error pop up), If there are any better solutions please tell].
Il put a code example I found:
import keyboard
keyboard.write("GEEKS FOR GEEKS\n")
keyboard.press_and_release('shift + r, shift + k, \n')
keyboard.press_and_release('R, K')
keyboard.wait('Ctrl')
Note: I don't know how to get it to run, also if there is any problems in it, please tell a viable solution. Thanks!

Related

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.

Syntax Err GET on an Apple II, AppleDOS 3.3 using Applesoft

I am using the AppleWin emulator, setup as an Apple IIe, Apple DOS 3.3 with Applesoft. My goal is to make a simple game.
This simple GET program works fine at the ] prompt (I am not sure, but I think it's called the monitor).
10 GET V$
20 PRINT V$
It prints the pressed key as expected
Then I start Applesoft using LOAD APPLESOFT. I tried writing the same simple program than at the ] prompt. But this time when I type the first line 10 GET V$ I get as output *** SYNTAX ERR.
I think it's not a supported feature, but in the ApplesoftII Basic Programming Reference Manual they list get a reserved keyword.
I could update to a higher version of Applesoft, then which version of Applebasic would support it?
I can also use another method of getting a key press without the user needing to press enter afterwards.
Applesoft on the Apple ][e (the first apple version to support lower-case at all) is case sensitive, and all the keywords are UPPERCASE only -- so get will not match the keyword (or anything) and will give you a syntax error.
Of course, if you're emulating an original Apple ][, there's no lower case at all, and lower case letters show up as flashing uppercase -- and still won't match a keyword.
After continuing to research my issue, I have found the location of memory for the keyboard buffer.
The keyboard buffer is -16384 and the way to reset that value of that address is to access -16368.
If the value in -16384 is larger than 128 then a key has been pressed.
So, I can use PEEK -16384 to read the value. To get an ASCII code you need to subtract 128 to that value.
Example code:
KEY= PEEK(-16384)
IF KEY>=128 THEN PRINT KEY-128
To tell the system you dealt with the key press and reset the value, you need to access the value in -16368. By using PEEK or POKE.
Example code:
POKE -16368,0
Or can also be PEEKed
PRINT PEEK -16368
if the error you are getting is *** SYNTAX ERR and not "?SYNTAX ERROR" then you are using integer basic and not applesoft basic. Try to switch to applesoft basic first by using FP

How to get rid of the error 'EOFError: EOF when using input() after having used sys.stdin.readlines() in a different function?

I am trying to replicate an Excel VBA macro to Python for the sake of learning a new programming language and I am stuck at a point which Google alone is not helping (I guess I do not even know what to look for). Could you please give it a try?
What I expect the program to do:
When running the user should be prompted with a few options and if the input is 0 then it should ask for a multi line input containing a full HTML source code from this website Steam Tools
After the input the user is expected to hit CTRL+D / CTLR+Z to confirm there is nothing else to add (I think the problem is here, maybe it is not able to "clear" the EOF error while using input() again?)
Then as an ouput the program should return the first 10 rows delimited by comma and create another input() to avoid the window to autoclose.
What the issue is:
When I run it from the desktop (double cliking the .py file) it autocloses withouth creating the last input().
When I run it from PyCharm it runs OK and the last input remains waiting for user action. However, it does dump an error like this:
File "D:/Stuff/_root/py/Steam/steam_cards_manager.py", line 51, in z_parse_tbody
input('\nCopy the program output and type Back:') EOFError: EOF when reading a line
Any feedback is appreciated as I don't know if I am doing things in an easy / effcient way.
I've upload my .py file and also a sample HTML to make it easier to replicate the issue, hope it helps.
https://github.com/brmenezewe/db
It turned out that I had to replace the CTRL+D shortcut by a "trigger" word that when sent via a single input() it breaks the While loop and joins the inputs previously received:
def z_input_lines():
lines = []
while True:
line = input()
if not line or line.lower() != "go":
lines.append(line)
else:
break
return "".join(lines)

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.

Getting an invalid syntax <Unknown, line 6> error for print function w/ Pylint on VSCode

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.

Resources