Hi everyone.
I am new to programming and to this forum as well. I’ve started to learn Python just recently, and as a challenge, I wanted to create a very basic “game”.
At the beginning it was pretty simple, I just coded a few lines so that when I run the program, I could move on a 100-cases grid (to the left when I type “l”, upwards when I type “u”…). And after it worked, I started adding more and more functions, allowing me to gather some fruits when on a specific case, store those fruits in a basket on another case, cook some fruit desserts, etc.
It was working just fine, but as soon as I added a new function allowing me to “teleport” from one case to another (btw, the function works, I tested it seperately), the program no longer worked. When I try to run it with IDLE, it says the syntax of the “gather” function is invalid. The strange thing is, this same function worked perfectly before.
But I don’t know, I’m probably missing something since it doesn’t work. I would be glad if someone could help me to find the error
# Location defined as (x, y)
location = [1, 1]
apple = 0
banana = 0
strawberry = 0
mango = 0
cherry = 0
apple_stored = 0
banana_stored = 0
strawberry_stored = 0
mango_stored = 0
cherry_stored = 0
fruit_salad = 0
apple_pie = 0
banana_split = 0
mango_juice = 0
'''
Functions for directions
l for left
r for right
u for up
d for down
'''
def moveLeft():
global location
if (location[0] > 0):
location = [location[0] - 1, location[1]]
else:
print("You cannot move to the left.")
def moveRight():
global location
if (location[0] < 10):
location = [location[0] + 1, location[1]]
else:
print("You cannot move to the right.")
def moveUp():
global location
if (location[1] > 0):
location = [location[0], location[1] - 1]
else:
print("You cannot move upwards.")
def moveDown():
global location
if (location[1] < 10):
location = [location[0], location[1] + 1]
else:
print("You cannot move downwards.")
'''
Check the location
Tells if you can gather when (0, 6)
Tells if you can store when (10, 2)
Tells if you can cook when (5, 5)
'''
def checkCase():
global location
if (location == [0, 6]):
print("You can gather here to get some fruits. Type 'gather' and press 'Enter' to gather. Type any other direction to pass.")
elif (location == [10, 2]):
print("You're in front of your basket. You can store some items ? Type 'store' and press 'Enter' to store. Type any other direction to pass.")
print("You can also type 'see basket' to see the items in your basket.")
elif (location == [5, 5]):
print("Here you can type the name of the recipe you want to make. You can make a 'fruit salad', an 'apple pie', a 'banana split' or some 'mango juice'.")
print("You can also type 'see fridge' to see what you already made.")
# elif
# Passes when you can gather (type 'gather' before), only when at [0, 6]
def gather():
global location
global apple
global banana
global strawberry
global mango
global cherry
if (location == [0, 6]):
if ((apple + banana + strawberry + mango + cherry) < 10):
from random import randint
x = randint(1, 5)
if (x == 1):
print("You got 1 apple.")
apple += 1
elif (x == 2):
print("You got 1 banana.")
banana += 1
elif (x == 3):
print("You got 1 strawberry.")
strawberry += 1
elif (x == 4):
print("You got 1 mango.")
mango += 1
elif (x == 5):
print("You got 1 cherry.")
cherry += 1
else:
print("Your bag is full, you no longer have enough room for more fruits. You can store some of them in your basket.")
else:
print("You cannot gather fruits here...")
# Passes when you 'store', when at [10, 2]
def store():
global location
global apple
global apple_stored
global banana
global banana_stored
global strawberry
global strawberry_stored
global mango
global mango_stored
global cherry
global cherry_stored
if (location == [10, 2]):
storeWhat = input("Which item do you want to store ?")
if (storeWhat == "apple"):
print("You've stored 1 apple.")
apple -= 1
apple_stored += 1
elif (storeWhat == "banana"):
print("You've stored 1 banana.")
banana -= 1
banana_stored += 1
elif (storeWhat == "strawberry"):
print("You've stored 1 strawberry.")
strawberry -= 1
strawberry_stored += 1
elif (storeWhat == "mango"):
print("You've stored 1 mango.")
mango -= 1
mango_stored += 1
elif (storeWhat == "cherry"):
print("You've stored 1 cherry.")
cherry -= 1
cherry_stored += 1
else:
print("You cannot store your items here...")
# Function to skip several cases (teleport)
def teleport():
global location
print("Where do you want to go ?")
location_x = int(input("x = ?"))
location_y = int(input("y = ?"))
if (location_x >= 0 and location_x <= 10 and location_y >= 0 and location_y <= 10):
location[0] = location_x
location[1] = location_y
print("You now moved to :", location)
else:
print("This case is not defined.")
# Function to make available recipes
def make():
global location
global apple
global banana
global strawberry
global mango
global cherry
global fruit_salad
global apple_pie
global banana_split
global mango_juice
if (location == [5, 5]):
cookWhat = input()
if (cookWhat == "fruit salad"):
if (apple > 0 and banana > 0 and strawberry > 0 and mango > 0 and cherry > 0):
print("You made a fruit salad.")
fruit_salad += 1
apple -= 1
banana -= 1
strawberry -= 1
mango -= 1
cherry -= 1
else:
print("You don't have the required ingredients to make a fruit salad.")
elif (cookWhat == "apple pie"):
if (apple > 3):
print("You made an apple pie.")
apple_pie += 1
apple -= 4
else:
print("You don't have enough apples to make an apple pie.")
elif (cookWhat == "banana split"):
if (banana > 0):
print("You made a banana split.")
banana_split += 1
banana -= 1
else:
print("You don't have any banana to make a banana split.")
elif (cookWhat == "mango juice"):
if (mango > 2):
print("You made some mango juice.")
mango_juice += 1
mango -= 3
else:
print("You don't have enough mangoes to make mango juice.")
else:
print("You cannot cook here...")
print ("You can move in any direction, you just have to type the first letter of the direction you want to go to (for instance, 'l' for 'left'. To see the items in your bag, type 'see bag'.")
print("Your location :", location)
while (apple_pie < 2):
action = input()
if (action == "l"):
moveLeft()
print("You've now moved to :", location)
checkCase()
elif (action == "r"):
moveRight()
print("You've now moved to:", location)
checkCase()
elif (action == "u"):
moveUp()
print("You've now moved to :", location)
checkCase()
elif (action == "d"):
moveDown()
print("You've now moved to :", location)
elif (action == "gather"):
gather()
elif (action == "store"):
store()
elif (action == "see bag"):
print("You have {} apple(s), {} banana(s), {} strawberry(ies), {} mango(es) and {} cherry(ies) in your bag.".format(apple, banana, strawberry, mango, cherry))
elif (action == "see basket"):
print("You have {} apple(s), {} banana(s), {} strawberry(ies), {} mango(es) and {} cherry(ies) stored in your basket.".format(apple_stored, banana_stored, strawberry_stored, mango_stored, cherry_stored))
elif (action == "cook"):
make()
elif (action == "see fridge"):
print("You have {} fruit salad(s), {} apple pie(s), {} banana split(s) and {} bottle(s) of mango juice in your fridge.".format(fruit_salad, apple_pie, banana_split, mango_juice))
elif (action == "teleport"):
teleport()
checkCase()
print("You now have 2 apple pies. Well done!")