IndentationError in the Following Python Script - python

My code is not running although everything is properly indented and I have been using Python for a while now, so I am no longer in the world of programming. I couldn't find the solution.
def revisedRussianRoulette(doors):
counter = 0
for i in range(0, len(doors), 2):
i = int(i)
if doors[i] == 1 & counter == 0:
counter += 1
elif doors[i] == 1 & counter == 1:
doors[i] = 0
doors[i-2] = 0
doors[i+2] = 0
elif doors[i] == 0 & counter == 1:
doors[i-2] = 0
return doors
n = int(input().strip())
doors = list(map(int, input().strip().split(' ')))
result = revisedRussianRoulette(doors)
print (" ".join(map(str, result)))
The thing I want to do with this code does not matter. I just want to ask if the syntax is correct because I am getting the following error.
C:\Users\lenovo\Desktop\Practice Files>2nd_answer_week_of_code_36.py
File "C:\Users\lenovo\Desktop\PracticeFiles\2nd_answer_week_of_code_36.py", line 13
return doors
^
IndentationError: unindent does not match any outer indentation level
Please, could anyone tell me the solution fast?
EDIT:
The solution provided by Vikas was accurate, although there were no differences between his and my code.

Do indenation like this :
def revisedRussianRoulette(doors):
counter = 0
for i in range(0, len(doors), 2):
i = int(i)
if doors[i] == 1 & counter == 0:
counter += 1
elif doors[i] == 1 & counter == 1:
doors[i] = 0
doors[i-2] = 0
doors[i+2] = 0
elif doors[i] == 0 & counter == 1:
doors[i-2] = 0
return doors

def revisedRussianRoulette(doors):
counter = 0
for i in range(0, len(doors), 2):
i = int(i)
condition_one = doors[i] == 1 & counter == 0
condition_two = doors[i] == 1 & counter == 1
condition_three = doors[i] == 0 & counter == 1
if condition_one:
counter += 1
elif condition_two:
doors[i] = 0
doors[i-2] = 0
doors[i+2] = 0
elif condition_three:
doors[i-2] = 0
return doors
n = int(input().strip())
doors = list(map(int, input().strip().split()))
result = revisedRussianRoulette(doors)
print (" ".join(map(str, result)))

Related

convert tradingview script into python3

I am looking for a converting from TV to python. Just a little code. This is the code in tradingview :
last_signal = 0
long_final = longCond and (nz(last_signal[1]) == 0 or nz(last_signal[1]) == -1)
short_final = shortCond and (nz(last_signal[1]) == 0 or nz(last_signal[1]) == 1)
last_signal := long_final ? 1 : short_final ? -1 : last_signal[1]
for variable :
-> longCond and shortCond, i have the right value (I compared between plot)
But for others, i have some differences (i think, because of last_signal)
this is my code in python :
for x in range(0,len(mavi),1):
last_signal[i] = 0
if x == 0:
longCond_tmp = 0
shortCond_tmp = 0
last_signal_tmp = 0
short_final_tmp = 0
long_final_tmp = 0
else:
if ((longCond_tmp and ((last_signal[i-1])) == 0) or ((last_signal[i-1]) == -1)):
long_final_tmp = 1
else:
long_final_tmp = 0
if ((shortCond_tmp and ((last_signal[i-1])) == 0) or ((last_signal[i-1]) == 1)):
short_final_tmp = 1
else:
short_final_tmp = 0
if long_final_tmp != 0:
last_signal_tmp = 1
else:
if short_final_tmp != 0:
last_signal_tmp = -1
else:
last_signal_tmp = last_signal[i-1]
last_signal[i] += last_signal_tmp
Are there errors in my script in python ?
Ok i found.
Just the number of "(" in
if ((longCond_tmp and ((last_signal[i-1])) == 0) or ((last_signal[i-1]) == -1)):
long_final_tmp = 1
else:
long_final_tmp = 0
if ((shortCond_tmp and ((last_signal[i-1])) == 0) or ((last_signal[i-1]) == 1)):
short_final_tmp = 1
else:
short_final_tmp = 0

Find the total number of occurrence of a string in a cyclic string

I'm currently learning Python and I'm stuck on this specific question.
Image
Here is my current code:
word = input()
text = 0
wordch = 0
positions = 0
repeated = 0
while repeated != 2:
for i in range(0, len(tablet)):
if tablet[i] == word[wordch]:
text += 1
wordch += 1
if text == len(word):
positions += 1
text = 0
wordch = 0
elif repeated == 1 and text == len(word):
positions += 1
text = 0
wordch = 0
break
elif i == len(tablet)-1:
repeated += 1
break
elif tablet[i] != word[wordch]:
text == 0
wordch == 0
print(positions)
I would hope for a code that is really basic using the same concepts but please do answer.
Thank you!
I have tried to solve the problem by using a different approach. As we know that we can only use (len(fav_word)) - 1 letters if we tried to create the substring in a cyclic manner from the end since if we took any more characters, we would have created them from the start itself without the cycle.
So, I just created a new string from the original string by appending the starting (len(fav_word)) - 1 to the original string and then find all occurrences of the fav_string in the new string.
def find_all(a_str, sub):
start = 0
while True:
start = a_str.find(sub, start)
if start == -1: return
yield start
start += 1
x = "cabccabcab"
fav = "abc"
y = x + x[0:len(fav)-1]
print(len(list(find_all(y, fav)))) # Output: 3
x = "ababa"
fav = "aba"
y = x + x[0:len(fav)-1]
print(len(list(find_all(y, fav)))) # Output: 2
x = "aaaaaa"
fav = "aa"
y = x + x[0:len(fav)-1]
print(len(list(find_all(y, fav)))) # Output: 6
x = "abaaba"
fav = "aaba"
y = x + x[0:len(fav)-1]
print(len(list(find_all(y, fav)))) # Output: 2
def find_str(g,find):
lg = len(g)
lf = len(find)
x=0
s=""
for index, i in enumerate(g):
if i == find[0]:
if index+lf <= lg:
s = "".join(g[index:index+lf])
if s == find:
x+=1
else:
rem = "".join(g[index:])
lr = len(rem)
for index,i in enumerate(g):
rem+=i
lr+=1
if lr == lf:
if rem == find:
x+=1
break
return x
print(find_str("abaaba","aaba"))
def split(word):
return [char for char in word]
x = "aaaaaa"
pattern = "aa"
mylist=split(x)
ok=True
occurrences=0
buffer=""
while ok:
char=mylist.pop(0)
buffer+=char
if buffer==pattern:
occurrences+=1
buffer=""
if len(mylist)==0:
ok=False
print(occurrences)
output:3

function unable to work with user inputs?

I have the following code, which is a code for a connect 4 game, the problem is that the functions seem to break in the user input part of the code. I don't know if I accidentally edited something to break it but I'm almost certain that it isn't as typing the adding this to the code:
addcounter(1,1)
addcounter(1,1)
addcounter(1,1)
addcounter(1,1)
addcounter(2,1)
addcounter(2,1)
addcounter(2,1)
addcounter(3,1)
addcounter(3,1)
addcounter(4,1)
checkdiagonal(4,1,1)
The output of this does return true as expected.
The code is as follows:
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 22 17:24:35 2019
#author: Norbert
"""
import numpy as np
h = 8
w = 9
x = 0
y = 0
playspace = np.zeros((h,w))
playspace[:, [0,-1]] = 8
def addcounter(column,team):
placed = False
while not placed:
for i in range(h-1,0,-1):
if playspace[i,column] > 0:
continue
else:
if team == 1:
playspace[i,column] = 1
if team == 2:
playspace[i,column] = 2
if team != 1 and team != 2:
print("error, invalid team")
placed = True
break
def height(column):
for i in range(h-1,0,-1):
if playspace[i,column] > 0:
continue
else:
return i
def checkhorizontal(y,x,team):
a1 = 0
a2 = 0
a3 = 0
a4 = 0
for i in range(4):
try:
if playspace[h-y,x+i] == team:
a1 += 1
if playspace[h-y,x+i-1] == team:
a2 += 1
if playspace[h-y,x+i-2] ==team:
a3 += 1
if playspace[h-y,x+i-3] ==team:
a4 += 1
except:
pass
if (a1 == 4) or (a2 == 4) or (a3 == 4) or (a4 == 4):
return True
def checkvertical(y,x,team):
a1 = 0
a2 = 0
a3 = 0
a4 = 0
checking = playspace[:,x]
for i in range(4):
try:
if checking[h-(y+i)] == team:
a1 += 1
if checking[h-(y+i-1)] == team:
a2 += 1
if checking[h-(y+i-2)] ==team:
a3 += 1
if checking[h-(y+i-3)] == team:
a4 += 1
except:
pass
if (a1 == 4) or (a2 == 4) or (a3 == 4) or (a4 == 4):
return True
def checkdiagonal(y,x,team):
diag1 = 0
diag2 = 0
if playspace[h-y,x] == team:
diag1 += 1
diag2 += 1
for i in range(1,4):
broken1 = False
try:
if playspace[h-y+i,x+i] == team and not broken1:
diag1 += 1
if playspace[h-y+i,x+i] != team:
broken1 = True
except:
pass
for i in range(1,4):
broken2 = False
try:
if playspace[h-y-i,x-i] == team and not broken2:
diag1 += 1
if playspace[h-y-i,x-i] != team:
broken2 = True
except:
pass
for i in range(1,4):
broken3 = False
try:
if playspace[h-y+i,x-i] == team and not broken3:
diag2 += 1
if playspace[h-y+i,x-i] != team:
broken3 = True
except:
pass
for i in range(1,4):
broken4 = False
try:
if playspace[h-y-i,x+i] == team and not broken4:
diag2 += 1
if playspace[h-y-i,x+i] != team:
broken4 = False
except:
pass
if (diag1 >= 4) or (diag2 >= 4):
return True
finished = False
turn = 0
team_turn = 1
print(playspace)
while not finished:
valid = False
print("turn: {}".format(turn))
print("It is player {}'s turn".format(team_turn))
while not valid:
player_input = int(input("Where would you like to drop a counter?"))
if playspace[0,player_input] != 0:
print("That isn't a valid column")
else:
valid = True
if team_turn == 1:
addcounter(player_input,team_turn)
if checkhorizontal(height(player_input),player_input,team_turn) == True or checkvertical(height(player_input),player_input,team_turn) == True or checkdiagonal(height(player_input),player_input,team_turn) == True:
print("Player {} wins".format(team_turn))
finished = True
if team_turn == 2:
addcounter(player_input,team_turn)
if checkhorizontal(height(player_input),player_input,team_turn) == True or checkvertical(height(player_input),player_input,team_turn) == True or checkdiagonal(height(player_input),player_input,team_turn) == True:
print("Player {} wins".format(team_turn))
finished = True
turn += 1
turn_changed = False
while not turn_changed:
if team_turn == 1:
team_turn = 2
turn_changed = True
break
if team_turn == 2:
team_turn = 1
turn_changed = True
break
print(playspace)
any help in trying to solve the bugs in the code would be grately appreciated. The connect 4 game uses simple 2-d arrays to display and store the game board. I have plans to use this array to create a pygame later on.
EDIT:
To clarify, the checks don't run to execute a victory message and end the game. despite the if statements being True. Only the vertical win seems to end the game.
I managed to correct the horizontal part, the code correction is as follows:
if (checkhorizontal(height(player_input)+1,player_input,team_turn) == True) or (checkvertical(height(player_input),player_input,team_turn) == True) or (checkdiagonal(height(player_input),player_input,team_turn) == True):
and in the horizontalcheck function:
def checkhorizontal(y,x,team):
a1 = 0
a2 = 0
a3 = 0
a4 = 0
checking = playspace[y,:]
print(checking)
for i in range(4):
try:
if checking[x+i] == team:
a1 += 1
if checking[x+i-1] == team:
a2 += 1
if checking[x+i-2] ==team:
a3 += 1
if checking[x+i-3] ==team:
a4 += 1
except:
pass
print(a1,a2,a3,a4)
if (a1 == 4) or (a2 == 4) or (a3 == 4) or (a4 == 4):
return True
Ignore the prints, they're for debugging. But I basically removed one dimension and flipped the h-y value to become the y value.

I need to make a EAN-13 number validity checker in Python2. I don't see why this doesn't work

inp = raw_input("Input a EAN-13 number.")
aaa = False
bbb = False
if len(inp) == 13:
bbb = True
else:
print "Error input"
exit
ean_number = int(inp)
def ean13(value_1):
mult_of_ten = 0
sum_of_digits = 0
done = False
for z in len(value_1):
if not z == 0:
if z % 2 == 0:
value_1[z] *= 3
elif not z % 2 == 0:
value_1[z] *= 1
for a in len(value_1):
sum_of_digits += value_1[a]
if sum_of_digits % 10 == 0:
result = 0
elif not sum_of_digits % 10 == 0:
while done == False:
mult_of_ten = sum_of_digits
for d in True:
mult_of_ten += d
if sum_of_digits % 10 == 0:
done == True
result = mult_of_ten - sum_of_digits
if result == value_1[12]:
print "True"
if bbb == True:
ean13(ean_number)
I really don't see why teacher can't help either.
I need to make a EAN-13 number validity checker in Python2. I don't see why this doesn't work. Can anyone help?
I have made an EAN 13 Number Validity Checker in Python2.
Hope it helps you out.
# ean = '9780201379624'
ean = raw_input("Input a EAN-13 number:\n")
err = 0
even = 0
odd = 0
check_bit = ean[len(ean)-1]#get check bit(last bit)
check_val = ean[:-1]#Get all vals except check bit
if len(ean) != 13:#Check the input length
print "Invalid EAN 13"
else:
for index,num in enumerate(check_val):#Gather Odd and Even Bits
if index%2 == 0:
even += int(num)
else:
odd += int(num)
if ((3*odd)+even+int(check_bit)) % 10 == 0:# Check if the algorithm 3 * odd parity + even parity + check bit matches
print "Valid EAN 13"
else:
print "Invalid EAN 13"

Python says: "IndexError: string index out of range."

I am making some practice code for a game similar to the board game, MasterMind-- and It keeps coming out with this error, and I can't figure out why it's doin it. Here's the code:
def Guess_Almost (Guess, Answer):
a = ''.join([str(v) for v in Answer])
g = str(Guess)
n = 0
am = 0
while n < 5:
if g[n] == a[0]:
am = am + 1
if g[n] == a[2]:
am = am + 1
if g[n] == a[3]:
am = am + 1
if g[n] == a[3]:
am = am + 1
n = n + 1
return(am)
Okay, the Guess is specified to be 4 integers, and the Answer is a list containing 4 numbers. They both have the same 'len' after the code, so i don't have a clue.
The point of this code is to turn the Answer into a string of 4 numbers, and see if any of those numbers match thoise of the guess, and return how many total matches there are.
See if this helps
def Guess_Almost (Guess, Answer):
a = ''.join([str(v) for v in Answer])
g = str(Guess)
n = 0
am = 0
if len(g) >= 5 and len(a) >=4:
while n < 5:
if g[n] == a[0]:
am = am + 1
if g[n] == a[2]:
am = am + 1
if g[n] == a[3]:
am = am + 1
if g[n] == a[3]:
am = am + 1
n = n + 1
return(am)

Categories