I want to make a function which pulls the data from the code and creates a table out of it:
if mode_choice ==1 or 2:
def main():
pv= float(input("Enter the principle amount: "))
interest = float(input("Enter the interest rate (in perecent): "))
term = int(input("Enter the term (in months): "))
interest = interest / 12 / 100
payment = (interest * pv) / (1-(1 + interest)**-term)
main()
Related
I have run this code and changed it more times than I can count but it always results in a loop. I'm not quite sure what I've done wrong nor how to end the loop.
"""
WeeklyPay.py: generate payroll stubs for all hourly paid employees and summarizes them
"""
def main():
"""
total_gross_pay = 0
hours_worked = 0
gross_pay = 0
hourly_rate= 0
:return: None
"""
employee = input("Did the employee work this week? Y or y for yes: ")
while employee == "Y" or "y":
hours_worked = int(input("How many hours did the employee work this week? "))
hourly_rate = int(input("What is the employee's hourly rate? "))
gross_pay = hours_worked * hourly_rate
print("Your weekly pay is: "+ gross_pay)
main()
You might find the while loop shown below works more like what your program should do:
def main():
"""Help the user calculate the weekly pay of an employee."""
while input('Did the employee work this week? ') in {'Y', 'y'}:
hours_worked = int(input('How many hours? '))
hourly_rate = int(input('At what hourly rate? '))
gross_pay = hours_worked * hourly_rate
print('The weekly pay is:', gross_pay)
if __name__ == '__main__':
main()
You are using a while loop, in which the variable employee is never changed, so the condition stays True. It should work if you replace the while with an if.
Could I know what's missing in my UDF , my brain is fried atm and couldn't figure out what went wrong.
import math
import numpy as np
CoP = input("Enter the Value 1: ")
Po = input("Enter the Value 2: ")
Rf = input("Enter the Value 3: ")
Ti = input("Enter the Value 4: ")
def Total():
Rf = Rf / 100
Ti = Ti/ 12
Total = CoP - (Po * np.e ** (-(Rf * Ti)))
print(Total)
return Total
~Brittany
You cant use the global vars directly inside the method
Either pass the vars as arguments or declare the vars as global inside the method
Edit~: Converted the str to float
Method 1
import numpy as np
CoP = float(input("Enter the Value 1: "))
Po = float(input("Enter the Value 2: "))
Rf = float(input("Enter the Value 3: "))
Ti = float(input("Enter the Value 4: "))
def Total(CoP, Po, Rf, Ti):
Rf = Rf / 100
Ti = Ti / 12
Total = CoP - (Po * np.e ** (-(Rf * Ti)))
print(Total)
return Total
Total(CoP, Po, Rf, Ti)
Method 2
CoP = float(input("Enter the Value 1: "))
Po = float(input("Enter the Value 2: "))
Rf = float(input("Enter the Value 3: "))
Ti = float(input("Enter the Value 4: "))
def Total():
global CoP, Po, Rf, Ti
Rf = Rf / 100
Ti = Ti / 12
Total = CoP - (Po * np.e ** (-(Rf * Ti)))
print(Total)
return Total
Total()
here is my code, I'm not sure what I can do. every time I try to run this code, no matter what I've changed I get hung up at the same spot: "builtins.NameError: name 'hourly_pay' is not defined" I've marked in the code where which line it's in; where I try to define "display_results"
def main():
display_message()
#Get name
employee_name=input("Enter employee's name: ")
#Get Sales
sales_amount = float(input("Enter the sales amount: "))
#Get Hours
hours_worked = int(input("Enter hours worked by this employee: "))
#Calculate Hourly Pay
hourly_pay = hours_worked * hourly_pay_rate
#Calculate Commission
commission = sales_amount * commission_rate
#Calculate Gross Pay
gross_pay = hourly_pay + commission_rate
#Calculate Witholding
witholding = gross_pay * witholding_rate
#Calculate Net Pay
net_pay = gross_pay -witholding
display_results = (hourly_pay, commission, gross_pay, witholding, net_pay)
def display_message():
print('This program uses functions to calculate pay, commission, gross')
print('pay, witholding, and net pay.')
def display_results():
print('Hourly pay amount is: ' (hourly_pay)) #<-- This is where it errors
print('commission amount is: ' (commission))
print('Gross pay amount is: ' (gross_pay))
print('Witholding amount is:' (witholding))
print('Net pay amount is:' (net_pay))
hourly_pay_rate = 7.5
commission_rate = 0.05
witholding_rate = 0.25
main()
display_results()
You are trying to reference global variable inside the local scope which will fail. Also you are trying to auto type cast the str to int. The correct way to do this is show below:
Solution
Use global keyword to refer to global scope variable. Also Refer to Doc on global vs local scope variables.
def main():
global hourly_pay
display_message()
#Get name
employee_name=input("Enter employee's name: ")
#Get Sales
sales_amount = float(input("Enter the sales amount: "))
#Get Hours
hours_worked = int(input("Enter hours worked by this employee: "))
#Calculate Hourly Pay
hourly_pay = hours_worked * hourly_pay_rate
#Calculate Commission
commission = sales_amount * commission_rate
#Calculate Gross Pay
gross_pay = hourly_pay + commission_rate
#Calculate Witholding
witholding = gross_pay * witholding_rate
#Calculate Net Pay
net_pay = gross_pay -witholding
display_results = (hourly_pay, commission, gross_pay, witholding, net_pay)
def display_message():
print('This program uses functions to calculate pay, commission, gross')
print('pay, witholding, and net pay.')
def display_results():
global hourly_pay
print('Hourly pay amount is: {}'.format(hourly_pay))
# print('commission amount is: ' (commission))
# print('Gross pay amount is: ' (gross_pay))
# print('Witholding amount is:' (witholding))
# print('Net pay amount is:' (net_pay))
if __name__ == "__main__":
hourly_pay = 0.00
commission = 0.00
gross_pay = 0.00
witholding = 0.00
net_pay = 0.00
hourly_pay_rate = 7.5
commission_rate = 0.05
witholding_rate = 0.25
main()
display_results()
Let us know if this solves your problem or not. I have tried it myself and returns 15.0 as output.
count = 0
while (count >4):
fixedcosts = float(input("Enter fixed costs: "))
salesprice = float(input("Enter the price for one unit: "))
variablecosts = float(input("Enter the variable costs for one unit: "))
contribution = float(salesprice)-float(variablecosts)
breakevenpoint = float(fixedcosts)/float(contribution)
roundedbreakevenpoint = round(breakevenpoint)
#Finds break even point
if int(roundedbreakevenpoint) < float(breakevenpoint):
breakevenpoint2 = int(roundedbreakevenpoint) + 1
print("Your break even point is ",int(breakevenpoint2),"units")
else:
print("Your break even point is ",int(roundedbreakevenpoint),"units")
#Finds number of units needed to make a profit
if int(roundedbreakevenpoint) < float(breakevenpoint):
breakevenpoint3 = int(roundedbreakevenpoint) + 2
print("To make a profit you need to sell ",int(breakevenpoint3),"units")
else:
int(roundedbreakevenpoint) >= float(breakevenpoint)
breakevenpoint4 = int(roundedbreakevenpoint) + 1
print("To make a profit you need to sell ",int(breakevenpoint4),"units")
decision = input("Would you like to restart the program?")
if decision == 'yes' or 'Yes':
count = count + 1
print("This program will now restart")
else:
print("This program will now be terminated")
print("Press enter to stop the program")
quit()
What is the best way to loop this code? I have tried while loop but I can't seem to be able to get it to work with a yes no answer.
def Get_Details():
Student_Name = input("Enter the name of the student: ")
Coursework_Mark = int(input("Enter the coursework mark achieved by the student: "))
while Coursework_Mark < 0 or Coursework_Mark >60:
print("Try again, remember the coursework mark is out of 60.")
Coursework_Mark = int(input("Enter the coursework mark achieved by the student: "))
Prelim_Mark = int(input("Enter the prelim mark achieved by the student: "))
while Prelim_Mark < 0 or Prelim_Mark > 90:
print("Try again, remember the prelim mark is out of 90.")
Prelim_Mark = int(input("Enter the prelim mark achieved by the student: "))
return Student_Name, Coursework_Mark, Prelim_Mark
def Calculate_Percentage(Coursework_Mark, Prelim_Mark):
Percentage = ((Coursework_Mark + Prelim_Mark)/150) * 100
if Percentage >= 70:
Grade = "A"
elif 60 >= Percentage <= 69:
Grade = "B"
elif 50 >= Percentage <= 59:
Grade = "C"
elif 45 >= Percentage <= 50:
Grade = "D"
else:
Grade = "No Award"
return Percentage, Grade
def Display_Results(Student_Name, Grade):
print(Student_Name + " achieved a grade " + str(Grade) + ".")
#MAIN PROGRAM
Student_Name, Coursework_Mark, Prelim_Mark = Get_Details()
Percentage = Calculate_Percentage(Coursework_Mark, Prelim_Mark)
Display_Results(Student_Name, Grade)
At the end of the program I receieve:
Program.py", line 41, in <module>
Display_Results(Student_Name, Grade)
NameError: name 'Grade' is not defined
How can this be fixed? Please help, thank you.
This program asks the user their name, coursework mark (out of 60) and prelim mark (out of 90) and calculates their percentage which is send to their screen as a grade along with their name.
Function Calculate_Percentage returns two values, Percentage and Grade. It looks like you wanted to assign them to a separate variable each exactly like you did with three values from the Get_Details call in the line above.
So last two lines should look like this:
Percentage, Grade = Calculate_Percentage(Coursework_Mark, Prelim_Mark)
Display_Results(Student_Name, Grade)
Please use a Pythonic naming convention to make your code more readable. For example variable names are usually all_lower_case.