jeudi 1 avril 2021

Why does the same function run on different function call? [duplicate]

I am making a life simulator game, which is run by commands the user inputs (something like the Dank Memer bot from discord.) Firstly, here is my code:

# Built-In Imports
import time
import random
import sys
import os

black = "\033[0;30m"
red = "\033[0;31m"
green = "\033[0;32m"
yellow = "\033[0;33m"
blue = "\033[0;34m"
magenta = "\033[0;35m"
cyan = "\033[0;36m"
white = "\033[0;37m"
bright_black = "\033[0;90m"
bright_red = "\033[0;91m"
bright_green = "\033[0;92m"
bright_yellow = "\033[0;93m"
bright_blue = "\033[0;94m"
bright_magenta = "\033[0;95m"
bright_cyan = "\033[0;96m"
bright_white = "\033[0;97m"

st = 0


def sp(str):
    for letter in str:
        sys.stdout.write(letter)
        sys.stdout.flush()
        time.sleep(st)
    print()


yes = ['YES', 'yes', 'Yes', 'y', 'Y']
no = ['NO', 'no', 'No', 'n', 'N']
all_choices = ['NO', 'no', 'No', 'n', 'N', 'YES', 'yes', 'Yes', 'y', 'Y']

# <-- Commands -->
allowed_commands = ['help', 'joblist', 'job', 'jobresign', 'work', 'bal', 'balance', 'dep', 'deposit', 'with',
                    'withdraw', 'fish', 'hunt', 'shop', 'buy', 'sell', ]

# <-- Jobs -->
all_jobs = ['cosplayer', 'youtuber', 'manager', 'fastfoodcook', 'housewife', 'internettroll', 'teacher']
current_job = []
working = False

money = 500
deposit_allowed = 100
deposited = 0


def rules():
    sp(f"""
        {blue}\nHere are the commands you can use:
        To see the list of commands, type [help]
        {bright_green}Work/Job:
        \t{bright_blue}See all Jobs, {yellow}Command: [joblist],
        \t{bright_blue}Get a job, {yellow}Command: [job],
        \t{bright_blue}Resign from job, {yellow}Command: [jobresign]
        \t{bright_blue}Work on the job, {yellow}Command: [work],

        {bright_green}Money:
        \t{bright_blue}Check your balance, {yellow}Command: ['bal' or 'balance'],
        \t{bright_blue}Deposit money in the bank, {yellow}Command: [dep or deposit],
        \t{bright_blue}Withdraw money from the bank, {yellow}Command: [with or withdraw],
        \t{bright_blue}Beg, {yellow}Command: [beg],
        \t{bright_blue}Inventory, {yellow}Command: [inv or inventory],

        {bright_green}Shopping/Buying:
        \t{bright_blue}See the shop, {yellow}Command: [shop],
        \t{bright_blue}Buy Item,  {yellow}Command: [buy],
        \t{bright_blue}Sell Item,  {yellow}Command: [sell],

        {red} Note: More commands will unlock once you start buying things.
        """)


def gamePlay():
    while True:
        command = input(f"{bright_green}Command (type [help] to see list of all commands):\n>> ")
        while command in allowed_commands:
            # <-- Display Rules -->
            if command == 'help':
                rules()
                break

            # <-- Display Jobs -->
            elif command == 'joblist':
                joblist_function()
                break

            # <-- Get Jobs -->
            elif command == 'job' and working == False:
                job_funtion()
                break

            elif command == 'job' and working == True:
                print(f"\n{red}You are already doing a job. You can't work on two jobs,that is dumb...\n")
                break

            # <-- Resign Job -->
            elif command == 'jobresign':
                job_resign()
                break

            # <-- Work -->
            elif command == 'work' and working == True:
                work()
                break

            elif command == "work" and working == False:
                print(f"{red}\nLOL, you don't have a job, how you gonna work?\n")
                break

            # <-- Deposit -->
            elif command == 'dep' or 'deposit' and deposit_allowed != deposited:
                dep_money()
                break

            elif command == 'dep' or 'deposit' and deposit_allowed == deposited:
                print("You have a full bank kiddo...")
                break

            # <-- Balance -->
            elif command == 'bal' or 'balance':
                display_balance()
                break


def display_balance():
    while True:
        global money
        global deposit_allowed
        global deposited
        print(f"{bright_magenta}\nWallet: ⏣ {money}\nBank: {deposited}/{deposit_allowed}\n")
        break


def joblist_function():
    time.sleep(0.5)
    os.system('clear')
    print(f"""
    {bright_red}Here are the list of jobs:

    ■ Cosplayer,        ■ YouTuber,
    ■ Manager,          ■ Fast Food Cook,
    ■ House Wife,       ■ Internet Troll,
                ■ Teacher

    """)


def job_funtion():
    time.sleep(0.5)
    os.system('clear')
    global working
    global current_job

    while working == False:
        job_select = input(
            f"\n{magenta}What job do you want to work as? [Enter the job name in lowercase and without spaces]\n> ")
        while job_select in all_jobs:
            if job_select in all_jobs and working == False:
                print(f"\n{green}You are working as a {job_select.title()}\n")
                current_job.append(f"{job_select.title()}")
                working = True
                break

        else:
            print(f"{red}\nOIII, there is no job like that...\n")


def job_resign():
    time.sleep(0.5)
    os.system('clear')
    global current_job

    if len(current_job) == 0:
        print(f"{red}\nLOL, you don't even have a job. What are you resigning from?\n")

    if len(current_job) == 1:
        print("\nYou have resigned from your job. Congrats on being jobless... \n")
        current_job.pop()


def work():
    global money
    time.sleep(0.5)
    os.system('clear')
    words = ['ritzy', 'cool', 'unique', 'calendar', 'sweater', 'ancient', 'open', 'wave', 'blush', 'gold', 'nod',
             'racial']

    random_choice = random.choice([1])
    if random_choice == 1:
        random_word = random.choice(words)
        print(
            f"{bright_yellow}\nWrite down the word that will appear on the screen for half a second(it will be hard to see):")
        time.sleep(4)
        print(f"\n{black}{random_word}\n")
        time.sleep(0.5)
        os.system('clear')
        work1 = input(f"{green}\nWhat was the word?\n> ")

        if work1 == random_word:
            print(f"{green}\nBOSS: Great work. I am going to give you ⏣ 15,000 for this hour of work.\n")
            money += 15000
        if work1 != random_word:
            print(f"{red}BOSS: Terrible effort. I am going to give you ⏣ 3,000 for this hour of work.\n")
            money += 3000

    if random_choice == 2:
        pass
    if random_choice == 3:
        pass
    if random_choice == 4:
        pass
    if random_choice == 5:
        pass


def dep_money():
    global money
    global deposit_allowed
    global deposited

    money_to_dep = int(input(f"{bright_blue}\nHow much money do you want to deposit?(enter amount ONLY):\n>> "))
    while money_to_dep <= deposit_allowed - deposited:
        try:
            money -= money_to_dep
            deposited += money_to_dep
            break
        except Exception:
            print(f"{red}OIIIIII, Wrong INPUT. Try again.")
            break

gamePlay()

If you see the gameplay() function, you will see that in the third last elif statment, I have set this conditional in which the dep_money() function runs if you input 'dep' or 'deposit'.

Then in the last elif statement, the display_balance() function runs when you enter 'bal' or 'balance'.

But when I run the code, even if I type bal or dep, only the dep_money() function runs both times. See the Ouput:

Command (type [help] to see list of all commands):
>> bal

How much money do you want to deposit?(enter amount ONLY):
>> 1
Command (type [help] to see list of all commands):
>> dep

How much money do you want to deposit?(enter amount ONLY):
>> 

This is my problem. Why does the same function run even though I have explicitly stated that different functions should run in different inputs.

Also, if I move the last elif statement above the third last, which is like this:

# <-- Balance -->
elif command == 'bal' or 'balance':
    display_balance()
    break

# <-- Deposit -->
elif command == 'dep' or 'deposit' and deposit_allowed != deposited:
    dep_money()
    break

elif command == 'dep' or 'deposit' and deposit_allowed == deposited:
    print("You have a full bank kiddo...")
    break

You will see that the display_balance() function runs both times even if I input dep.

Why is this happening and how do I fix it?

Aucun commentaire:

Enregistrer un commentaire