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)
Related
I am trying to calculate the cost each person will have to pay daily on a trip. I can get the prompts to work, but not the math. I have no idea on how to do the math with the lists. Anyone got any ideas?
Here is what I have so far:
def main():
morepeople = True
NumPPL = list()
Days = list()
Gas = list()
Food = list ()
Nopeople = 0
while(morepeople):
Numppl = (input('Enter the amount of people traveling: '))
if(Numppl == Nopeople):
morepeople = False
else:
NumPPL.append(Numppl)
Days.append(input('Enter the amount of predicted days traveling: '))
Gas.append(input('Enter gas cost per day: '))
Food.append(input('Enter food cost per day: '))
break
Cost = sum(Gas) and sum(Food)/ sum(NumPPL)
print('Everyones daily share is: ', Cost)
main()
welcome to these boards.
This is a curious approach and interesting question.
Couple things to help you...
1> Input() passes a string regardless of the value actually input. You should convert it to an integer or float before using it in a mathematical operation.
2> As dea87 called out, aggregations on list objects require sum() or len() to get the total of values or count of entries, respectively.
I'm working with Python 3 on Visual Studio Code and the project I'm working on is to calculate the user's lunar weight after receiving their terrestrial weight, then output how their weight changes overtime given an increase and a period of time.
This is the error message I've received multiple times, no matter how many times I've re-code:
TypeError: can't multiply sequence by non-int of type 'float'
This specifically happens whenever I use the input() and/or sys.stdin.readline() functions, but all of the pertaining variables are integers or floats, whether or not I try to convert them to floats by using the float() function around my inputs.
Here is my code:
# this programs converts your terra weight to your lunar weight, then it takes increase over a period of time
# error whether I use float() or not
terraWeight = float(input("Terra Weight: "))
ask = input("Is this in LBS or KG? ")
# converts pounds to kilograms
# 2.204... is used to convert lbs to kgs
if ask == "lbs" or "LBS":
initial = terraWeight / 2.2046223302272
# didn't need this, but assignment error popped up
x = 0
# or replace input() with sys.stdin.readline()
increase = input("Increase: ")
period = input("Period: ")
# gets lunar weight over time
def lunarWeight():
global increase
global period
global x
global initial
print("Earth Weight = %s kgs." % terraWeight)
year = 0
lunarWeight = initial * 0.165
print("Moon Weight = %s kgs. \n" % lunarWeight)
postIncrease = lunarWeight * increase
for x in range(0, period):
year += 1
lunarWeight += postIncrease
print("Year %s = %s kgs." % (year, lunarWeight))
lunarWeight()
The terminal directs to the this section of my code:
postIncrease = lunarWeight * increase
Which is on line 28. I'm not sure what the problem is, and I tried debugging but I still get the same error messages. I saw other posts with the same problem but they had problems using lists, which I'm pretty sure I'm not using. Any advice please?
Screenshot
I think you should write this lines as:
increase = float(input("Increase: "))
period = int(input("Period: "))
period is used in range() so it should be integer
I am getting a value error when I try to enter 10.8 as hours. Why can't I enter a float value and convert it into int here?
def computepay(hours,rate):
try:
hours=int(hours)
rate=float(rate)
if hours > 40:
payment = 40 * rate # Standard Payment until 40 Hours
payment = payment + rate *(hours-40) * 1.5 # + the rest which has more rate
print("Pay:",payment)
else:
payment=hours*rate
print("Pay:", payment) # Otherwise Normal Payment
except:
print("Value error")
hours=input("Enter hours:")
rate=input("Enter Rate:")
computepay(hours,rate)
It's simply because int() cannot convert strings that do not represent integers. As input returns as string, I suggest you pass it to float() first.
So : hours = int(float(hours)).
I couldn't make much sense of the code but it appears that you are passing string values to the method directly. input() returns a string. In your case putting in 10.8 would produce a
ValueError, invalid literal with base 10.
What you should be doing is casting your input into a float and then passing it to your function.
Do something like this.
hours = float(input("Enter hours:"))
rate = float(input("Enter Rate:"))
computepay(hours,rate)
So I have a homework on coding a bank ATM machine and I have done most of the code but I do not seem to make work like the professor stated "If the user enters an invalid numeric value, inform the user as such. If the user enters a valid number between 0 and 100, display the amount of funds left in the account"
example:
kitten : "Invalid Entry".
20.5 : "Your account has $79.50 remaining"
My code so far:
amt = int(input("Withdraw amount: "))
if amt <= 0:
print("Invalid Amount")
else:
print{"Invalid Entry")
if amt > 100:
print("Not enough funds in account")
if (amt >=0 and amt <=100):
print("Your account has ${0:1.2f} remaining." .format(100-amt))
My issue is that when I enter 20.5 it give me an error
"ValueError: invalid literal for int() with base 10: '20.5'"
The second issue is when I enter a string "ValueError: invalid literal for int() with base 10: 'kitten'"
The reason why entering 20.5 gives you an error is because in the amt variable, it is looking for an integer to be inputted. In python, any number with a decimal is considered a float.
The code for the amt variable is currently
amt = int(input("Withdraw amount: "))
For the amt variable to have a number with a decimal as acceptable input, the amt variable should be this:
amt = float(input("Withdraw amount: "))
You are converting everything to an int, so putting in strings and doubles will give you errors. You need to check the content of your input before blindly converting it to an int.
You need to think about what your code is actually doing instead of just blindly changing things until they work without understanding why.
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.