Python- Invalid Syntax error - python-3.x

I'm getting an invalid syntax error in the final variable. I don't see what the problem is, I think my indentation is correct, can tell me what I am doing wrong? I'm trying to make a simple python xor program.
msg='To use this decimal to binary converter tool, you should type a decimal value like 308 into the left field below, and then hit the Convert button. This way you can convert up to 19 decimal characters (max. value of 9223372036854775807) to binary value.'
key='ab'
encrypt=[]
decrypt=[]
count=0
for i in msg:
if count>=len(key):
count=0
encrypt.append(ord(i)^ord(key[count]))
count+=1
count=0
print(encrypt)
for i in encrypt:
if count>=len(key):
count=0
count+=1
decrypt.append(i^ord(key[count])
final=''.join(chr(e) for e in decrypt)
print(final)

Whenever you see dubious error messages in places that don't make sense, count your parentheses!
In you case, you missed a closing parentheses when calling the ord function:
decrypt.append(i ^ ord(key[count]))

Related

Encountering an Invalid literal error whilst String Slicing

Okay so I am trying to write a program that can read lines of RLE and convert them into lines of ASCII Art, I am very new to Python so could anyone help me in understanding what I have done wrong in the code to create this error.
Thanks
It's telling you that you're passing an invalid Integer Literal Value (0xa0) to your function int() in the block of code for b in range (0,int(num))
If you're going to pass a Hexadecimal value to the int function, you need to specify the numeric base (16) as the 2nd parameter of the int function int("0xa0",16)

Data Being Read as Strings instead of Floats

A Pytorch program, which I don't fully understand, produced an output and wrote it into weight.txt. I'm trying to do some further calculations based on this output.
I'd like the output to be interpreted as a list of length 3, each entry of which is a list of floats of length 240.
I use this to load in the data
w=open("weight.txt","r")
weight=[]
for number in w:
weight.append(number)
print(len(weight)) yields 3. So far so good.
But then print(len(weight[0])) yields 6141. That's bad!
On closer inspection, it's because weight[0] is being read character-by-character instead of number-by-number. So for example, print(weight[0][0]) yields - instead of -1.327657848596572876e-01. These numbers are separated by single spaces, which are also being read as characters.
How do I fix this?
Thank you
Edit: I tried making a repair function:
def repair(S):
numbers=[]
num=''
for i in range(len(S)):
if S[i]!=' ':
num+=S[i]
elif S[i]==' ':
num=float(num)
numbers.append(num)
num=''
elif i==len(S)-1:
num+=S[i]
num=float(num)
numbers.append(num)
return numbers
Unfortunately, print(repair('123 456')) returns [123.0] instead of the desired [123.0 456.0].
You haven't told us what your input file looks like, so it's hard to give an exact answer. But, assuming it looks like this:
123 312.8 12
2.5 12.7 32
the following program:
w=open("weight.txt","r")
weight=[]
for line in w:
for n in line.split():
weight.append(float(n))
print weight
will print:
[123.0, 312.8, 12.0, 2.5, 12.7, 32.0]
which is closer to what you're looking for, I presume?
The crux of the issue here is that for number in w in your program simply goes through each line: You have to have another loop to split that line into its constituents and then convert appropriately.

How do I get the values in my dictionary that I have created in python, to be used, instead of the strings?

Below is a shortened version of my code, without all the validation. I am writing a program that tells the user how strong their password is, by seeing their overall score at the end. If the password has 3 letters next to each other in a row, and those three letters are also next to each other on the 'qwerty' keyboard, then their overall score goes down by 5. I have created a dictionary to assign each letter on the keyboard a value, and then if 2 consecutive letters in the password have a difference of 1, it means there are 3 letters in a row on the keyboard.
However, I keep getting a
ValueError: invalid literal for int() with base 10:
I don't really know how to use dictionaries, so any help is much appreciated!
password=str(input("Please enter a password with more than 4 digits, and it should only be letters:"))
score=0
keyboard={'Q':1,'q':1,'W':2,'w':2,'E':3,'e':3,'R':4,'r':4,'T':5,'t':5,'Y':6,'y':6,'U':7,'u':7,'I':8,'i':8,'O':9,'o':9,'P':10,'p':10,'A':12,'a':12,'S':13,'s':13,'D':14,'d':14,'F':15,'f':15,'G':16,'g':16,'H':17,'h':17,'J':18,'j':18,'K':19,'k':19,'L':20,'l':20,'Z':22,'z':22,'X':23,'x':23,'C':24,'c':24,'V':25,'v':25,'B':26,'b':26,'N':27,'n':27,'M':28,'m':28}
for n in range ((len(password))-2):
if (int(password[n+1])-int(password[n])==1) and (int(password[n+2])-int(password[n+1]==1)):
score=score-5
print(score)
If your password input is only letters, then this following line will raise an error.
int(password[n+1])
and so will int(password[n]) and all your other int casts. The reason for this is because you're casting non-digit characters to int. That's what's causing the error you're seeing.
I believe, your intention is to do
int(keyboard[password[n+1]]) - int(keyboard[password[n]]) == 1
but since, the values of your keyboard dictionary are already int's, then the int casts in your if-statement are not required.
keyboard[password[n+1]] - keyboard[password[n]] == 1

Python Praw TypeError: not all arguments converted during string formatting

I wasn't getting this message when I was running the script earlier...and now I am. What am I missing?
comments.reply("__%s__","\n","Current Temp: %s\u00b0F" % (str(cityname),temp_f))
The problem is at the third string:
"Current Temp: %s\u00b0F" % (str(cityname),temp_f)
It contains only one format specifier %s but a 2-tuple as formatting arguments. Hence not all arguments can be converted during the formatting. On the other hand your first string __%s__ goes unformatted, so I have the feeling that you rather want do combine these three strings either (e.g. by leaving out the commas):
comments.reply("__%s__\nCurrent Temp: %s\u00b0F" % (cityname, temp_f))

Python: numerical elements in a list cant be converted into integers

Hello people,
I've got a problem with my code. For some reason the values are not converted to integers from strings and are not adding up. Here is my code.
def SumOfState(i,j):
cf=readPopest(file1)
sum2=[]
sum7=[]
Diff=0
for y in range((j)):
StateList=str(cf[y+i]).split(',')
sum2.append(StateList[2])
sum7.append(StateList[7])
results2 = [int(i) for i in sum2]
results7 = [int(i) for i in sum7]
print sum(results2)
print sum(results7)
Error message : Inappropriate argument value (of correct type).
An error occurred attempting to pass an argument to a function.
cf=readPopest(file1)
the code ^^ gives a list containing words and numbers. One element is taken % split into sublists.
Ive tried the int() function and the for loop variant of it.
suggest me an edit, please.
Really appreciate any help.
Thanks.
-Addie Vanhala
I guess looking at you code, it is because sum2 and sum7 contain non integers, probably because some part of file1 (accessed though readPopest) is non int.

Resources