Results 1 to 7 of 7

Thread: Python: I can't figure out why this crashes.

  1. #1
    Join Date
    Mar 2008
    Beans
    79
    Distro
    Ubuntu 10.04 Lucid Lynx

    Python: I can't figure out why this crashes.

    This used to work. I must have accidentally altered a character or something.

    Note: The odd part is that I can drag and drop various parts of it into the interpreter and they run fine. I can get it to output it to a file just fine by dragging the function over. It crashes where commented when I attempt to run the entire thing.

    PHP Code:
    #! /usr/bin/python


    def get_run():
        
    raw_input('\n\nRun again? (y/n): ')
        while 
    != 'y' and != 'Y' and != 'n' and != 'N':
            
    raw_input('Run again? (y/n): ')
        if 
    == 'Y':
            
    'y'
        
    return r


    def get_dest
    ():
        
    ''
        
    while != 's' and != 'S' and != 'f' and != 'F':
            
    raw_input('Output to (F)ile or (S)creen?: ')
        if 
    == 'S''s'
        
    if == 'F''f'
        
    return d


    def fib_calc
    (d):
        
    long(raw_input('\nPlease enter a number: '))
        if 
    >= 0:
            
    aboutput 01, [0]
            while 
    <= n:
                
    output.append(b)
                
    abb
            
    if == 's':
                print 
    '\nFibanacci numbers <='n'\n',
                print 
    output,
            
    elif d == 'f':
                
    fname 'Desktop/FibOutput' str(n)
                print 
    fname
                check 
    raw_input('one')
    #crash here --->
                
    open(fname'w')
                
    check raw_input('two')
                
    f.write(str(output))
                
    f.close
                
    print '\nResults written to 'fname,
        else: print 
    '\nError: Unsupported number.',


    run =  'y'
    while run == 'y':
        
    dest get_dest()
        
    fib_calc(dest)
        
    run get_run() 
    Thanks for any thoughts.

  2. #2
    Join Date
    Mar 2009
    Beans
    10

    Re: Python: I can't figure out why this crashes.

    Are you running it while inside a different directory (not containing a Desktop directory) ?

  3. #3
    Join Date
    Mar 2008
    Beans
    79
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python: I can't figure out why this crashes.

    I've tried it both ways. It used to work from Documents.

    I'm just going to re-write it. I've already found tons of things I need to do better. I am curious why it fails though.
    Last edited by Arazu; March 24th, 2009 at 07:22 PM.

  4. #4
    Join Date
    Feb 2008
    Beans
    367

    Re: Python: I can't figure out why this crashes.

    I copied and pasted your code into a file called "test.py" and ran it VIA commandline with the command "python test.py" It worked for me, both ways, with a file, and to screen. I have Python 2.4.3.

    Maybe you have hidden characters in your file or something.

  5. #5
    Join Date
    Mar 2008
    Beans
    79
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python: I can't figure out why this crashes.

    PHP Code:
    douglas@Prometheus:~/Documents/PythonScriptspython Fibonacci.py
    Output to 
    (F)ile or (S)creen?: f

    Please enter a number
    3
    Desktop
    /FibOutput3
    one
    Traceback 
    (most recent call last):
      
    File "Fibonacci.py"line 46in <module>
        
    fib_calc(dest)
      
    File "Fibonacci.py"line 36in fib_calc
        f 
    open(fname'w')
    IOError: [Errno 2No such file or directory'Desktop/FibOutput3' 
    I moved the .py file to my desktop and changed fname to fname = 'FibOutput' + str(n).

    Now it works. I don't know why it worked last night and now it's complaining at me.

  6. #6
    Join Date
    Dec 2007
    Location
    .
    Beans
    Hidden!
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Python: I can't figure out why this crashes.

    Quote Originally Posted by Arazu View Post
    PHP Code:
    douglas@Prometheus:~/Documents/PythonScriptspython Fibonacci.py
    Output to 
    (F)ile or (S)creen?: f

    Please enter a number
    3
    Desktop
    /FibOutput3
    one
    Traceback 
    (most recent call last):
      
    File "Fibonacci.py"line 46in <module>
        
    fib_calc(dest)
      
    File "Fibonacci.py"line 36in fib_calc
        f 
    open(fname'w')
    IOError: [Errno 2No such file or directory'Desktop/FibOutput3' 
    I moved the .py file to my desktop and changed fname to fname = 'FibOutput' + str(n).

    Now it works. I don't know why it worked last night and now it's complaining at me.
    You need to change the filepath to an absolute filepath "/home/yourusername/Desktop/FibOutput"

    or move your program to your home folder and run it from there.

  7. #7
    Join Date
    May 2006
    Location
    Victoria, Australia
    Beans
    648
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Python: I can't figure out why this crashes.

    A couple of things unrelated to your problem directly (seeing as the above post seems to have the solution anyways ).

    instead of using while r != 'y' and r != 'Y' and r != 'n', etc you can do

    while r not in ('y', 'Y', 'n', 'N')

    better yet, because you're dealing with strings here, we can do

    while r not in ("yYnN"):

    then we can further reduce the code by doing

    while r.lower() not in ("yn"):

    and then when you return r, we can do
    return r.lower()
    instead of checking if it's capital or not.

    So the first two functions become

    PHP Code:
    def get_run():
        
    #get initial input
        
    =raw_input('\n\nRun again? (y/n): ')
        
        
    #While the input is incorrect
        #  keep asking for correct input
        
    while r.lower() not in ('yn'):
            
    raw_input('Run again? (y/n): ')
            
        return 
    r.lower()

    def get_dest():
        
    question 'Output to (F)ile or (S)creen?: '
        
        
    raw_input(question)
        while 
    d.lower() not in ('sf'):
            
    raw_input(question)
        
        return 
    d.lower() 
    Then we can see that the two functions are very similar, so we can create a decorator function that reduces code repetition a little more

    PHP Code:
    def get_value(f):
        
    def replacementFunc():
            
    options f()
            if 
    len(options) == 2:
                
    question1acceptableInput options
                question2 
    question1
            
            elif len
    (options) == 3:
                
    question1acceptableInputquestion2 options
            
            d 
    raw_input(question1)
            while 
    d.lower() not in list(acceptableInput):
                
    raw_input(question2)
            
            return 
    d.lower()
        
        return 
    replacementFunc

    @get_value
    def get_run
    ():
        return (
    '\n\nRun again? (y/n): ',
            
    'yn',
            
    'Run again? (y/n): ',
        )

    @
    get_value
    def get_dest
    ():
        return (
    'Output to (F)ile or (S)creen?: ',
            
    'sf',
        ) 
    Whilst you could probably argue that reduces readability slightly, if you have a lot of these type of functions, that's quite a bit of typing you can save

    Anywho, enough procrastination, I think I'll return to my uni studies now
    if u find some of my ideas weird...look at my avatar for the reason
    http://delfick.storage.googlepages.c...ycfuserbar.png

Bookmarks

Posting Permissions

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