Find the answer to your Linux question:
Results 1 to 3 of 3
I have written a small program in python for calculating various computer-related equations. This is the code: Code: #!/usr/bin/env python # File name: compmathcalc.py running = True while running: print ...
  1. #1
    Just Joined!
    Join Date
    Nov 2006
    Location
    ~/
    Posts
    40

    Python Control Flow

    I have written a small program in python for calculating various computer-related equations. This is the code:

    Code:
    #!/usr/bin/env python
    # File name: compmathcalc.py
    
    running = True
    while running:
        print 'Type b for Bandwidth Calculator, c for Color Depth Calculator, or h for Help'
    
        mode = raw_input('Select mode : ')
    
        def b():
    
            print 'Bandwidth Calculator'
    
        speed = int(raw_input('Enter the speed in MHz : '))
        width = int(raw_input('Enter the width in bits : '))
    
        bandwidth = speed * width / 8
        print 'The bandwidth in MB/s is', bandwidth
    
        continue
    
        def c():
    
            print 'Color Depth Calculator'
        
        depth = int(raw_input('Enter the color depth in bits: '))
    
        colors = 2 ** depth
        print 'The number of colors is', colors
    
        continue
    
        def h():
    
            print 'This is the help section. It is currently under construction. :)'
    
        continue
    
        if mode == b:
    
            b
    
        if mode == c:
    
            c
    
        if mode == h:
    
            h
    
    else:
        print 'The while loop is over'
    
    print 'Done'
    The problem is that it doesn't "listen" to the defined variables. It will just jump from one mode to another despite what the input is.

    What do I need to do to fix this?

    Thanks,
    BigFoot

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by BigFoot13 View Post
    I have written a small program in python for calculating various computer-related equations. This is the code:

    Code:
    #!/usr/bin/env python
    # File name: compmathcalc.py
    
    running = True
    while running:
        print 'Type b for Bandwidth Calculator, c for Color Depth Calculator, or h for Help'
    
        mode = raw_input('Select mode : ')
    
        def b():
    
            print 'Bandwidth Calculator'
    
        speed = int(raw_input('Enter the speed in MHz : '))
        width = int(raw_input('Enter the width in bits : '))
    
        bandwidth = speed * width / 8
        print 'The bandwidth in MB/s is', bandwidth
    
        continue
    
        def c():
    
            print 'Color Depth Calculator'
        
        depth = int(raw_input('Enter the color depth in bits: '))
    
        colors = 2 ** depth
        print 'The number of colors is', colors
    
        continue
    
        def h():
    
            print 'This is the help section. It is currently under construction. :)'
    
        continue
    
        if mode == b:
    
            b
    
        if mode == c:
    
            c
    
        if mode == h:
    
            h
    
    else:
        print 'The while loop is over'
    
    print 'Done'
    The problem is that it doesn't "listen" to the defined variables. It will just jump from one mode to another despite what the input is.

    What do I need to do to fix this?

    Thanks,
    BigFoot
    thats because you have defined your functions with only the "print" statements. the rest of the statements does not belong to the various functions because of the indentations.
    Also, put your functions outside the while loop. you don't need to define them everytime inside the loop. If your functions return values, then you have to include return statement.
    Code:
    import sys
    def c():
         print 'Color Depth Calculator'    
         depth = int(raw_input('Enter the color depth in bits: '))
         colors = 2 ** depth
         return colors
         
    def b():
         print 'Bandwidth Calculator'
         speed = int(raw_input('Enter the speed in MHz : '))
         width = int(raw_input('Enter the width in bits : '))
         bandwidth = speed * width / 8
         return bandwidth
         
    def h():
         print 'This is the help section. It is currently under construction. :)'
    while 1:
        print """
        b) Bandwidth Calculator 
        c) Color Depth Calculator
        h) Help
        q) Quit
        """
        mode = raw_input('Select mode : ')    
        if mode == "b":
            result = b()
            print 'The bandwidth in MB/s is', result
        elif mode == "c":
            result=c()
            print 'The number of colors is', result
        elif mode == "h":
            h()
        elif mode=="q":
            sys.exit(0)

  3. #3
    Just Joined!
    Join Date
    Nov 2006
    Location
    ~/
    Posts
    40
    Ah, I see. Thanks for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...