Couldn't send this answer yesterday because the site was down for some reason.
First thing you can fix is to add a guard before calling main()
. This is the usual way to do things in Python so that you can import you files without running the code corresponding to main
:
if __name__ == "__main__":
main()
Then, one can see that you are using recursion to emulate loops. It is not quite a good idea in Python because recursion is not that optimised (for various reasons) so you should stick to loops if it doesn't make things more complicated. In your case, it doesn't at all : you can remove calls to main
from your main
function which becomes :
def main():
global gold
global apples
while True:
print "Gold: %r Apples: %r" % (gold, apples)
print "Pick an apple?"
choice = raw_input(prompt)
if choice == "yes":
pick()
elif choice == "no":
global gold
global apples
print "Apples: %r Gold: %r" % (apples, gold)
print "Sell apples?"
sell = raw_input(prompt)
if sell == "yes" and apples >= 1:
global gold
global apples
apple_sales = randint(1,5)
gold = (gold * (apples / 2)) * apple_sales
apples = apples - apples
if gold >= 25 ** 25:
print "\t\t\tYou won!"
print "Congrats on controlling the apple market!"
return
elif sell == "yes" and apples <= 0:
print "\nNot enough apples!"
time.sleep(0.7)
elif choice == "exit":
print "Bye..."
return
From the pick
function, you probably don't need to call main
as the loop from the main
function should handle things properly.
def pick():
global gold
global apples
print "Type 0 to exit. How many?"
print "Apples: %r Gold %r" % (apples, gold)
try:
apple_num = int(raw_input(prompt))
if apple_num == 3 or apple_num == 2 or apple_num == 1:
global apples
apples = apples + apple_num
time.sleep(0.5)
pick()
elif apple_num >= 4:
print "You can't haul that many apples!"
time.sleep(0.5)
pick()
except ValueError:
pick()
We can apply the same kind of arguments to pick
calling itself :
def pick():
global gold
global apples
while True:
print "Type 0 to exit. How many?"
print "Apples: %r Gold %r" % (apples, gold)
try:
apple_num = int(raw_input(prompt))
if apple_num == 3 or apple_num == 2 or apple_num == 1:
global apples
apples = apples + apple_num
time.sleep(0.5)
elif apple_num >= 4:
print "You can't haul that many apples!"
time.sleep(0.5)
else:
return
except ValueError:
pass
Then, you do not need to repeat global my_var
before each and every use, you can do it once at the beginning of the function and that should be enough.
Also, if apple_num == 3 or apple_num == 2 or apple_num == 1
can be written in a concise way in Python : if apple_num in [3, 2, 1].
But as we are using integers, we can use a even better way :
if 1 <= apple_num <= 3`.
Then, you can write apples += apple_nums
instead of apples = apples + apple_num
.
You can simply write apples = 0
instead of apples = apples - apples
.
You should try to avoid writing the same condition twice, it makes things harder to understand/maintain. For instance, you could write :
sell = raw_input(prompt)
if sell == "yes":
if apples >= 1:
apple_sales = randint(1,5)
gold = (gold * (apples / 2)) * apple_sales
apples = 0
if gold >= 25 ** 25:
print "\t\t\tYou won!"
print "Congrats on controlling the apple market!"
return
elif apples <= 0:
print "\nNot enough apples!"
time.sleep(0.7)
and then, becomes apples
is known to be an integer at all time, the second condition will always be true if the first is false : we don't need it. Last details on this : the pythonic way to write if my_int != 0
is if my_int
. It applies to apples
because it will be a positive-or-null integer. Thus, you can write : if apples
Now, we can actually go into your global
issue : your pick
function could return the number of picked apple and shouldn't print about the total number of apples and gold.
def main():
apples = 1
gold = 1
while True:
print "Gold: %r Apples: %r" % (gold, apples)
print "Pick an apple?"
choice = raw_input(prompt)
if choice == "yes":
apples += pick()
elif choice == "no":
print "Apples: %r Gold: %r" % (apples, gold)
print "Sell apples?"
sell = raw_input(prompt)
if sell == "yes":
if apples:
apple_sales = randint(1,5)
gold = (gold * (apples / 2)) * apple_sales
apples = 0
if gold >= 25 ** 25:
print "\t\t\tYou won!"
print "Congrats on controlling the apple market!"
return
else:
print "\nNot enough apples!"
time.sleep(0.7)
elif choice == "exit":
print "Bye..."
return
def pick():
picked_apples = 0
while True:
print "Type 0 to exit. How many?"
print "Picked apples: %r" % (picked_apples)
try:
apple_num = int(raw_input(prompt))
if 1 <= apple_num <= 3:
picked_apples += apple_num
time.sleep(0.5)
elif apple_num >= 4:
print "You can't haul that many apples!"
time.sleep(0.5)
else:
return picked_apples
except ValueError:
pass
Finally, you could make your user interface better by providing the different options:
def main():
apples = 1
gold = 1
while True:
print "Gold: %r Apples: %r" % (gold, apples)
print "Action ? (pick/sell/exit)"
choice = raw_input(prompt)
if choice == "pick":
apples += pick()
elif choice == "sell":
if apples:
apple_sales = randint(1,5)
gold = (gold * (apples / 2)) * apple_sales
apples = 0
if gold >= 25 ** 25:
print "\t\t\tYou won!"
print "Congrats on controlling the apple market!"
return
else:
print "\nNot enough apples!"
time.sleep(0.7)
elif choice == "exit":
print "Bye..."
return