Author Topic: my first python script  (Read 667 times)

0 Members and 1 Guest are viewing this topic.

Offline 0pt1musPr1m3

  • EZ's Asshole
  • Peasant
  • *
  • Posts: 89
  • Cookies: 90
  • Certified Asshole
    • View Profile
my first python script
« on: January 18, 2015, 06:46:01 am »
.
« Last Edit: September 14, 2015, 04:49:43 am by 0pt1musPr1m3 »
Don't measure yourself by what you have accomplished, but by what you should have accomplished with your ability.

Offline d4rkcat

  • Knight
  • **
  • Posts: 287
  • Cookies: 115
  • He who controls the past controls the future. He who controls the present controls the past.
    • View Profile
    • Scripts
Re: my first python script
« Reply #1 on: January 18, 2015, 07:08:13 am »
Looks pretty good to me, I don't think you needed the indents.
At least it ran fine for me once I removed them.
Did some minor tweaks to the code, for example in python:

Code: [Select]
a = a + b

becomes:

Code: [Select]
a += b

On the whole a nice script. Good job.
+1

Code: (Python) [Select]
#!/usr/bin/env python

import sys, time
from datetime import datetime

def write_logfile(data):
with open('worklogs.txt', 'a') as textfile:
textfile.write(data)

def new_entry(hours, wage, descr):
now = datetime.now()
if len(str(wage)) == 3:
now1 = 'Wage: %s, hours: %s, Description: %s %s %s/%s/%s'% (wage,hours,descr,now.strftime("%A")[0:3],now.month,now.day,now.year)
write_logfile(now1)

elif len(str(wage)) <= 2:
now1 = 'Wage: 0%s, Hours: %s, Description: %s %s %s/%s/%s'% (wage,hours,descr,now.strftime("%A")[0:3],now.month,now.day,now.year)
write_logfile(now1)

def new_payment(amount):
now = datetime.now()
now2 = str('Payment of %s recieved %s %s/%s/%s')% (amount,now.strftime("%A")[0:3],now.month,now.day,now.year)
write_logfile(now2)

def total_owed():
wages, payments = 0, 0
print "calculating total"
time.sleep(0.75)
for line in open('worklogs.txt'):
if line.startswith("Wage"):
amount = line[6:9]
wages += int(amount)
elif line.startswith("Payment"):
payment = line[11:14]
payments += int(payment)
totalOwed = wages - payments
print "Wages: " + str(wages)
print "payments: " + str(payments)
print "Total owed: " + str(totalOwed)

print "Did you work or recieve a payment today?\n1) Yes\n2) No"
choice1 = raw_input("Choose a menu number: ")

if choice1 == "1":
while True:   
print "1) Make a new entry"
print "2) Current amount owed"
print "3) Record payment recieved"
print "4) Quit"
choice = raw_input("Choose a menu number: ")

if choice == "1":
new_hours = raw_input("How many hours did it take? ")
new_wage = raw_input("How much did you make? ")
new_descr = raw_input("What did you do? ")
new_entry(new_hours, new_wage, new_descr)

elif choice == "2":
total_owed()

elif choice == "3":
payment = raw_input("How much were you paid? ")
new_payment(payment)

elif choice == "4":
sys.exit()

else:
print "Invalid entry"


elif choice1 == "2":
now = datetime.now()
now1 = 'OFF %s/%s/%s %s'% (now.month,now.day,now.year,now.strftime("%A")[0:3])
write_logfile(now1)

else:
print "Invalid entry"
Jabber (OTR required): thed4rkcat@einfachjabber.de    Email (PGP required): thed4rkcat@yandex.com    PGP Key: here and here     Blog

<sofldan> not asking for anyone to hold my hand uber space shuttle door gunner guy.


Offline Scient1st

  • /dev/null
  • *
  • Posts: 8
  • Cookies: -3
    • View Profile
Re: my first python script
« Reply #2 on: February 06, 2015, 02:40:33 pm »
Is it bad practice to combine multiple lines based on readability?


Most people would say it is. Python's indentation is designed to make code readable, and believe me, its better to use a line for each statement/expression. It might not look like much to the person writing the code, but it's a lot better for the person who tries to read it (and understand it). Once you get "Python eyes" from looking at indented code all the time, this sort of multi-statement lines can be a bit misleading, especially while debugging.