How can I use input function to fill the parameters? - python-3.x

Create a function called computepay which takes two parameters (hours and rate ).
Enter Hours: 45
Enter Rate: 10
Pay: 475.0
This is my code, but it doesn't show an input dialog when I run the program.
def computepay(x, y):
x = input('Enter Hours: ')
y = input('Enter Rate: ')
if int(x) <= 40 :
print('\nPay: ' + int(x) * int(y))
else :
print('\nPay: ' + 40 * int(y) + ((int(x) - 40) * 15))

If you want to just call the function with zero or empty string or even you can pre define in it, now you can get the input dialog in whatever ide you are using
def computepay(x=0, y=0):
x = input('Enter Hours: ')
y = input('Enter Rate: ')
if int(x) <= 40 :
print('\nPay: ' + str(int(x) * int(y)))
else :
print('\nPay: ' +str( 40 * int(y) + ((int(x) - 40) * 15)))
computepay()

Do like this and let me know if that works out for you Dale
def computepay():
x = int(input('Enter Hours: ')) #this is the way to get int input
y = int(input('Enter Rate: '))
if x <= 40 :
basicCompute = x * y
#use f-string instead, it is clean and correct, the way you were doing things with the print was wrong
print(f'\nPay: {basicCompute}')
else :
# have a variable which computes that for you, don't do that in your print directly
compute = (40 * y) + ((x - 40) * 15)
print(f'\nPay: {compute}')
# this is how you call your method
computepay()
OUTPUT
# this is how you get the thing in your terminal, tested and verified
Enter Hours: 30
Enter Rate: 10
Pay: 300
ALTERNATIVE
If you want your method to accept the two arguments, then do not do input() inside your method
def computepay(x, y):
# use x and ye with the arguments only
if x <= 40 :
basicCompute = x * y
print(f'\nPay: {basicCompute}')
else :
compute = (40 * y) + ((x - 40) * 15)
print(f'\nPay: {compute}')
# When you call your method, before that you have to accept x and y params, then
# pass it to your method
hours = int(input('Enter Hours: '))
rate = int(input('Enter Rate: '))
# then pass it to your method
# this will do the call, and print the data in your console
computepay(hours, rate)
Hope that helps :)

You need to call that function below.
computepay(parameterA,parameterB)
Mind the indentation issues!

Related

How to divide a variable with the number of inputs

I have a variable s and i want to divide that variable with the number of inputs there are. I am a beginner, can anyone tell me the code that can perform that action?
I haven't tried anything because i don't know the specific code that can perform that action.
x = int(input("Insert x: "))
y = int(input("Insert y: "))
s = x + y
print(s/number of inputs)
I expect the output of s/number of inputs.
Not sure why this is needed but you can define your own function that inputs and increments a global counter:
number_of_inputs = 0
def my_input(msg):
global number_of_inputs
x = input(msg)
number_of_inputs += 1
return x
x = int(my_input("Insert x: "))
y = int(my_input("Insert y: "))
s = x + y
print(s / number_of_inputs)
Input:
Insert x: 10
Insert y: 20
Output:
15.0

When approximating the value of pi with the leibniz formula

My question is: Why do these two programs provide different answers! Thank you in advance!
I created two programs that find the value of pi as close as possible based off of the user input for the total sum. However, I created two programs one that steps by 2 and one that steps by 4. I was wondering why the answers provided by the two codes are different.
Code that uses 2 step.
import math
total = 0
def main():
#input
n = int(input("How many numbers are we going to process: "))
#process
#initialize total
total = 0
#create the loop that runs via amount input
for i in range(0, n , 2):
total = total + ( 1 / ((i * 2) + 1)) - (1 / ((i * 2) + 3))
amount = total * 4
print(amount)
print("In comparison to pi the value is: ", (math.pi - amount))
main()
Code that uses 4 step
import math
total = 0
def main():
#input
n = int(input("How many numbers are we going to process: "))
#process
#initialize total
total = 0
#create the loop that runs via amount input
for i in range(0, n , 4):
total = total + ( 1 / ((i * 2) + 1)) - (1 / ((i * 2) + 3)) + (1/
((i * 2) + 5)) - (1/ ((i * 2) + 7))
amount = total * 4
print(amount)
print("In comparison to pi the value is: ", (math.pi - amount))
main()

how do I use the numbers from the list, to do the math equation in last 5 lines of code?

lope = []
def main():
print('This program is going to do 5 calculations for growth rates on FCF.')
growth = .10
freecash = int(input('What is initial free cash flow'))
for i in range(5):
freecash = freecash * (1+growth)
lope.append(freecash)
equation = freecash * (1+.03)
print('The value in of Free cash in 5 years is: ',equation)
# this next part is for calculating the terminal
print('This Calculates the terminal for you.')
bwacc = float(input('Whats the WACC: '))
# ltg == Long Term Growth
ltg = .03
equation1 = equation * (1+ltg)/(bwacc-ltg)
print('The Terminal Value is: ',equation1)
one = lope[0] / (1+bwacc) ** 1
two = lope[1] / (1+bwacc) ** 2
three = lope[2] / (1+bwacc) ** 3
four = lope[3] / (1+bwacc) ** 4
five = lope[4] / (1+bwacc) ** 5
print('The pv of perpetuity is: ',one, two,three,four,five)
This code is meant to calculate these 5 equations in the last 5 lines of code, I'm not really understanding how this isn't working when I put print statements.
The best way would be to use a loop:
list4numbers = []
for i in range(5):
a = lope[i] / (1+bwacc) ** i+1
list4numbers.append(a)
With this you could do what ever you needed with the list. Hope I helped!
-Zeus

Python "int object is not callable"

TypeError: 'int' object is not callable
Anyone knows how can I fix this error? I know error is somewhere in the equation in the total. Thanks
decimal = 0
rate = 0
principal = 0
years = 0
def simple(p, r, n,):
decimal = r / 100
print("Principal: " + str(p))
print("Rate: " + str(decimal))
print("Number of Years: " + str(n))
total = p (1 + (decimal * n))
print("Total: " + str(total))
def main():
principal = int(input("Enter Principal: "))
rate = float(input("Enter Rate: "))
years = int(input("Enter Numbers of Years: "))
simple(principal, rate, years)
main()
print("End of Program")
Here p is an integer that you try to call :
total = p (1 + (decimal * n))
I think that you want :
total = p*(1 + (decimal * n))
On this line p is expected to be a function because it is immediately followed by a parenthesis:
total = p (1 + (decimal * n))
But p is passed as a parameter above, so I'm guessing you are passing an integer. If you mean to multiply:
total = p * (1 + (decimal * n))
You should define p first simple(int p,int r,int t) then total=p*(1+decim

Celsius to Fahrenheit loop in list

I'm trying to create a list of Celsius to Fahrenheit temperature conversions from celsius range 0-100 in increments of 0.5. This is what I have so far but I can't seem to get the loop to run correctly because it starts with celsius:0 fahrenheit:0; I need it to start with celsius:0 fahrenheit:32 (the correct conversion).
count = 0
celsius = 0
while (celsius <= 100):
print ('Celsius:', celsius, 'Fahrenheit:', count)
celsius = celsius + 0.5
count = (((celsius)*9/5)+32)
I think what you're looking for is more something like this:
celsius = 0
while celsius <= 100:
fahrenheit = celsius * 9.0/5.0 + 32
print ('Celsius:', celsius, 'Fahrenheit:', fahrenheit)
celsius += 0.5
Why don't you write a function?
def toFarenheit(celsius):
return (9.0/5.0) * celsius + 32
def toCelsius(farenheit):
return (farenheit - 32) * (5.0 / 9.0)
# I don't actually use this method, but it's still good to have
Then, you can do:
for y in range(0,200):
x = y / 2.0
print("Celsius: ", x, ", Farenheit: ", toFarenheit(x))

Resources