Writing things to <file>.<ext>

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1052 Views - Last Post: 11 July 2012 - 03:58 PM Rate Topic: -----

#16 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Writing things to <file>.<ext>

Posted 11 July 2012 - 03:49 PM

#!/usr/bin/python3
def main():
    check = input('\nDo you want to store usernames? (y/n)')

    if check != "n" and check != "y":
        print ('\nInvalid Input')
        return main()
    
    elif check != 'n' or 'N':
        output = input('\nWhat should the name of the file be, containing the username(s) be? ')
        out = open(output)
        if output == ' ' or '':
            main()
        else:
            while check != 'N' or 'n':
                uName = []
                number = int(input('How many usernames do you have?'))
                for x in range(number):
                    first = input('\n First Name: ')
                    last = input('\n Last Name: ')
                    try:
                        uName.append((first[0] + last[:(len(last)/2)]))
                        output.write(uName[x] + '\n')
                    except ValueError:
                        print ('Something went wrong')
                    finally:
                        out.close()
                again = input('\nDo you want to add more? (y,n) ')
                if again == 'n' or 'N':
                    print ('\nStored. Exiting program')
                    out.close()
                    return

    
    elif again == 'n' or 'N':
        print('Nothing Stored.')
main()



Thanks to sepp2k and Dogstopper, I have surpassed the previous error however I am greeted with a new one.
Traceback (most recent call last):
  File "/home/k3y/Python/Look2.py", line 37, in <module>
    main()
  File "/home/k3y/Python/Look2.py", line 22, in main
    uName.append((first[0] + last[:(len(last)/2)]))
TypeError: slice indices must be integers or None or have an __index__ method


Was This Post Helpful? 0
  • +
  • -

#17 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Writing things to <file>.<ext>

Posted 11 July 2012 - 03:51 PM

In Python 3 the division operator / returns a floating point number even if both operands are integers. So you can't use the result of / as an array index. Instead you should use the integer division operator //.
Was This Post Helpful? 1
  • +
  • -

#18 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Writing things to <file>.<ext>

Posted 11 July 2012 - 03:58 PM

View Postsepp2k, on 11 July 2012 - 05:51 PM, said:

In Python 3 the division operator / returns a floating point number even if both operands are integers. So you can't use the result of / as an array index. Instead you should use the integer division operator //.


Thank you sir; I believe that I finally got it ( I feel proud ):
#!/usr/bin/python3
def main():
    check = input('\nDo you want to store usernames? (y/n)')

    if check != "n" and check != "y":
        print ('\nInvalid Input')
        return main()
    
    elif check != 'n' or 'N':
        output = input('\nWhat should the name of the file be, containing the username(s) be? ')
        out = open(output)
        if output == ' ' or '':
            main()
        else:
            while check != 'N' or 'n':
                uName = []
                number = int(input('How many usernames do you have?'))
                for x in range(number):
                    first = input('\n First Name: ')
                    last = input('\n Last Name: ')
                    try:
                        uName.append((first[0] + last[:(len(last)//2)]))
                        out.write(uName[x] + '\n')
                    except:
                        pass
                    finally:
                        out.close()
                again = input('\nDo you want to add more? (y,n) ')
                if again == 'n' or 'N':
                    print ('\nStored. Exiting program')
                    out.close()
                    return

    
    elif again == 'n' or 'N':
        print('Nothing Stored.')
main()



it may not pass a professionals standard but I think for one of my first true script redos it is pretty snappy =D.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2