I am trying to make a little program in python, nothing complicated, but extremely long. Just now, I modified one condition for an 'if' statement, and then ran it through the MacOS terminal, since it has a Python shell / IDLE (I don't know how is it called in english).
My document has exactly 456 lines, and the 'IndentationError' I get is at 'line 457' and here is the output :
File "/Volumes/my usb key/MAIN_0.3.py", line 457
^
IndentationError: expected an indented block
I really dont know what to do with that, since the last lines of my programs are all comments. If you want some code, I'll try to put it simply (and in english) for you all, but I would like to know first if it isn't a problem with running it in 'Terminal', because it is really weird.
EDIT : here is some code :
#coding= utf-8
#here I put a lot of variables(38), which are all 'int' or 'randint(x, y)'
#then I define some functions, which are all calculus
#here is the main loop :
while (a!=10) and (b > 10):
function_a(a)
c=0
while (c != 1) and (b > 0):
while b != 0:
print "\n"
d=randint(0,20)
if 0 <= d <= 3: #commentary explaining my program for my buddies at school
b=function_b(b, e, f, g, h, i, j, a, k, l)
if b <= 0: #commentary
m += n
p += a_b
if p > 1000: #commentary
p -= 1000
k += 1
print " BRAVO !"
print " now", k
time.sleep(1)
b += b_levelup
e += e_levelup
f += f_levelup
print " you won", b_levelup, "points"
time.sleep(1)
print " you won", e_levelup, "points"
time.sleep(1)
print " you won", f_levelup, "points"
time.sleep(1)
print "\n"*2
if 3 < d <= 8: #commentary
b=function_b(b, e, f, g, q, r, s, a, k, t)
if b <= 0: #commentary
m += u
p += v
if p > 1000: #
p -= 1000
k += 1
print " BRAVO !"
print " now", k
time.sleep(1)
b += b_levelup
e += e_levelup
f += f_levelup
print " you won", b_levelup, "points"
time.sleep(1)
print " you won", e_levelup, "points"
time.sleep(1)
print " you won", f_levelup, "points"
time.sleep(1)
print "\n"*2
if 8 < d <= 14: #commentary
b=function_b(b, e, f, g, w, x, y, a, z, k, a_a)
if b <= 0: #commentary
m += n
p += a_b
if p > 1000: #commentary
p -= 1000
k += 1
print " BRAVO !"
print " now", k
time.sleep(1)
b += b_levelup
e += e_levelup
f += f_levelup
print " you won", b_levelup, "points"
time.sleep(1)
print " you won", e_levelup, "points"
time.sleep(1)
print " you won", f_levelup, "points"
time.sleep(1)
print "\n"*2
if 15 <= d <= 16:#commentary
a_c += 1
function_c()
if d == 17: #commentary
a_d += 1
function_d()
#boss commentary
if (d == 18) and (a_d == 0):
print " mkay"
time.sleep(2)
print " nope"
print " next door"
#commentary
if (d == 18) and (a_d >= 1):
print " arrived"
time.sleep(2)
print " wanna go ?"
print " yes - 1"
print " No - any other"
a_e=input(" ")
if a_e == 1:
#boss commentary
Comments aren't statements, so when you do
if something:
# Comment
The if has no body. That's a syntax error. If you want a do-nothing placeholder body, that's what the pass statement is for.
if something:
# Comment
pass
Related
I am searching for common terms in 2 decks of cards.
I want to search deck[v]...1 to deck[b]...1,2,3,4,5,6,7,8,9,10,11,12,13.
I then want to increment [v] to search deck[v]...2 to deck[b]...1,2,3,4,5,6,7,8,9,10,11,12,13.
etc etc etc
I can get the first part but can't figure out how to increment [v] and reset [b] to 1.
Any help would be appreciated.
Theres 13 terms
b = 1
v = 1
while b <= 13:
print(b, deck[b])
print("Intersection between", "v = ",v, "and", "b = ",b, deck[v].intersection(deck[b]))
if len(deck[v].intersection(deck[b])) == 1:
print("1 match")
print("v = ", v)
print("b = ", b)
b = b + 1
Why not just use 2 for loops?
for b in range(1,14):
for v in range(1,14):
# Your code
print(b, deck[b])
print("Intersection between", "v = ",v, "and", "b = ",b, deck[v].intersection(deck[b]))
if len(deck[v].intersection(deck[b])) == 1:
print("1 match")
print("v = ", v)
print("b = ", b)
You just need to insert your current code into a new loop and increment v in that loop:
b = 1
v = 1
while v <= 13:
while b <= 13:
print(b, deck[b])
print("Intersection between", "v = ",v, "and", "b = ",b, deck[v].intersection(deck[b]))
if len(deck[v].intersection(deck[b])) == 1:
print("1 match")
print("v = ", v)
print("b = ", b)
b = b + 1
v = v + 1
SOLVED
I wrote a python program and im trying to search for an n in it,
but the last 3 lines in which i'm trying trial and error method aren't giving
any output.
Code:
#3
i = 0
def rekur(x, n):
global i
i += 1
#print('iteracja: ' + str(i) + '\nwartosc x = ' + str(x) + '\nwartosc y = ' + str(round(n,0))+ '\n')
if i > 50:
return 0
if n == 1:
return x
else:
if n % 3 == 0:
k = rekur(x, round(n/3, 0))
return round(pow(k, 3), 0)
else:
return round(x*rekur(x, n-1), 0)
print(rekur(3,4))
for g in range(1000):
if round(rekur(3, g),0) == 81:
print(g)
Output:
81
Process finished with exit code 0
These lines aren't giving any output:
for g in range(1000):
if round(rekur(3, g),0) == 81:
print(g)
why is that?
I found out why.
It was happening because of the state of i.
now when i added
if i > 50:
i = 0
return 0
it is giving me wanted answer
I'm trying to solve a problem where I need to create a function but I need the output to be in stdout format. This is the code i have been working on:
def oddNumbers(l, r):
s = ""
if l % 2 == 0:
if r % 2 == 0:
for i in range(l + 1, r, 2):
s += str(i) + " "
else:
for i in range(l + 1, r + 1, 2):
s += str(i) + " "
else:
if r % 2 == 0:
for i in range(l, r, 2):
s += str(i) + " "
else:
for i in range(l, r + 1, 2):
s += str(i) + " "
return s
This code works for single digit outputs but doesn't work for double digit or triple digit answers. How do I get the proper output in stdout format? Any help would be appreciated. thanks!
for example, for oddNumbers(2, 6), I get the ouput:
3
5
which is correct
but for double-digit outputs such as oddNumbers(96, 97), I get the output:
9
7
which is not correct.
i am supposed to get
97
i don't know how to fix this.
Not sure what you mean. The code works for all answers.
def oddNumbers(l, r):
s = ""
if not l % 2:
if not r % 2:
for i in range(l+1, r, 2):
s += str(i) + " "
else:
for i in range(l+1, r+1, 2):
s += str(i) + " "
else:
if not r % 2:
for i in range(l, r, 2):
s += str(i) + " "
else:
for i in range(l, r+1, 2):
s += str(i) + " "
return s
print(oddNumbers(0,10**4))
outputs every other number from 1 to 9999
I just want to figure out how often each element of F occurs within N and print it out. I have used the nested for loop, it works. But when I used nested while loop, it did not work as expected. I checked my code but cannot find out why.
F = [4,7,2]
N = [2,3,4,2,5,6,3,2,6,7,3,4]
Nested for loop version, works as expected:
four_count = 0
seven_count = 0
two_count = 0
for n in N:
for f in F:
if n == f and f == 4:
four_count += 1
elif n == f and f == 7:
seven_count += 1
elif n == f and f == 2:
two_count += 1
print(str(F[0]) + " occurs in N " + str(four_count) + " times")
print(str(F[1]) + " occurs in N " + str(seven_count) + " times")
print(str(F[2]) + " occurs in N " + str(two_count) + " times")
This is correct output:
4 occurs in N 2 times
7 occurs in N 1 times
2 occurs in N 3 times
Nested while loop version, wrong output:
four_count = 0
seven_count = 0
two_count = 0
N_Count = 0
F_Count = 0
while N_Count < len(N):
while F_Count < len(F):
if N[N_Count] == F[F_Count] and F[F_Count] == 4:
four_count += 1
elif N[N_Count] == F[F_Count] and F[F_Count] == 7:
seven_count += 1
elif N[N_Count] == F[F_Count] and F[F_Count] == 2:
two_count += 1
F_Count += 1
N_Count += 1
print(str(F[0]) + " occurs in N " + str(four_count) + " times")
print(str(F[1]) + " occurs in N " + str(seven_count) + " times")
print(str(F[2]) + " occurs in N " + str(two_count) + " times")
Wrong output from nested while loop:
4 occurs in N 0 times
7 occurs in N 0 times
2 occurs in N 1 times
You have to reset F_Count = 0 after while N_Count < len(N):, otherwise list F is only looped once. So it would be:
...
while N_Count < len(N):
F_Count = 0
while F_Count < len(F):
...
But unless you're learning about loops, this would not be the best way to do what you want. Something using count would be better, like:
counts = [N.count(f) for f in F]
or similar
I'm trying to build a system to randomly generate ship positions for a basic battleship game. To represent the grid I'm using nested list. However once the function has finished the matrix appears to revert back to what it was before I ran the function.
As you can see by running this code, the whole matrix works as intended when printed within the function but afterwards not. It's my first program I've tried building on my own and I've never gotten stuck for this long before. Thanks
import random
row, column = 10, 10;
Matrix = [[0 for x in range(row)] for y in range(column)]
ships = {'battleship':5, 'cruiser':4,'destroyer':3}
any_left = 1
def position_ship(type):
row, column = 10, 10;
Matrix = [[0 for x in range(row)] for y in range(column)]
ship_size_to_assign = type
start_pointV = 5 ### random.randint(0,10)
start_pointH = 5 ### random.randint(0,10)
start_point_direction = 1 ###random.randint(0,4)
print start_pointV
print start_pointH
print start_point_direction
start_point_direction = 1
if start_point_direction == 1:
n = 0
if (start_pointV + type) <= 10:
while ship_size_to_assign != 0:
Matrix[(start_pointH-1)][(start_pointV+n-1)] = 1
print "----------"
print Matrix[(start_pointH-1)][(start_pointV+n-1)]
print "----"
n = n + 1
ship_size_to_assign = ship_size_to_assign - 1
if start_point_direction == 2:
print "/////"
n = 0
if (start_pointH + type) <= 10:
while ship_size_to_assign != 0:
Matrix[start_pointH+n-1][start_pointV-1] = 1
n = n + 1
ship_size_to_assign = ship_size_to_assign - 1
if start_point_direction == 3:
print "/////"
n = 0
if (start_pointV - type) > 0:
while ship_size_to_assign != 0:
Matrix[start_pointH-1][start_pointV-n-1] = 1
n = n + 1
ship_size_to_assign = ship_size_to_assign - 1
if start_point_direction == 4:
print "/////"
n = 0
if (start_pointH - type) > 0:
while ship_size_to_assign != 0:
Matrix[start_pointH-1][start_pointV+n-1] = 1
n = n + 1
ship_size_to_assign = ship_size_to_assign - 1
print "####"
print Matrix[0]
print Matrix[1]
print Matrix[2]
print Matrix[3]
print Matrix[4]
print Matrix[5]
print Matrix[6]
print Matrix[7]
print Matrix[8]
print Matrix[9]
position_ship(5)
print "/////"
print Matrix[0]
print Matrix[1]
print Matrix[2]
print Matrix[3]
print Matrix[4]
print Matrix[5]
print Matrix[6]
print Matrix[7]
print Matrix[8]
print Matrix[9]