r/PythonProjects2 • • Nov 17 '21

[P] Moderate Python Program w/ Excel. Can anyone help?

Heyo everyone! I'm back again looking for some more tips/guidance in my python programming journey.

I have to edit some code to where it writes the results into a python program.

Column A will be employee names. Column B will be corresponding pay amounts.

I need to write the labels 'Name' into cell A1 and 'Pay Amount' in B1.

Here is the code I have so far---

# create blank dictionary

paychecks = {}

# Read in all the names and hours

empCount = int(input("\nHow many employees worked this week? "))

for nextEmp in range (1, empCount+1):

 name = input("\\nWho was employee " + str(nextEmp) + "? ")

 hours = int(input("How many hours did " + name + " work this week? "))

 paychecks\[name\] = hours

print ("Employee -- pay") #NEED TO CHANGE THIS BLOCK OF CODE

print ("---------------")

for nextEmp in paychecks.keys() :

 name = nextEmp

 hours = paychecks\[name\]

 if hours <= 40:

      pay = hours \* 8.50

 else :

      pay = (40\*8.50) + ( 12.75 \* (hours-40))

print(nextEmp, "---", pay)

I know right now the only thing I need to really edit is the 'Print' blocks of code, but I'm unsure of what to put in it's place to make the program run as intended. Any help would be greatly appreciated!

2 Upvotes

6 comments sorted by

2

u/Foreign_Flower1141 Nov 17 '21

Please edit the post and format code with ``` surrounding at the beginning and the end. It is pain to read and guess indentations

1

u/MtnDoobie Nov 17 '21

I think I did it. When I posted this from my PC the format was fine, but I’m on mobile now so I can’t tell if the format changed for you.

2

u/Foreign_Flower1141 Nov 17 '21

It's still not formatted. Just to make sure, by formatting I mean indenting the code that's being done by surrounding all of it in ``` symbols (that is, in markdown editor. You can use code highlighter in fancy pants editor)

# just like this
if True:
    print("I am indented")

1

u/Foreign_Flower1141 Nov 17 '21

So do you need to output excel file? You can use openpyxl library from pip.

1

u/MtnDoobie Nov 17 '21

I sort of fixed the format. For some reason it wont format the middle part of my code.

Basically as the code sits now, I enter how many employees worked this week lets say (1). Then the code says "Who was employee 1" (Bill), "how many hours did Bill work this week" (5) for example.

Then at the end of my current code, it Prints out the employees, and their total pay based on hours worked.

What I want to do, is-- Instead of print the Employee Name and total pay, I want the program to enter those values in an excel spreadsheet. A1- Name, B1 - Corresponding pay amounts. Then obviously fill in the data below A1 and B1. Does that sort of make sense?

Essentially almost the entire program is written, but instead of printing, I need all of the data to transfer to excel, into those 2 labeled columns.

1

u/Foreign_Flower1141 Nov 17 '21

I gotta sleep now, but you can check out openpyxl library. It's easy to use. Check out examples and quickstart section in the docs. I'll be able help you out in few hours, if still needed.