1

very new to Python, and I was trying to make a tip calculator for my manager at work.

The way tips are calculated is that the percent of the day the server works, they get that percent of the tips. For example, if Server 1 works 30% of the hours that day, they receive 30% of the tips that were given that day.

I am stuck trying to figure out how to divide a number (the variable "hourNum" in the code) by each element in the array that stores the hours servers worked. This would get me a percentage of the day that the server worked.

With the code below, the math does not seem to calculate properly nor print correctly.

I have been unable to find anything similar on Stack Overflow that asks the same question unfortunately.

Attached below is the source code, the problem is in the last section titled "calculate percent of day server worked":

#get hours in the day
print('Enter how many hours were worked in the day: ')

hourNum = int(input())

#get tips for the day
print("Enter how much tips were earned (enter to the nearest whole dollar, do not use a dollar sign): ")

tipNum = int(input())


# creating an empty list 
lst = [] 
  

# number of elemetns as input 
serverNum = int(input("Enter number of servers that worked the day : ")) 
  

# iterating till the range 
print("Enter the number of hours each server worked (in order): ")

for i in range(0, serverNum): 
    ele = int(input()) 
  
    lst.append(ele) # adding the element 
      
print("You entered: ", lst) 


#calculate percent of day servers worked
n = 0
for i in range (0,serverNum):
    print (hourNum / lst[0 + n])
    n+1

Any tips or help would be much appreciated :)

1
  • Can you tell us what is your exact input and expected output? Commented Jun 27, 2020 at 4:26

2 Answers 2

1

The expression hourNum / lst[i] should be 100 * lst[i] / hourNum so that this value represents the percentage of hours in the workday (hourNum) that each server worked, multiplied by 100 so that it is a percentage. In addition, instead of printing the final percentages, why not store those in a list so the program remembers them?

#get hours in the day
hourNum = int(input('Enter how many hours were worked in the day: '))

#get tips for the day
tipNum = int(input("Enter how much tips were earned (enter to the nearest whole dollar, do not use a dollar sign): "))

# creating an empty list 
lst = [] 

# number of elemetns as input 
serverNum = int(input("Enter number of servers that worked the day: ")) 
  
# iterating till the range 
print("Enter the number of hours each server worked (in order): ")

for i in range(0, serverNum): 
    ele = int(input()) 
  
    lst.append(ele) # adding the element 
      
print("You entered:", lst) 


#calculate percent of day servers worked
lst_percents = []
for i in range (0,serverNum):
    lst_percents.append(round(100* lst[i] / hourNum))

print("The percent of day the servers worked:", lst_percents)

Sample input and output:

Enter how many hours were worked in the day: 10
Enter how much tips were earned (enter to the nearest whole dollar, do not use a dollar sign): 100
Enter number of servers that worked the day: 4
Enter the number of hours each server worked (in order): 
1
2
4
5
You entered: [1, 2, 4, 5]
The percent of day the servers worked: [10, 20, 40, 50]

I will leave the calculation of the tips for each server to you. Hopefully this will be easier with the a list for the percent of day the servers worked.

Sign up to request clarification or add additional context in comments.

1 Comment

If you found my answer helpful, please consider accepting it, thank you!
0

Change your last chunk of code to this:

#calculate percent of day servers worked

for i in lst:
    print(f"Worker {i+1}: {round(i/sum(lst)*100)}%")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.