Jump to content

My code is acting weird

Go to solution Solved by Mira Yurizaki,

The problem starts on line 43 of the mathtest module. If you intended the code to run inside the mathtest function, you need to indent the code. Otherwise it's out of the function's scope and mathtest is a function that does nothing. And since the scope outside of the function definition is basically the global scope and what I said earlier, Python will execute that code the moment you import the module.

This is my main for some python 3 code i'm writing. For some reason when i run the program, it runs one of my modules and not the main. Am i missing something?

I initially did not have the if_name_=='_main_': segment, but i did have the main() line. i just found that and tried to see if it would help... it didn't. 

 

import selection, joke, time, hello
def main():
    name=''
    username = hello.hello(name)
    games = input("Would you like to see my programs?\n")
    if games == 'y':
        selection.selection()
    elif games == 'n':
        print('Really',username,'?')
        time.sleep(.500)
        print('Your no fun')
        time.sleep(.500)
        jokeyn = input("Would you like to hear a joke then?\n")
        if jokeyn == 'y':
            joke.joke()
        else:
            print('Why are you here then?')
    else:
        exit()

if __name__== "__main__":
    main()

Link to comment
https://linustechtips.com/topic/964604-my-code-is-acting-weird/
Share on other sites

Link to post
Share on other sites

Python executes modules when you import them. That's the nature of its interpreted design. So if you have something like this:

print('Hello world!')

def print_test():
    print('Hello test!')

If you import this module, it's going to execute print('Hello world!') because it's executing this code as if you were punching it in the Python interpreter.

 

It's best to think of Python files as a script to save you from inputting the same things into the interpreter itself.

Link to comment
https://linustechtips.com/topic/964604-my-code-is-acting-weird/#findComment-11684514
Share on other sites

Link to post
Share on other sites

Thank you for the reply. I have a basic understanding of python (super basic) but this might help, Here is the main module for this code. I will attach a picture of the mathTest module that keeps running first. 

At the bottom you can see the console output shows the first input line from the mathTest module and not the main like it is suppose to.

Yes, I have selected the main module before running to tell the program i want that code to run but no matter which one i select it always runs the mathTest module. 

(There are a few logic errors in the mathTest code as well but that shouldn't effect it running only the expected output for that module)

 

5b81c04de2d83_Screenshot(11).thumb.png.c2fc58c2c0ec31ee243c301b84724168.png5b81bfe275edf_Screenshot(10).thumb.png.d5aee74682490bca089d69a9e77e6dfe.png

Link to comment
https://linustechtips.com/topic/964604-my-code-is-acting-weird/#findComment-11684940
Share on other sites

Link to post
Share on other sites

My apologizes. Here is the remainder of the mathTest code.

I know the code for the mathtest does not give me the correct output but it does run without to many problems. 

My issue is that it overrides my main module by running the mathtest first then it runs the main. 

 

I have attached image of the console output as well. For some reason, the mathTest runs first. 

 

when i go into selection module and make the call to the mathtest a note, the program runs just fine. once i enable the mathtest in selection it creates this issue. 

I have attached a third image of the selection module as well, if it helps. 

 

5b821698ae711_Screenshot(15).thumb.png.d8398d5c4e8d0d213613c9ca6f3b528e.png

5b8217326da51_Screenshot(16).thumb.png.9fd2bfb82374f66bd4cce72b682912ba.png

5b82180833568_Screenshot(17).thumb.png.4d287fa60c40f8305edeb875bbc3dece.png

Link to comment
https://linustechtips.com/topic/964604-my-code-is-acting-weird/#findComment-11685689
Share on other sites

Link to post
Share on other sites

The problem starts on line 43 of the mathtest module. If you intended the code to run inside the mathtest function, you need to indent the code. Otherwise it's out of the function's scope and mathtest is a function that does nothing. And since the scope outside of the function definition is basically the global scope and what I said earlier, Python will execute that code the moment you import the module.

Link to comment
https://linustechtips.com/topic/964604-my-code-is-acting-weird/#findComment-11685830
Share on other sites

Link to post
Share on other sites

8 minutes ago, SirFriendly2525 said:

I don't know how i missed something that simple. 

The battlecry of the software developer! :P

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
https://linustechtips.com/topic/964604-my-code-is-acting-weird/#findComment-11701796
Share on other sites

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

×