Pyflakes returning erroneous syntax error - python

I'm writing a program that generates a random playing card and draws it in the .stdout terminal.
It keeps giving me SyntaxError: invalid syntax when I triple checked my code and everything is correct.
Input:
def drawCard():
#cs and cr are global variables I declared in another function, so please disregard those.
#also, Italic(), Bold(), etc are custom escape methods I made to format my text.
#The indexing on cr and cs is to print the first character in the string (they are strings).
cardlayout = f" ______\n|{cr[0]}| |\n|{Bold(Underline(cs[0]))}{End()}| |\n| {Italic('0' + cr)}{End()} _|\n| |{Bold(cs[0])}{End()}|\n|____{Underline('|' + cr[0])}{End()}|"
if cr == '10':
cardlayout = f" ______\n|{cr}| |\n|{Bold(Underline(cs[0]))}{End()}| |\n| {Italic(cr)}{End()} _|\n| |{Bold(cs[0])}{End()}|\n|{Underline(' |' + cr)}{End()}|"
if cr == 'Ace' or cr == 'Jack' or cr == 'Queen' or cr == 'King':
cardlayout = f" ______\n|{cr[0]}| |\n|{Bold(Underline(cs[0]))}{End()}| |\n| {Italic("a ")}{End()}_|\n| |{Bold(cs[0])}{End()}|\n|____{Underline('|J')}{End()}|"
print(cardlayout)
Output:
File "main.py", line 24
cardlayout = f" ______\n|{cr[0]}| |\n|{Bold(Underline(cs[0]))}{End()}| |\n| {Italic("a ")}{End()}_|\n| |{Bold(cs[0])}{End()}|\n|____{Underline('|J')}{End()}|"
^
SyntaxError: invalid syntax
How is my syntax invalid here?

Related

replace trademark symbol (™) when alone

I'm trying to remove trademark symbol (™) but only in the case it's not followed by any other symbol for instance I might have ’ which is a bad encoding of quotation mark (') so I don't want to remove trademark symbol (™) and hence broking the pattern that i'm using to replace xx™ with quotation mark.
dict = {};
chars = {
'\xe2\x84\xa2': '', # ™
'\xe2\x80\x99': "'", # ’
}
def stats_change(char, number):
if dict.has_key(char):
dict[char] = dict[char]+number
else:
dict[char] = number # Add new entry
def replace_chars(match):
char = match.group(0)
stats_change(char,1)
return chars[char]
i, nmatches = re.subn("(\\" + '|\\'.join(chars.keys()) + ")", replace_chars, i)
count_matches += nmatches
Input: foo™ oof
Output: foo oof
Input: o’f oof
Output: o'f oof
Any suggestions ?

When using os.startfile file is not found

import os # Tuo os moduuli
print("Valitse suoritettava luokka:"); #Tulosta...
print("1 = Kertolasku");
print("2 = Osamaara");
print("3 = Vertailu");
print("4 = Floydin kolmio");
print("5 = Kertotaulu");
print("6 = Viikonpaiva");
print("7 = Rivit");
v = int(input("Valitse:")) # v = seuraava numero
if v == 1: # jos v on yhtäkuin 1 niin avaa...
os.startfile(r'%USERPROFILE%\Desktop\JAVA\BATCH\x01.bat')
if v == 2:
os.startfile(r'%USERPROFILE%\Desktop\JAVA\BATCH\x02.bat')
if v == 3:
os.startfile(r'%USERPROFILE%\Desktop\JAVA\BATCH\x03.bat')
if v == 4:
os.startfile(r'%USERPROFILE%\Desktop\JAVA\BATCH\x04.bat')
if v == 5:
os.startfile(r'%USERPROFILE%\Desktop\JAVA\BATCH\x05.bat')
if v == 21:
os.startfile(r'%USERPROFILE%\Desktop\JAVA\BATCH\x06.bat') #Secret
if v == 1942:
os.startfile(r'%USERPROFILE%\Desktop\JAVA\BATCH\x07.bat') #Secret
if v == 6:
os.startfile(r'%USERPROFILE%\Desktop\JAVA\BATCH\x08.bat')
if v == 7:
os.startfile(r'%USERPROFILE%\Desktop\JAVA\BATCH\x09.bat')
else: # muutoin tulosta...
print ("VIRHE - Onko syötetty luku 1-5? Onko JAVA kansio sijainnissa %USERPROFILE%\Desktop ?");
print ("VIRHE - Onko Java asennettu? Onko OS = Windows?");
print ("VIRHE - Onko Java:n path variable asetettu oikein?");
input('Paina ENTER sammuttaaksesi...') #Lisää sulkeva komento.
This code returns the following error:
Error message
I have checked that file named x01.bat exists in:%USERPROFILE%\Desktop\JAVA\BATCH
I have tried to replace \ with / but windows doesn't recognize / as an way to access a directory.
I have tried to escape the backslash with another backslash. It only makes the same error with three backslashes instead of 2. (Yes I removed r before ' when I escaped a backslash with a backslash.)
I even tried to escape a backslash with a backslash before a backslash but error is the same with more backslashes.
Verdict: It seems that Python 3.6 on Windows 10 wants to add a backslash before a backslash no matter what I do. Because of that windows doesn't know where the .bat file is.
Any help is appreciated, please share your ideas!
For constructing paths in python, it is generally easier to use os.path.join
So something like
os.startfile(os.path.join(os.environ['USERPROFILE'], 'Desktop', 'JAVA', 'BATCH', 'x01.bat'))

Python Turtle write() syntax error

I have a Problem. I am learning Python i get a EOL Error. I searching for a solution but i find nothing.
def gefangen(mx,my):
if not geschafft:
paula.write('Super, du hast Paula erwischt!')
else:
paula.write('Das zählt ja wohl nicht?!')
onclick(start) # Mauskoordinaten werden
paula.onclick(gefangen) # automatisch übergeben
while paula.distance(horst) > 4:
horst.fd(randint(3,7))
horst.lt(randint(-20,40)/10)
paula.setheading(paula.towards(horst))
paula.fd(randint(1,10))
if count % 20 == 0:
horst.stamp()
paula.stamp()
count += 1
if horst.ycor()<-260 or horst.xcor()>310:
horst.write('Puh, geschafft!')
paula.write('Mist, entwischt!')
geschafft=True
break
if not geschafft:
horst.write("Erwischt!",font=("Arial",14,"bold")")
pu()
goto(-200,-200)
shape("circle")
write("Noch einmal? Hier klicken")
pd()
st()
http://prntscr.com/gstq0d i get this error (look screenshot)
It looks like you have an extra double quote at "bold")") ...
if not geschafft:
horst.write("Erwischt!",font=("Arial",14,"bold")")
horst.write("Erwischt!",font=("Arial",14,"bold")")
In this line, You have given an extra double quotes at the end.

How to decrypt in Python?

I am trying to decrypt a file in Python that I encrypted with another program. Some letters are correctly decrypted while others are not. I am not sure what is going on. All I essentially did was reverse the code for the decryption files. I think it has to do with the way it is iterating through the text, but I am not sure how to fix it.
Here is my decryption code:
decryption_library = {'%':'A','9':'a','#':'B','#':'b','1':'C','2':'c','3':'D','4':'d',
'5':'E','6':'e','7':'F','8':'f','0':'G','}':'g','{':'H',']':'h','[':'I',',':'i',
'.':'J','>':'j','<':'K','/':'k','0':'L','\-':'l','\"':'M',':':'m',';':'N',
'+':'n','$':'O','-':'o','$':'Q','%':'q','^':'R','&':'r','*':'S',
'(':'s',')':'T','~':'t','`':'U','5':'u','\\':'V','+':'v','=':'W','7':'w',
'~':'X',')':'x','2':'Y','*':'y',']':'Z','8':'z'}
orig_file = open('ENCRYPTED_Plain_Text_File.txt','r')
file_read = orig_file.read()
orig_file.close()
encrypt_file = open('DECRYPTED_Plain_Text_File.txt','w')
for ch in file_read:
if ch in decryption_library:
encrypt_file.write(decryption_library[ch])
else:
encrypt_file.write(ch)
encrypt_file.close()
encrypt_file = open('ENCRYPTED_Plain_Text_File.txt','r')
file_read = encrypt_file.read()
encrypt_file.close()
codes_items = decryption_library.items()
for ch in file_read:
if not ch in decryption_library.values() or ch == '.' or ch == ',' or ch == '!':
print(ch)
else:
for k,v in codes_items:
if ch == v and ch != '.':
print(k,end='')
Here is the encrypted text:
)]6 ^-94 ;-~ )9/6+
#2 ^$#5^) 7^$*)
)7- &-94( 4,+6&}64 ,+ 9 *6\-\--7 7--4,
%+4 (-&&* [ 2-5\-4 +-~ ~&9+6\- #-~]
%+4 #6 -+6 ~&9+6\-6&, \--+} [ (~--4
%+4 \---/64 4-7+ -+6 9( 89& 9( [ 2-5\-4
)- 7]6&6 ,~ #6+~ ,+ ~]6 5+46&}&-7~];
Here is what it should be:
The Road Not Taken
BY ROBERT FROST
Two roads diverged in a yellow wood,
And sorry I could not travel both
And be one traveler, long I stood
And looked down one as far as I could
To where it bent in the undergrowth;
Here is what it decrypts to:
xZe Road NoX xakev
BY RQBuRx wRQyx
xwo roads diverged iv a yeVoVoow woodi
qvd sorry I YouVod voX XraveVo boXZ
qvd be ove XraveVoeri Voovg I sXood
qvd Voooked dowv ove as zar as I YouVod
xo wZere iX bevX iv XZe uvdergrowXZN
Your decryption_library is not correct. F.e for index ')' you have value 'T' and also 'x'

Python + getopt - Problems with parsing

I have some problems with writing some Gerrit http://code.google.com/p/gerrit/ hooks.
http://gerrit.googlecode.com/svn/documentation/2.2.0/config-hooks.html
If I parse the command line for
patchset-created --change --change-url --project --branch --uploader --commit --patchset
def main():
if (len(sys.argv) < 2):
showUsage()
exit()
if (sys.argv[1] == 'update-projects'):
updateProjects()
exit()
need = ['action=', 'change=', 'change-url=', 'commit=', 'project=', 'branch=', 'uploader=',
'patchset=', 'abandoner=', 'reason=', 'submitter=', 'comment=', 'CRVW=', 'VRIF=' , 'patchset=' , 'restorer=', 'author=']
print sys.argv[1:]
print '-----'
optlist, args = getopt.getopt(sys.argv[1:], '', need)
id = url = hash = who = comment = reason = codeReview = verified = restorer = ''
print optlist
for o, a in optlist:
if o == '--change': id = a
elif o == '--change-url': url = a
elif o == '--commit': hash = a
elif o == '--action': what = a
elif o == '--uploader': who = a
elif o == '--submitter': who = a
elif o == '--abandoner': who = a
elif o == '--author' : who = a
elif o == '--branch': branch = a
elif o == '--comment': comment = a
elif o == '--CRVW' : codeReview = a
elif o == '--VRIF' : verified = a
elif o == '--patchset' : patchset = a
elif o == '--restorer' : who = a
elif o == '--reason' : reason = a
Command line input:
--change I87f7802d438d5640779daa9ac8196aeb3eec8c2a
--change-url http://<hostname>:8080/308
--project private/bar
--branch master
--uploader xxxxxxx-xxxxx xxxxxxx (xxxxxxxxxxxxx.xxxxxxx#xxx-xxxx.xx)
--commit 49aae9befaf27a5fede51b498f0660199f47b899 --patchset 1
print sys.argv[1:]
['--action', 'new',
'--change','I87f7802d438d5640779daa9ac8196aeb3eec8c2a',
'--change-url',
'http://<hostname>:8080/308',
'--project', 'private/bar',
'--branch', 'master',
'--uploader', 'xxxxxxx-xxxxx', 'xxxxxxx', '(xxxxxxxxxxxxx.xxxxxxx#xxx-xxxx.xx)',
'--commit', '49aae9befaf27a5fede51b498f0660199f47b899',
'--patchset', '1']
print optlist
[('--action', 'new'),
('--change', 'I87f7802d438d5640779daa9ac8196aeb3eec8c2a'),
('--change-url', 'http://<hostname>:8080/308'),
('--project', 'private/bar'),
('--branch', 'master'),
('--uploader', 'xxxxxxx-xxxxx')]
I don't know why the script generates
'--uploader', 'xxxxxxx-xxxxx', 'xxxxxxx', '(xxxxxxxxxxxxx.xxxxxxx#xxx-xxxx.xx)'
and not
'--uploader', 'xxxxxxx-xxxxx xxxxxxx (xxxxxxxxxxxxx.xxxxxxx#xxx-xxxx.xx)'
because so the script dont't parse --commit --patchset ...
When I parse comment-added all things works:
Command line input:
-change I87f7802d438d5640779daa9ac8196aeb3eec8c2a
--change-url http://<hostname>.intra:8080/308
--project private/bar
--branch master
--author xxxxxxx-xxxxx xxxxxxx (xxxxxxxxxxxxx.xxxxxxx#xxx-xxxx.xx)
--commit 49aae9befaf27a5fede51b498f0660199f47b899
--comment asdf
--CRVW 0
--VRIF 0
print sys.argv[1:]
'--action', 'comment',
'--change', 'I87f7802d438d5640779daa9ac8196aeb3eec8c2a',
'--change-url',
'http://<hostname>:8080/308',
'--project', 'private/bar',
'--branch', 'master',
'--author', 'xxxxxxx-xxxxx xxxxxxx (xxxxxxxxxxxxx.xxxxxxx#xxx-xxxx.xx)', <<< That's right!
'--commit', '49aae9befaf27a5fede51b498f0660199f47b899',
'--comment', 'asdf',
'--CRVW', '0',
'--VRIF', '0']
As the options names and values are space-separated, you have to put the values in quotes if they contain spaces themselves.
If you write --uploader xxxxxxx-xxxxx xxxxxxx (xxxxxxxxxxxxx.xxxxxxx#xxx-xxxx.xx), the last two strings will actually end up in args from the line
optlist, args = getopt.getopt(sys.argv[1:], '', need)
as they are not associated with --uploader
You should quote an argument, if it contains spaces, like for all commandline tools:
--uploader "xxxxxxx-xxxxx xxxxxxx (xxxxxxxxxxxxx.xxxxxxx#xxx-xxxx.xx)"
You may also consider using gnu_getopt() as it would allow you to mix option and non-option arguments.
From the Documentation
The getopt() function stops processing options as soon as a non-option argument is encountered
If you use gnu_getopt, the rest of the options namely commit and pathset will still be parsed correctly even though the uploader argument has missing quotes

Categories