what am i doing wrong this is my code and my error is on the 2nd line also im using godot and i'm using gdscrpt inside godot and this is not all of my code its just the code that i got the error on
if is_on_wall():
if spritedir == "left" and test_move(transform, Vector2(-1,0)):
anim_switch("push")
if spritedir == "right" and test_move(transform, Vector2(1,0)):
anim_switch("push")
if spritedir == "up" and test_move(transform, Vector2(0,-1)):
anim_switch("push")
if spritedir == "down" and test_move(transform, Vector2(0,1)):
anim_switch("push")
as the error says you need to indent the code after the first if:
if is_on_wall():
if spritedir == "left" and test_move(transform, Vector2(-1,0)):
anim_switch("push")
if spritedir == "right" and test_move(transform, Vector2(1,0)):
anim_switch("push")
if spritedir == "up" and test_move(transform, Vector2(0,-1)):
anim_switch("push")
if spritedir == "down" and test_move(transform, Vector2(0,1)):
anim_switch("push")
GDScript has a similar syntax to Python. When you declare a conditional statement like "if", you need to indent the code below your condition.
if x == y:
# All the code with indent.
pass
When you indent the code, you are actually saying that it belongs to the scope of your conditional statement. Therefore, if you do not indent after an "if" statement it will bring an error, because in case of True/False, there is nothing to be executed.
Related
I want to move to the top of a set of nested loops as I am removing items from a list to run arithmetic on. So when I use break it ends all the loops, and I tried break/continue but to no luck. Is there a way to break out of the inner loops and have it start from the top loop?
def postfix_eval(chaine):
chaine, cumulator, basedict, zed = chaine.split(), 0, [], 0
for x in chaine:
if x.isdigit():
basedict.append(int(x))
chaine.remove(x)
for y in chaine:
if y.isdigit():
basedict.append(int(y))
chaine.remove(y)
print("chaine at y" , chaine)
for zed in chaine:
if zed == "-" or zed == "+" or zed == "*" or zed == "/":
chaine.remove(str(zed))
print("chaine at zed", chaine)
operators = {'+': int(x)+int(y) , '-': int(x)-int(y), '/':
int(x)+int(y), '*':int(x) * int(y)}
cumulator += operators[zed]
break
continue
continue
return cumulator
In python, there is a concept for/else loops. So imagine having a simple code like:
for x in a:
for y in b:
...
if (...):
break
else:
...
Your program will go into the else block only if the loop was not interrupted by the break. If not clear maybe look it up.
I am programming a betting application consisting of multiple games, each with it's own function. I want to check if the bets for each game is less than or equal to 0. If yes, it prints invalid bet and the game function stops and returns 0 winnings. I want the checker to be a function that I can call for each game instead of including the entire code for each one. Is there a statement, when returned by a function, it ends/returns the calling function?
I did an if statement for each game function but this seems repetitive so I was thinking if I could have it as a function instead.
def roulette(bet):
if bet <= 0:
print('Invalid Bet')
return 0
# other code
def card_draw(bet):
if bet <= 0:
print('Invalid Bet')
return 0
# other code
def bet_checker(bet):
if bet <= 0:
print('Invalid Bet')
# statement that returns 0 to the function that calls it
# ideal roulette function
def roulette(bet):
bet_checker(bet)
# other code
I'm not sure if I understand you correct. But you could let your bet_checker function return False in case the bet is not valid. The calling function roulette will only run "other code2" when bet_ckecker returned True.
def bet_checker(bet):
if bet <= 0:
print('Invalid Bet')
return False
else:
# other code1
return True
def roulette(bet):
if bet_checker(bet):
print('Valid bet')
# other code2
Calling
roulette(0)
returns 'Invalid Bet' and won't run any other code.
im trying to make a basic coin flipper and i want a means to repeating it once done, so i added the answer variable and if it == y then it will repeat my main function.
But in while(answer == "y"): it is saying that answer is not defined even though i have globalised the variable?
import random
def main():
myVar = random.randint(1,2)
if myVar == "2":
print("Heads")
else:
print("Tails")
global answer
answer = input("repeat?")
while(answer == "y"):
main()
main()
The while loop must be after you call the main() function. Even though you have defined the function before the while loop, it isn't run before you call it.
main()
while(answer == "y"):
main()
Also, the result will also always be "Tails", since you check if myVar == "2", which is a string. And myVar is never a string in this case. Remove the quotes to check integer value.
if myVar == 2:
if new.upper() == "C":
first()
try:
a, b, c = re.split(r"(\s+)", new)
except ValueError:
return
The syntaxs error occures to the colon behind "C". if new.upper() == "C":
What can i do to fix this problem?
Your indentation is incorrect. It should look like.
if new.upper() == "C":
first()
try:
a, b, c = re.split(r"(\s+)", new)
except ValueError:
pass
Additionally use pass instead of return unless that snippet of code is inside a function. Because return can only be used inside a function.
Edit:
Added in the correct indentation points
import pygame
number=1
screen = pygame.display.set_mode((1920,1080))#,pygame.FULLSCREEN)
app1=pygame.image.load("moonlight.jpg").convert()
app2=pygame.image.load("youtube.jpg").convert()
clock = pygame.time.Clock()
while True:
if number==1:
screen.blit("app"+str(number), (-100,0))
clock.tick(40)
pygame.display.update()``
How do I get "app"+str(number) to be recognised as the app1 variable.
I get the error "argument 1 must be pygame.Surface, not str"
To make your life easier, try doing something like this:
while True:
if number == 1:
screen.blit(app1, (-100,0))
elif number == 2:
screen.blit(app2, (x coordinates, y coordinates))
clock.tick(40)
pygame.display.update()
This way, you won't have to deal with conflicting variable types. Also using this method, your code will become more compact.