Home > Blockchain >  anyway to make this more efficent?
anyway to make this more efficent?

Time:11-05

for a project im working on i want a thing that asks for a response from 1 through 4, then asks if they want the disciption to be shown again apon using the script again, then changes it to the corosponding anwser. then asks if they user would like to contiue and if yes it runs the script

the solution ive came up with is quite long and unefficent

this is my code

confirm = 'not y :)'
while confirm != 'y':
    print('Press 1 for script 1')
    print('Press 2 for script 2')
    print('Press 3 for script 3')
    print('Press 4 for script 4')
    script = int(input('What script would you like to run?'))
    while script != range(1,4):
        print('Enter a number from 1 to 4')
        print('Press 1 for script 1')
        print('Press 2 for script 2')
        print('Press 3 for script 3')
        print('Press 4 for script 4')
        script = int(input('What script would you like to run?'))

    if script == 1:
        if booleans.discscript1 == True: 
            print('discription 1 blah  blahlbah lbabhl')
            while showagain != 'y' or showagain != 'n':
                showagain = input('Dont Show again(y/n)? ').lower()
                if showagain == 'y':
                    booleans.discscript1 = False
                elif showagain == 'n':
                    booleans.discscript1 = True
            confirm = input('Continue(y/n)?')
            while confirm != 'y' or confirm != 'n':
                confirm = input('Continue(y/n)?')
            if confirm == 'y':
                # run script 1
        else:
            confirm = input('Continue(y/n)?')
            while confirm != 'y' or confirm != 'n':
                confirm = input('Continue(y/n)?')
            if confirm == 'y':
                # run script 1

it all works fine besides ill need to write from the if script == 1: down 3 more times to have it work the way i want it to. im wondering if there is any way i could shorten it down/make it more efficent? i tryed using a class however im not familar enough with them to understand how i would do it in this instance

CodePudding user response:

if u want to check the user input, you are obligated to use if statements, or switch statements. What i can see is that your code is a litle messy, try to separate it into functions so it looks more clear, something like this

if script == 1:
    script_1_Called()
elif script == 2:
    script_2_Called()
elif script == 3:
    script3Called()
elif script == 4:
    script_4_Called()
else:
    print("there is not a script with number "   script)

also after u call a script u can make a function to ask if the user wants to run the script again, something like this

def ask_run_again():
    your_code...

with this u would not need to write the piece of code every time you want to use it.

also i recommend every new programmer to write variables with "_", like i used it makes the code look more clear to others, for example you use "showagain", i strongly recommend you to use "show_again", trust me it makes big pieces of code better to read.

CodePudding user response:

I see it like this:

class Script:

    def __init__(self, script_number, discr:str, code): #code should be a script func name without '()' at the end 
        self.discr_showing_status = True
        self.script_number = script_number
        self.code = code 
        self.discriptiption = discr 

        self.show_discr()  

    def run(self):
        return self.code(self.script_number)

    def show_discr(self):
        if self.discr_showing_status:
            print(self.discriptiption)
            while (showagain:=input('Dont Show again(y/n)? ').lower()) != 'n':               
                if showagain == 'y':     #do now show it again -> yes
                    self.discr_showing_status = False
                    break



def code_for_scripts(script_number):
    if script_number == 1:
        # code for script 1 should be here here
        # print('script1 returns something')
        return 'script1 returns something' 
    if script_number == 2:
        # code for script 2 should be here here
        # print('script2 returns something')
        return 'script2 returns something'
    if script_number == 3:
        # code for script 3 should be here here
        # print('script3 returns something')
        return 'script3 returns something'
    if script_number == 4:
        # code for script 4 should be here here
        # print('script4 returns something')
        return 'script4 returns something'



confirm='not y :)'
while confirm != 'y':
    for i in range(1,5):
        print(f'Press {i} for script {i}')
    script = int(input('What script would you like to run? (digits only!): '))         
    if script not in range(1,5):
        print('Enter a number from 1 to 4')
        continue
 
    script_instance = Script(script, f'discription {script} blah  blahlbah lbabhl', code_for_scripts)

    while (confirm:=input('Continue(y/n)?').lower()) != 'n':               
        if confirm == 'y':    
            print(script_instance.run())
            break

code_for_scripts is for example, your actual scripts may be of course different from this
cool thing is that there is no need to write a code for every script (if script == 1: ..., if script == 2: ..., etc). every time new script instance is to be created, and from that instance everything else will be done
of course the same logic could be realized by just a function but OOP is better choice
the major while loop could be altered into something with oop too, but I've not done that for you to see how it can be rewritten in a shorter way

  • Related