Jump to content

Python: Probably a simple fix I am missing but...

I am relatively new to python and am teaching my self some before taking a class on it in my school which starts soon, basically I am making a project manager and I need to to call my other programs to use (which it is doing fine), after I tell it to "close" the program I want it to bring me back to the manager main page, which it is also doing fine, however I want to be able to call a program multiple times, for example on my screen I want to be able to use my calculator, "close" it (so quit that one script to go back to my main one) and then go back to calculator, however after closing one of the programs I can't get it to come back.

 

tldr; I need to be able to use a program multiple times, right now after "closing" once I can't re-"open" it.

 

Note: the reason I am not importing the script at the beginning is because it keeps running it instantly, which I don't want it to do. Also I am new to python so any other tips you may have for me will be well appreciated. 

 

Manager (Main script) code:

import timePassword = 'password'count = 0while count < 4:    username = raw_input ('Username Please: ')    if username == 'Close':        break    password = raw_input ('Password Please: ')    if password == 'Close':        break    while password == Password:        print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \nWelcome to your Manager ' + username + '!'        print 'Projects: \n%5s Calculator | Games \n%10s Music | Madlibs \n %11s Log Out' %(' ', ' ', ' ')        program = raw_input ('Which program would you like to enter? ').title()        if program == 'Calculator': #Calculator            import Projects.Calculator          elif program == 'Games': #Games            print 'Games'        elif program == 'Music': #Music            print 'Music'        elif program == 'Madlibs': #Madlibs            import Projects.Madlibs        elif program == 'Log Out': #Close            print 'Project Manager Logging Out, Type Close In Login Box To Shut Down.'            time.sleep(2)            break        else:            print 'Error: That is not a program on this Project Manager.'    if password != Password and count != 2:        print 'Wrong password, please try again.'    count += 1    if count == 3:        print 'ERROR: Too many failed password attempts, manager is now closing down.'        break 

 

Calculator (one of the scripts I want to import and use) code:

from __future__ import division def add():    a = int(raw_input('First Number: '))    b = int(raw_input('Second Number: '))    return a + bdef sub():    c = int(raw_input('First Number: '))    d = int(raw_input('Second Number: '))    return c - ddef div():    e = int(raw_input('First Number: '))    f = int(raw_input('Second Number: '))    return e / fdef mul():    g = int(raw_input('First Number: '))    h = int(raw_input('Second Number: '))    return g * hdef exp():    i = int(raw_input('First Number: '))    j = int(raw_input('Second Number: '))    return i ** jwhile True:    print 'Calculator: (Integers Only Please.)'    method = raw_input('(A)ddition, (S)ubtraction, (D)ivision, (M)ultiplication, (E)xponents, (C)lose? ')    if method == 'A':        print 'Adding: '        print add()    elif method == 'S':        print 'Subtracting: '        print sub()    elif method == 'D':        print 'Dividing: '        print div()    elif method == 'M':        print 'Multiplying: '        print mul()    elif method == 'E':        print 'Exponizing: '        print exp()    elif method == 'C':        break    else:        print 'Error: Please choose A, S, D, M, E, or C.' 
 

 

Intel 3570K - MSI GTX 660Ti 3GB OC Edition - 16GB Corsair LP RAM - ASRock Extreme4 Motherboard - Corsair HX850 - Adata Premier Pro SP900 120GB SSD with Windows 7 - Seagate Barracuda 1TD HDD - Seagate Barracuda 500GB HDD - Thermaltake Frio CPU Cooler - CM Storm Enforcer Case - Macbook Pro Early 2011 Laptop

Link to comment
https://linustechtips.com/topic/53868-python-probably-a-simple-fix-i-am-missing-but/
Share on other sites

Link to post
Share on other sites

I haven't used python a lot, but if I had to guess I would say you cannot import a module more than once. And even if you can, you shouldn't!

What you should be doing instead is make a function with your calculator code and calling that function when the user asks for it.

 

Also, your program could use some restructuring: the way you have it makes the password authentication the main concern of your project than the manager itself. From an architecture standpoint it would become a nightmare to change the authentication procedures or policies. Also, when the users issues the 'Log out' command in the manager he would be taken to the user/password prompt again and it counts as a failed authentication.

The way I'd do it would make the authentication code in a separate function and the rest of the code would only run if the result of that function is positive.

 

EDIT (regarding your note): the reason the calculator runs instantly when you import it at the beginning of the file is because it is not a function: the while loop is executed as soon as it is found.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×