Comparing each char in two strings and incrementing: Python - python

for x in predRslt:
for y in actRslt:
if x == y and x =='1':
trueP += 1
elif x == y and x =='0':
trueN += 1
elif x != y and x == '1':
falseN += 1
elif x != y and x == '0':
falseP += 1
charNum += 1
totalActN = trueN + falseP
totalActP = falseN + trueP
totalPredN = trueN + falseN
totalPredP = trueP + falseP
print falseP
cmp_rslt('0110101001','1100100101')
actual output: 25
expected output: 2
Im trying to go through each string sequentailly from the beginning and compare the results. Then increment the corresponding TrueN, TrueP, FalseN, or FalseP.
for some reason, I keep getting an output of 25 when I should be getting way less than that because there's only 10 comparisons I should be making

you should iterate using the index of the first string like this:
for idx, x in enumerate(predRslt);
y = actRslt[idx]
if x == y and x =='1':
trueP += 1
elif x == y and x =='0':
trueN += 1
elif x != y and x == '1':
falseN += 1
elif x != y and x == '0':
falseP += 1

Related

Why is (x,y) NoneType?

I'm having problem with my code (Jupyter). Ordered pattern vacuum cleaner with obstacles. I am loading an array from a file (see picture if needed)
Then I have a function:
x_lim = 9
y_lim = 9
def ordered_move(x,y,gulv):
if gulv[x,y] == 0:
if x < 9:
x +=1
return (x,y)
if x == x_lim:
y +=1
return (x,y)
if gulv[x, y] > 0:
x == 0
y += 1
return (x,y)
if x == 9 and y == 9:
x == 6
y == 3
x += 1
return (x,y)
if x == 9 and y == 3:
x == 7
y == 4
x += 1
return (x,y)
if x == 9 and y == 4:
x == 6
y == 5
x += 1
return (x,y)
if x == 9 and y == 5:
x == 7
y == 6
x += 1
return (x,y)
if x == 9 and y == 6:
x == 6
y == 7
x += 1
return (x,y)
and then I use the code in:
x = 0
y = 0
time = 0
dust_in_room = 100
dust_removed = 0
time_passed_vec = []
dust_in_room_vec = range(100)
while dust_in_room > 7:
if array_from_file[x,y] == -1:
dust_in_room -= 1
array_from_file[x,y] = -2
time_passed_vec = time_passed_vec + [time]
dust_removed +=1
print(time)
time += 1
(x,y)=ordered_move(x,y,array_from_file)
I get the Type Error
"cannot unpack non-iterable NoneType object"
in the last line.
As you can tell from the code I am new to python, so maybe you are able to help with a simple explanation. Thank you.
The explanation is that none of the if conditions is verified.
If a function ends without a return statement the result is like if there was a return None.

Return to the top after results

I'm currently experimenting on different ways to solve exponents(via addition). As soon as the results are printed, I could not use the program anymore.
I've tried continue and break, but they're not appropriate
x = int(input())
y = 0
z = x
while z != 0:
y += x
z -= 1
if z <= 0:
y *= x
print(y)
I just need to re-use the application again after the previous results, and keep using it until it is closed.
You will need to wrap everything in a while loop and give some type of exit condition.
x = input()
while x != 'exit':
x = int(x)
y = 0
z = x
while z != 0:
y += x
z -= 1
if z <= 0:
y *= x
print(y)
x = int(input())
print('You chose to end the program, goodbye!')

Trying to run as function to recognize certain strings in a list and reiterate them

iput = []
final = []
while True:
iput += [input('Enter words here: ')]
if not iput[-1]:
break
z = 1
while i < len(iput) - 1:
print(i)
while iput[i][0] != iput[i][z]:
if z == len(iput[i]):
break
z += 1
i += 1
print(final)

Use x if z equals true else use z

I need forLoop1 to use y if z equals 1 else use x and same for forLoop2 but reversed
my code:
for z in range(3):
count=[0]*plys
for y in range(len(game)): #forLoop1
for x in range(len(game[y])): #forLoop2
for i in range(plys):
if game[y][x] == i+1:
count[i] += 1
for i in range(plys):
if count[i] >= 3:
print("Player " + str(i+1) + " is the winner")
count=[0]*plys
I tried something like this:
for y if z == 0 else x in range(len(game)):
and:
for (y if z == 0 else x) in range(len(game)):
But that didn't work
Any help would be greatly appreciated
and sorry if I'm bad at explaining it
The ... if ... else ... conditional expression produces an expression, you can't use it to man-handle the for loop index variable names like that.
But you can do this:
for z in range(3):
count=[0]*plys
for k1 in range(len(game)): #forLoop1
for k2 in range(len(game)): #forLoop2
y, x = (k1, k2) if z == 1 else (k2, k1)
for i in range(plys):
if game[y][x] == i+1:
count[i] += 1
for i in range(plys):
if count[i] >= 3:
print("Player " + str(i+1) + " is the winner")
count=[0]*plys
However, it might be clearer if you just use a full if... else block:
if z == 1:
y, x = k1, k2
else:
y, x = k2, k1

Python: syntax assignment error in iteration

I am doing a simple script for self learning in python where the scripts in turn finds the 1000th prime number but I get a syntax error.
x = 0
y = 2
counter = x
integer = y
while (counter>999):
if (y%2 == 0 or y%3 == 0):
y = y + 1
else:(counter = counter + 1 and integer = integer + 1)
print (y)
when it comes to the ='s assignment right after the ELSE operator and I don't understand why it won't let me add one to both the counter and integer when this has worked in other iteration scenario
In python you can't make an assignment inside an expresion, to avoid misspellings between = and ==. So you must do that in two lines:
x = 0
y = 2
counter = x
integer = y
while (counter>999):
if (y%2 == 0 or y%3 == 0):
y = y + 1
else:
counter += 1
integer += 1
print (y)
try this
else:
counter = counter + 1
integer = integer + 1
In python, assignment to variable has no Boolean value. and mean Boolean operator not do this and this.
so you need to split the statements.
x = 0
y = 2
counter = x
integer = y
while (counter>999):
if (y%2 == 0 or y%3 == 0):
y = y + 1
else:
counter += 1
integer += 1
print (y)

Categories

Resources