0
\$\begingroup\$

I am creating an arcade type thing in Python. Could someone suggest ways to simplify this code, or any other tips? All help is much appreciated.

I think the higherorlower function is particularly over-complicated.

import time
import random
tokens = 10000

def main():
    menu = int(input("What would you like to play?(Enter the corresponding number for each one)\n1. Slot Machine - 20 coins per turn\n2. Higher or Lower - 10 coins per turn\n"))
    if menu == 1:
        slotmachine(tokens)
    if menu == 2:
        higherorlower(tokens)

def tokencheck(cost):
    while True:
        if tokens >= cost:
            break
        else:
            print ("You do not have enough tokens")

def game_end():
    time.sleep(2)
    retry = input("Enter the corresponding letter for each one.\nM. Menu\nQ. Quit the arcade\n")
    if retry == "m" or retry == "M":
        main()
    if retry == "q" or retry == "Q":
        print("BYE BYE!")

def slotmachine(tokens):
    tokencheck(20)
    print ("You are playing slot machine!")
    print ("To win 1000 tokens you need to get 3 of the same number on each shake!")
    while True:
        tokencheck(20)
        shake = input("Press Enter to Shake!")
        tokens -= 10
        a,b,c = [random.randint(0,9),random.randint(0,9),random.randint(0,9)]
        print(a, b, c)
        if a != b or a != c:
            pass
        else: 
            print ("You win 1000 tokens!")
            tokens = tokens + 1000
            print ("You now have", tokens, "tokens!")
            break
    game_end()            

def higherorlower(tokens):
    goes = 0
    tokencheck(10)
    print ("You are playing higher or lower!")
    time.sleep(2)
    print ("You have to predict whether the next number (from 1 - 10) will be higher or lower than the current number.\nYou get 100 tokens for each round which you predict correctly.\nIf you predict incorrectly you will lose all of your earnings.")
    time.sleep(4)
    print("Cashout by inputting C.\n")
    time.sleep(1)
    num = random.randint(1,10)
    while True:
            tokens+=10
            time.sleep(1)
            print (num)
            choice = input("Higher or Lower (H/L)\n")
            if (choice == "C" or choice == "c") and goes >= 100:
                break
            if (choice == "C" or choice == "c") and goes < 100:
                "Cannot checkout!"
                pass
            else:
                pass
            num1 = random.randint(1,10)
            print (num1)
            time.sleep(1)
            if (num1 >= num and (choice == "H" or choice == "h")) or (num1 <= num and (choice == "L" or choice == "l")):
                print("You are correct and have earned 100 tokens!\n")
                tokens+=100
                num = num1
                goes+=100
                pass
            else:
                print("You are incorrect and have lost all of your earnings")
                tokens -= goes
                time.sleep(1)
                print ("You now have", tokens "tokens remaining.")
                break
    game_end()

print ("Welcome to the arcade!")
time.sleep(0.5)
print("You have", tokens, "tokens remaining.")
time.sleep(1)
main()
\$\endgroup\$
2
  • \$\begingroup\$ One easy win would be to eliminate those upper and lower case character checks. Just convert the character to lower case and compare against just a single char e.g. (choice == "H" or choice == "h") => (choice.lower() == "h") \$\endgroup\$ Mar 19 '17 at 22:41
  • \$\begingroup\$ Well, before simplification first fix the bugs.... \$\endgroup\$ Mar 19 '17 at 22:46
1
\$\begingroup\$

slotmachine() and higherorlower() call gameend(). In turn, gameend() may call main(). This is considered "spaghetti code", because functions should not be used as goto labels.

Rather, if your code acts as a loop, then it should be written with a loop, like this:

def main():
    while True:
        menu = int(input("What would you like to play?(Enter the corresponding number for each one)\n1. Slot Machine - 20 coins per turn\n2. Higher or Lower - 10 coins per turn\n"))
        if menu == 1:
            slot_machine(tokens)
        elif menu == 2:
            higher_or_lower(tokens)
        if game_end():
            break

The way you pass tokens is inappropriate. In main(), you are using a global variable tokens. However, in slotmachine() and higherorlower(), there is a local variable coincidentally named tokens, due to the function parameter. Make up your mind whether you want to use a global variable or pass the state around using parameters and return values.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.