Reputation: 133
def variables() exists so that the program will reset when the user chooses to start a new game, or at least this is my intention. However, nothing is printed to the shell when I try to initially run the program. People seem to use this sort of def variables() operator(?) to reset programs, at least this is how I interpreted it upon researching it. What can be done to make the program run properly?
EDIT: If I'm completely missing the purpose of what def is supposed to do, please let me know. Every explanation I see online is unclear to me.
import random
def variables():
y = 0
x = 0
i = 1
a_random = random.randrange(-3,3,1)
b_random = random.randrange(-3,3,1)
while i > 0:
question = (input("You can go up, down, left, and right. Which do you choose? "));
if question in ["up"]:
y = y + 1
print("You've moved up one unit!")
print("Your position is now (", x, ",", y, ")")
if question in ["down"]:
y = y - 1
print("You've moved down one unit!")
print("Your position is now (", x, ",", y, ")")
if question in ["right"]:
x = x + 1
print("You've moved right one unit!")
print("Your position is now (", x, ",", y, ")")
if question in ["left"]:
x = x - 1
print("You've moved left one unit!")
print("Your position is now (", x, ",", y, ")")
if (x == a_random) and (y == b_random):
print("You've found the treasure!")
repeat = (input("Play again? "))
if repeat in ["yes"]:
variables()
if repeat in ["no"]:
i = -1
print("Game over.")
if question in ["cheats"]:
print("The treasure is located at (", a_random, ", ", b_random, ").")
Upvotes: 0
Views: 2077
Reputation: 1658
Is this the full program? You aren't actually executing the variables() function anywhere. You probably want:
if __name__ == '__main__':
variables()
That said, I'd revisit some texts on basic Python program structure before moving further.
Upvotes: 2