Python Forum
True or false if running something?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
True or false if running something?
#1
Hi, there! I'm writing a program as a project and it is meant to assist users with simple tasks. I have my own set of commands that I have made global in my program. Most commands will not allow another command to be executed until the current one is completed or aborted.
Basically, data can be messed up if the "cancel" command I made is executed when no command is currently running.
What I was attempting to do was have a global indicator that changes from True and False depending on if a command is running.
However, I tried putting it in 100 different ways, and every time, it would be ignored or raise an error.
Is there some sort of way to make a proper indicator so my "cancel" command wont run if no other command is?
This is all jumbled up grammatically; I hope it is still decently legible.
I also apologize if my code looks like a wreck ^^; I just started because of an assignment and im worried its pretty silly looking..

This is the cancel command.
cmd(input) is just the default operation that prompts a command to run
lecnac is just the cancel command, but it doesnt reprompt with the same huge amount of text.
I understand that it looks like there'd still be no problem with this, but it's something I need to fix.
def _cancel():
    if COMMAND(t) == True:
        print('WARNING: Aborting a command will not save your data.')
        print('Continue? (yes/no)')
        print('')
        yn = input()
        if yn == str('yes'):
           print('Action aborted.')
           print('')
           cmd(input)
        elif yn == str('no'):
            print('Action resumed.')
            print('')
        else:
            print('')
            print('You did not follow the specified format. Please try again.')
            _lecnac()
    else:
        print('You are not currently running a command.')
        print('')
        cmd(input)
I don't have an example of me trying to indicate what I was talking about, but I set up an example with my quit command (t/f), and
hopefully this will give enough explanation on what I am trying to do.

def _quit():
    t
    print('Are you sure you want to quit? (yes/no)')
    yn = input()
    if yn == str('_cancel'):
        _cancel()
    if yn == str('yes'):
        print('Goodbye.')
    elif yn == str('no'):
        print('')
        f
        cmd(input)
    else:
        print('You did not follow the specified format. Please try again.')
        _quit()
Reply
#2
What is COMMAND()?

Without seeing it, and without having you rewrite it to use a class, I'd say toss a global variable in. Something like
command_running = False

def _cancel():
    global command_running
    if command_running:
        # do stuff
    else:
        print("no command to cancel")
Then, each command would need to also use that variable, to set it to True when it starts, and to False when it was done. Or, if you have a launcher object, that could handle it for you.
Reply
#3
(Mar-26-2018, 07:43 PM)nilamo Wrote: What is COMMAND()? Without seeing it, and without having you rewrite it to use a class, I'd say toss a global variable in. Something like
command_running = False def _cancel(): global command_running if command_running: # do stuff else: print("no command to cancel")
Then, each command would need to also use that variable, to set it to True when it starts, and to False when it was done. Or, if you have a launcher object, that could handle it for you.
I tried putting it in like you said, but it said command_running was not defined, even though I am pretty sure I specified every time a command was and wasn't running. Here is the entirety of my code.
I'm pretty sure I misunderstood what you meant entirely. This should probably be in the assignment section but way more people are active here and I know most things, however, I don't think I know how to set up a global variable.
tbh kind of embarrassed sharing my coding as its a mess to me, but everyone starts somewhere, right? Razz
#! /usr/bin/env

# Program is meant to assist people with their assignments. User specific
# data is meant to aid people by type of class, or by specific needs.
# My profile will be the example.
# * = not guranteed feature
# -------------------------------------------commandbank-------------------------------------------

def cmd(userinput):
    command_running = False
    userinput = input()
    if userinput == str('_dylMaj'):
        _dylMaj()
        cmd(input)
    elif userinput == str('_quit'):
        _quit()
    elif userinput == str('_cancel'):
        _cancel()
    elif userinput == str('_class'):
        _class()
    else:
        print('')
        print('That is not a valid command. Type "_dylMaj" for a list of commands.')
        print('Remember to use an underscore when executing a dylMajor command.')
        print('')
        cmd(input)
    

def _dylMaj():
    command_running = True
    print('')
    print('--------------------------------------commandList--------------------------------------')
    print('_dylMaj - displays a list of commands')
    print('_info   - displays program info')
    print('_dev    - enter developer mode              (Define new classes and links)')
    print('_def    - enter definition mode             (Define new words)')
    print('_mstr   - enter master mode w/ password     (Edit user data and existing classes)')
    print('_wrk    - enter work mode                   (Filters out distractons)')
    print('_sUser  - save user profile                 (Only works with user profiles)')
    print('_sGeo   - save photo from geo calculator    (Can save at any given point.)')
    print('_class  - opens prompt for changing classes (Opens resources for specified class.)')
    print('_cancel - cancels current opperation        (Goes back to previous command executed)')
    print('_undo   - reenter a piece of information    (Ex: undos accidental user/word data)')
    print('_quit   - exit the program                  (uses break command)')
    print('---------------------------------------------------------------------------------------')
    print('')
    command_running = False

def _cancel():
    global command_running
    if command_running:
        print('WARNING: Aborting a command will not save your data.')
        print('Continue? (yes/no)')
        print('')
        yn = input()
        if yn == str('yes'):
           print('Action aborted.')
           print('')
           cmd(input)
        elif yn == str('no'):
            print('Action resumed.')
            print('')
        else:
            print('')
            print('You did not follow the specified format. Please try again.')
            _lecnac()
    else:
        print('You are not currently running a command.')
        print('')
        cmd(input)
def _lecnac():
    print('Continue? (yes/no)')
    print('')
    yn = input()
    if yn == str('yes'):
        print('Action aborted.')
        print('')
        cmd(input)
    elif yn == str('no'):
        print('Action resumed.')
        print('')
        cmd(input)
    else:
        print('')
        print('You did not follow the specified format. Please try again.')
        _lecnac()

def _quit():
    command_running = True
    print('Are you sure you want to quit? (yes/no)')
    yn = input()
    if yn == str('_cancel'):
        _cancel()
    if yn == str('yes'):
        print('Goodbye.')
    elif yn == str('no'):
        print('')
        command_running = False
        cmd(input)
    else:
        print('You did not follow the specified format. Please try again.')
        _quit()


# -------------------------------------------------------------------------------------------------
# (just more notes on what to do)
Reply
#4
(Mar-26-2018, 07:43 PM)nilamo Wrote: What is COMMAND()? Without seeing it, and without having you rewrite it to use a class, I'd say toss a global variable in. Something like
command_running = False def _cancel(): global command_running if command_running: # do stuff else: print("no command to cancel")
Then, each command would need to also use that variable, to set it to True when it starts, and to False when it was done. Or, if you have a launcher object, that could handle it for you.
I'm happy to report I did some tinkering and managed to get it to work.

Example of a command that uses your suggestion
def _quit():
    global command_running
    command_running = True
    print('Are you sure you want to quit? (yes/no)')
    yn = input()
    if yn == str('_cancel'):
        _cancel()
    if yn == str('yes'):
        print('Goodbye.')
    elif yn == str('no'):
        print('')
        command_running = False
        cmd(input)
    else:
        print('You did not follow the specified format. Please try again.')
        _quit()
Thank you very much! While I'm here still, I was curious to know if there's anything on my code I could tidy up? It's a bit jumbled.
Reply
#5
(Mar-27-2018, 05:31 PM)Artdigy Wrote: While I'm here still, I was curious to know if there's anything on my code I could tidy up? It's a bit jumbled.
Well, functions calling themselves isn't great. You're still learning, so it's not a big deal, but that could be replaced with a while loop. Taking your _quit() as an example:

def _quit():
    global command_running
    command_running = True
    
    while True:
        yn = input("Are you sure you want to quit? (yes/no) ")
        if yn == "_cancel":
            return _cancel()
        elif yn == "yes":
            print("Goodbye.")
            return
        elif yn == "no":
            print("")
            command_running = False
            return cmd(input)
        else:
            print("You did not follow the specified format. Please try again.")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  difference between «1 in [2] == False» and «(1 in [2]) == False» fbaldit 2 2,237 Apr-20-2020, 05:39 PM
Last Post: fbaldit
  Do break operators turn while loop conditions from True to False? Drone4four 5 2,962 Oct-24-2019, 07:11 PM
Last Post: newbieAuggie2019
  Returning True or False vs. True or None trevorkavanaugh 6 9,244 Apr-04-2019, 08:42 AM
Last Post: DeaD_EyE
  Returning true or false in a for loop bbop1232012 3 8,141 Nov-22-2018, 04:44 PM
Last Post: bbop1232012
  True == not False Skaperen 6 3,910 Aug-23-2018, 10:26 AM
Last Post: DeaD_EyE
  saving a true/false of a single value Skaperen 3 2,505 Aug-20-2018, 02:31 AM
Last Post: ichabod801
  Get True of false outside a loop morgandebray 2 2,451 Aug-09-2018, 12:39 PM
Last Post: morgandebray
  True vs False Skaperen 10 7,636 Jun-14-2017, 09:56 AM
Last Post: wavic
  How to turn variable true and false using function? hsunteik 5 6,501 Feb-20-2017, 11:44 AM
Last Post: hsunteik

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020