So, I'm kind of new to programming and have been trying Python. I'm doing a really simple program that converts usd to euroes.
This is the text of the problem that I'm trying to solve
You are going to travel to France. You will need to convert dollars to euros (the
currency of the European Union). There are two currency exchange booths. Each has
a display that shows CR: their conversion rate as euros per dollar and their fee as a
percentage. The fee is taken before your money is converted. Which booth will give
you the most euros for your dollars, how many euros, and how much is the difference.
Example 1:
Dollars: 200
CR1: 0.78
Fee: 1 (amount 152.88 euros)
CR2: 0.80
Fee: 3 (amount 155.2 euros)
Answer: 2 is the best; difference is 2.32 euros; 155.2 euros
And here is my code
from __future__ import division
usd = int(input("How much in USD? "))
cr1 = int(input("What is the convertion rate of the first one? "))
fee1 = int(input("What is the fee of the first one? "))
cr2 = int(input("What is the convertion rate of the second one? "))
fee2 = int(input("What is the fee of the second one? "))
def convertion (usd, cr, fee):
usdwfee = usd - fee
convert = usdwfee * cr
return convert
first = convertion(usd, cr1, fee1)
second = convertion(usd, cr2, fee2)
fs = first - second
sf = second - first
def ifstatements (first,second,fs,sf):
if first < second:
print "1 is the best; difference is ",fs," euroes. 2 converts to ",first," euroes."
elif first > second:
print "2 is the best; difference is",sf," euroes. 2 converts to", second," euroes."
ifstatements(first, second, fs, sf)
The problem is that when I run the program it won't print out. It just takes my input and doesn't output anything.
Check your logic more.
cr1 = int(input("What is the convertion rate of the first one? "))
Your conversion rate is in int. As in Integer which means it can't have a floating point (a decimal "CR1: 0.78" from your example). Your cr1 will become 0 if you cast it into an int. Also change your dollar and fees to accept floats too since I'm assuming you want to deal with cents too
So change:
usd = float(input("How much in USD? "))
cr1 = float(input("What is the convertion rate of the first one? "))
fee1 = float(input("What is the fee of the first one? "))
cr2 = float(input("What is the convertion rate of the second one? "))
fee2 = float(input("What is the fee of the second one? "))
And it should work.
Related
I just completed my pre-course assessment.
The project was:
A cupcake costs 6.50. If you purchase more than 6 cupcakes, each of them will cost 5.50 instead, and if you purchase more than 12, each of them will cost 5.00 instead.
Write a program that asks the user how many cupcakes they want to purchase, and display the total amount they have to pay.
My inputs have been:
amt=input("How many cupcakes would you like to but? ")
if amt>=6:
disc = amt*0.12
elif amt>=12:
disc=amt*0.24
print(disc)
I can't seem to get the code running. Can anyone please dump it down for me?
THE COMPLETE SOLUTION TO YOUR PROBLEM IS:
amt=int(input("How many cupcakes would you like to but? "))
if amt <= 6:
pancake_price_650 = 6.50
price_inferior_6 = amt * pancake_price_650
print(price_inferior_6)
elif amt > 6:
pancake_price_550 = 5.50
price_superior_6 = amt * pancake_price_550
print(price_superior_6)
elif amt > 12:
pancake_price_500 = 5.00
price_superior_12 = amt * pancake_price_500
print(price_superior_12)
You were wrong in constructing the logic of conditions.
Also, you haven't converted the input to a string. Input is always read as a string, in your code you haven't read it as an integer. Convert string input to int using int(), because int() transforms a string into an integer. You need to add int before input, but put it in parentheses.
Python takes input in string format, you need to cast the input into int but in your code, you're comparing a string with int in the conditionals.
amt=int(input("How many cupcakes would you like to but? "))
if amt>=6:
disc = amt*0.12
elif amt>=12:
disc=amt*0.24
print(disc)
I am new to Python and a student, my college has chosen the worst book on earth for our course. I cannot find examples of any concepts, so I apologize in advance as I know these concepts are very basic. I hope you can help me.
I need to know how to use the round feature in a string. I find examples but they do not show the string, just simple numbers.
Here is what we are supposed to get as an output:
Enter the gross income: 12345.67
Enter the number of dependents: 1
The income tax is $-130.87 <---- this is what we are supposed to figure out
Here is the coding we are given to alter:
TAX_RATE = 0.20
STANDARD_DEDUCTION = 10000.0
DEPENDENT_DEDUCTION = 3000.0
# Request the inputs
grossIncome = float(input("Enter the gross income: "))
numDependents = int(input("Enter the number of dependents: "))
# Compute the income tax
taxableIncome = grossIncome - STANDARD_DEDUCTION - \
DEPENDENT_DEDUCTION * numDependents
incomeTax = taxableIncome * TAX_RATE
# Display the income tax
print("The income tax is $" + str(incomeTax))
As I do not have an NUMBER to plug into the formula - I have to figure out how to use "incomeTax" - I have no idea how to do this. THe book doesnt explain it. Help?
You can use format strings:
print("The income tax is ${:.2f}".format(incomeTax))
If you are using Python 3.6+, you can also use f-strings:
print(f"The income tax is ${incomeTax:.2f}")
You can round just before making it a string:
print("The income tax is $" + str(round(incomeTax,2)))
Output:
Enter the gross income: 12345.67
Enter the number of dependents: 1
The income tax is $-130.87
Im a student as well, with the same dumb book and HW.
I tried the above
str(round(incomeTax,2))
and it didn’t work. Maybe I typed something wrong. After playing around I found this to work
# Display the income tax
incomeTax = round(incomeTax,2)
print(“The income tax is $” + str(incomeTax))
I hope this helps some other poor soul searching the web for an answer!
I'm in an intro programming class and am lost. We've had several labs that required knowledge that we haven't been taught but I've managed to find out what I need on google (as nobody responds to the class message board) but this one has me pretty frustrated. I'll include a pastebin link here: https://pastebin.com/6JBD6NNA
`principal = input()
print('Enter the Principal Value of your investment: $', float(principal))
time = input()
print('\nEnter the Time(in years) you plan to save your investment: ', int(time))
rate = input()
print('\nEnter the Rate (2% = 0.02) you will collect on your investment: ', float(rate))
interest = (float(principal) * float(rate)) * int(time)
final_value = float(principal) + float(interest)
print('\nThe Final Value of your investment will be: $%.2f' % final_value)`
So I need the output of the dollar amounts to have a comma ($27,500.00) but I have no idea how to do this. I've seen a couple of solutions on this site and others but I can't get them to work. PLEASE can someone help me?
In Python 2.7 or above, you can use
print('The Final Value of your investment will be: ${:,.2f}'.format(final_value))
This is documented in PEP 378.
Source: Python Add Comma Into Number String
Here is a working example:
principal = float(input('Enter the Principal Value of your investment: $'))
time = int(input('\nEnter the Time(in years) you plan to save your investment: '))
rate = float(input('\nEnter the Rate (2% = 0.02) you will collect on your investment: '))
interest = principal * rate * time
final_value = principal + interest
print('The Final Value of your investment will be: ${:,.2f}'.format(final_value))
Your last line should be:
print ("\nThe Final Value of your investment will be: ${:,.2f}".format(final_value))
So far I have been coding this all week trying to get it to work.
It should come out as this:
Please enter Amount that you would like to borrow(£): 4000
Please enter Duration of the loan(Years):2
Please enter the interest rate (%):6
The total amount of interest for 2 (years) is: £480.00
The total amount of the period for 2 (years) is £4480.00
You will pay £186.67 per month for 24 months.
Do you wish to calculate a new loan payment(Y or N)
Code:
monthlypayment = 0 #Variable
loanamount = 0 #Variable
interestrate = 0 #Variable
numberofpayments = 0 #Variable
loandurationinyears = 0 #Variable
loanamount = input("Please enter the amount that you would like to borrow(£) ")
loandurationinyears = input("How many years will it take you to pay off the loan? ")
interestrate = input("What is the interest rate on the loan? ")
#Convert the strings into floating numbers
loandurationinyears = float(loandurationinyears)
loanamount = float(loanamount)
interestrate = float(interestrate)
#Since payments are once per month, number of payments is number of years for the loan
numberofpayments = loandurationinyears*12
#calculate the monthly payment based on the formula
monthlypayment = (loanamount * interestrate * (1+ interestrate)
* numberofpayments / ((1 + interestrate) * numberofpayments -1))
#Result to the program
print("Your monthly payment will be " + str(monthlypayment))
looks to me like the only thing you're missing in the code above from what you described is a while loop. This will allow your program to calculate the loan and run the program over and over again until the user inputs no, in which case the program exits. all what you have to do is:
YorNo = input("Do you wish to calculate a loan payment")
YorNo.Title()
while YorNo != "n":
#Your code goes here
YorNo = input("Do you wish to calculate a loan payment")
YorNo.Title()
print("Thank you for using the program")
In case you dont understand this, baisically, you type the first 3 lines just before your code. Then you leave an indentation and type your code after them. Once your done, you type in the 3rd and 4th line. Then, simply go back that indentation (to show the program that this isnt part of the loop.) If im not mistaken, the result of this will be:
You will be asked wether you want to calculate a loan
If you answer "y" your code will run and the loan will be calculated and printed to the user
Then you will be asked again. The above will repeat until you input "n"
the N cant be capitalised
I am typing a program for my class, the problem is worded very odd, but I have put in my code for the problem, am I declaring the numbers as float correctly?
Simple question but I'm keeping my mind open to other ways of doing things.
print " This program will calculate the unit price (price per oz) of store items,"
print " you will input the weight in lbs and oz, the name and cost."
item_name = (input("Please enter the name of the item. "))
item_lb_price = float(input("Please enter the price per pound of your item "))
item_lbs = float(input("please enter the pounds of your item. "))
item_oz = float(input("plese enter the ounces of your item. "))
unit_price = item_lb_price / 16
total_price = item_lb_price * (item_lbs + item_oz / 16)
print "the total price per oz is", unit_price
print "the total price for", item_name, "is a total of", total_price
Your floats are fine.
You need to use raw_input though to get a string back.
input("Enter a number") is equivalent to eval(raw_input("Enter a number")). Currently the code tries to evaluate the input as code (run as a python expression).
15:10:21 ~$ python so18967752.py
This program will calculate the unit price (price per oz) of store items,
you will input the weight in lbs and oz, the name and cost.
Please enter the name of the item. Apple
Traceback (most recent call last):
File "so18967752.py", line 6, in <module>
item_name = (input("Please enter the name of the item. "))
File "<string>", line 1, in <module>
NameError: name 'Apple' is not defined
Other comments:
For your multiline banner at the top, declare the string as a multiline string and print that.
Check to make sure the ranges make sense for the inputs (validate). Of course, if this is for a class you may not have reached this point (Seen if/else?)
Be explicit with your constants to make them floats to make sure it doesn't default to integer division
Slightly cleaned up form:
banner = '''
This program will calculate the unit price (price per oz) of store items.
You will input the weight in lbs and oz, the name and cost.
'''
print banner
item_name = raw_input("Please enter the name of the item. ")
item_lb_price = float(raw_input("Please enter the price per pound of your item."))
item_lbs = float(raw_input("Please enter the pounds of your item."))
item_oz = float(raw_input("plese enter the ounces of your item."))
unit_price = item_lb_price / 16.0
total_price = item_lb_price * (item_lbs + (item_oz / 16.0))
print "The total price per oz is", unit_price
print "The total price for", item_name, "is a total of", total_price
if it is python 2 you should be using raw_input instead of input.
if it is python 3 you should be enclosing the values you want to print in parentheses.
yes you are using the float function correctly but are not verifying that the input is correct. it is liable and even likely to throw errors.
besides the program is not correct for python 2 because of the input() and not correct for python 3 because of the use of the print statement.
And you should devide by a float: 16.0 instead 16.
Division in python is per default using integers, so if you want a floating point result you should divide by 16.0
unit_price = item_lb_price / 16.0