# user input
num = int(input("Please enter engine size: "))
# calculations
if num <= 1000:
print("The motor tax for your vehicle is €150")
elif num >= 1001 <= 1200:
print("The motor tax for your vehicle is €175")
elif num >= 1201 <= 1400:
print("The motor tax for your vehicle is €200")
elif num >= 1401 <= 1600:
print("The motor tax for your vehicle is €250")
elif num >= 1601 <= 1800:
print("The motor tax for your vehicle is €300")
elif num >= 1801 <= 2000:
print("The motor tax for your vehicle is €350")
else:
print("The motor tax for your vehicle is €500")
I know I've probably made a stupid mistake here, I'm just hoping someone could point me in the right direction.
I'm trying to get python to print the relevant amount for each of the specified engine size.
Each time I run it with any amount greater than 1000 it will only give me the output of €175.
Thanks so much in advance!
This isn't doing what you want or expect it to be doing:
elif num >= 1001 <= 1200:
You should replace it with something like:
elif num in range(1001, 1201):
NOTE: to check <=, you need to have upper bound of range incremented by 1!
Otherwise, you could write what you originally had as:
elif 1001 <= num <= 1200:
Welcome to Stack Overflow!
i have updated the answer to PL200 to provide an answer to your question. This is a suggestion to improve a bit your code:
# Motor tax is based on ranges
if num <= 1000:
tax = 150
elif num <= 1200:
tax = 175
elif num <= 1400:
tax = 200
elif num <= 1600:
tax = 250
elif num <= 1800:
tax = 300
elif num <= 2000:
tax = 350
else:
tax = 500
print("The motor tax for your vehicle is €{}".format(tax))
You actually don't need 2 inequations because of the if / elif chain.
Or you could do even better to avoid a long if/elif:
def get_tax(num):
"""Return the vehicle tax based on ranges"""
# Ranges are stored as "maximum size" / "tax"
tax_ranges = (
(1000, 150),
(1200, 175),
(1400, 200),
(1600, 250),
(1800, 300),
(2000, 350),
)
default_tax = 500
for max_size, tax in tax_ranges:
if num <= max_size:
return tax
return default_tax
num = int(input("Please enter engine size: "))
print(f"The motor tax for your vehicle is {get_tax(num)}")
NOTE: I added a f-string at the end to print but it might not supported by your version of Python if you have a version earlier than 3.6. In that case, just replace {get_tax(num)} by "... {}".format(get_tax(num)) and remove the f before the string
Related
So I am new to python and am writing this for an assignment, For example if I run 101 as the input, it will run correctly and display that a I need "1 $100 Bill and 1 $1 Bill" but if I run that I need $125 back, it will execute as "1 $100 Bill and 1 $20 Bill" but not execute the remaining $5 bill. I also realized that I cannot run anything under 100 either but that I can fix. I am trying to understand the If/else statements
#
# Python ATM machine
#
import math
withdrawl = int(input("how much money do you want to withdrawl (max withdrawl is $200): "))
if (withdrawl > 200):
print("You are trying to withdraw too much money, this machine is kind of sucky, have a great day!!")
else:
print("1 100 Dollar Bill")
remainder1 = withdrawl - 100
if ((remainder1 / 20) >= 1):
print (math.trunc(remainder1/20)," 20 Dollar Bills")
else:
print("0 20 Dollar Bills")
remainder2 = remainder1 - (math.trunc(remainder1/20)*20)
if ((remainder2 / 10) >= 1):
print (math.trunc(remainder2/10)," 10 dollar Bills")
else:
print ("0 10 Dollar Bills")
remainder3 = remainder2 - (math.trunc(remainder2/10)*10)
if ((remainder3 / 5) >= 1):
print (math.trunc(remainder3 / 5)," 5 dollar Bills")
else:
print("0 5 dollar bills")
remainder4 = remainder3 - (math.trunc(remainder3/5)*5)
if ((remainder4 / 1) >= 1):
print (math.trunc(remainder3/1)," 1 dollar Bills")
else:
print("Thank you for using our ATM machine")
print ("Thank you for using our ATM machine")
Try to use while loop, like this:
withdraw = int(input("how much money do you want to withdraw (max withdraw is $200): "))
data = []
if (withdraw > 200):
print("You are trying to withdraw too much money, this machine is kind of sucky, have a great day!!")
else:
rest = withdraw
while rest > 0:
if rest > 100:
data.append(100)
rest = rest - 100
if 100 > rest >= 50:
data.append(50)
rest = rest - 50
if 50 > rest >= 20:
data.append(20)
rest = rest - 20
if 20 > rest >= 10:
data.append(10)
rest = rest - 10
if 10 > rest >= 5:
data.append(5)
rest = rest - 5
if 5 > rest > 0:
data.append(1)
rest = rest - 1
Output for 37:
[20, 10, 5, 1, 1]
import math
withdrawl = int(input("how much money do you want to withdrawl (max withdrawl is $200): "))
if (withdrawl > 200):
print("You are trying to withdraw too much money, this machine is kind of sucky, have a great day!!")
else:
if(withdrawl >= 100):
print("1 100 Dollar Bill")
remainder = withdrawl - 100
else:
remainder = withdrawl
if ((remainder / 20) >= 1):
print (math.trunc(remainder/20)," 20 Dollar Bills")
remainder = remainder - (math.trunc(remainder/20)*20)
else:
print("0 20 Dollar Bills")
if ((remainder / 10) >= 1):
print (math.trunc(remainder/10)," 10 dollar Bills")
remainder = remainder - (math.trunc(remainder/10)*10)
else:
print ("0 10 Dollar Bills")
if ((remainder / 5) >= 1):
print (math.trunc(remainder / 5)," 5 dollar Bills")
remainder = remainder - (math.trunc(remainder/5)*5)
else:
print("0 5 dollar bills")
if (remainder != 0):
print (remainder," 1 dollar Bills")
print ("Thank you for using our ATM machine")
So you've forgotten to update the value of the remainder + you don't have to use a different variable as a remainder just one is enough, this code should work for you.
And as #ArthurTacca said every if/else block happens regardless of what happened in the previous one.
I am not able access elif part :
total = input('What is the total amount for your online shopping?')
country = input('Shipping within the US or Canada?')
if country == "US":
if total <= "50":
print("Shipping Costs $6.00")
elif total <= "100":
print("Shipping Costs $9.00")
elif total <= "150":
print("Shipping Costs $12.00")
else:
print("FREE")
if country == "Canada":
if total <= "50":
print("Shipping Costs $8.00")
elif total <= "100":
print("Shipping Costs $12.00")
elif total <= "150":
print("Shipping Costs $15.00")
else:
print("FREE")
In fact you can use int("50") to convert "50" from string to integer 50.
You are comparing strings. Change your input to an integer with total = int(input(...)) and compare it to numbers instead of strings: if total <= 50: etc.
total = input('What is the total amount for your online shopping?')
python input defaults to string data type. you have to convert to int to compare
total = int(input('What is the total amount for your online shopping?'))
Updating the working code :
total = int(input('What is the total amount for your online shopping?'))
country = input('Shipping within the US or Canada?')
if country == "US":
if total <= 50:
print("Shipping Costs $6.00")
elif total <= 100:
print("Shipping Costs $9.00")
elif total <= 150:
print("Shipping Costs $12.00")
else:
print("FREE")
if country == "Canada":
if total <= 50:
print("Shipping Costs $8.00")
elif total <= 100:
print("Shipping Costs $12.00")
elif total <= 150:
print("Shipping Costs $15.00")
else:
print("FREE")
You are comparing strings when you want to compare integers, meaning you are comparing ASCII values instead.
See the following snippet:
total = "120"
if total <= "50":
print("yes")
Which prints yes when you obviously won't expect it. Convert your strings to integers.
I would like to charge a car under the condition that in the end it is charged at least 75% and if prices are positive I would like it to continue charging, but not surpass the maximum of 100%. The list of prices are the prices for everytime I charge (or decide not to charge) the car.
So here is what I have so far:
maxF = 100
minF = 75
SoC = 55
i = 0
chargeRate = 10
price = [2,-2,3,4]
while SoC < minF:
if price[i] > 0:
SoC += chargeRate
i += 1
# if there is no time left (no prices available), I have to charge in order to reach 75%
elif (minF - SoC)/chargeRate <= (len(price) - i):
SoC += chargeRate
i += 1
else:
i += 1
print(SoC)
In this piece of code the car charges prices 2 and 3, but not 4. I don't know how to include for the car to continue charging, after having passed the 75%.
I am greatful for any suggestions.
Many thanks,
Elena
From my understanding, you should try the following code:
while SoC <= 100:
if (i < len(price)) and (SoC < 75 or price[i] > 0):
SoC += chargeRate
else:
break
i+=1
print(SoC)
This should work for you. I'm not sure what you are trying to do with the line
elif (minF - SoC)/chargeRate <= (len(price) - i):
The problem is from the MIT edX Python Course 6.00.1 Problem Set 1 Part C. Here are the problems. Scroll to part C. I'm aware that there are countless questions asked about the edX course but none of them have really helped me. Whenever I run my bisection search code, nothing happens. No error message, nothing. Could someone help me find the issue in my code? Sorry if code is horribly inefficient, very new to python and programming.
#Python script for finding optimal saving rate of monthly salary in order to purchase 1M house in 36 months
salary = float(input("Enter salary: "))
total_cost = 1000000
salary_raise = 0.07 #semiannual raise rate
down = 0.25 * total_cost #downpayment
steps = 0
r = 0.04 #annual investments returns
low = 0 #low parameter for bisection search
high = 10000 #high parameter
current_savings = 0
while (current_savings <= (down - 100)) or (current_savings >= (down + 100)):
current_savings = 0
monthly_salary = salary/12
guess_raw = (high + low)/2
guess = guess_raw/10000.0
months = 0
steps += 1
while months < 36: #Finds end amount of money after 36 months based on guess
months += 1
multiple = months%6
monthly_savings = monthly_salary * guess
current_savings = current_savings + monthly_savings + current_savings*r/12
if multiple == 0:
monthly_salary += salary_raise * monthly_salary
if (current_savings >= (down - 100)) and (current_savings <= (down + 100)): #If the guess is close enough, print the rate and number of steps taken
print ("Best savings rate: ",guess)
print ("Steps in bisection search: ",steps)
break
elif current_savings < (down - 100): #If the guess is low, set the low bound to the guess
if guess == 9999:
print ("It is not possible to pay the down payment in three years.")
break
else:
low = guess
elif current_savings > (down + 100): #If the guess is high, set the high bound to the guess
high = guess
I'm very new to python so I apologize if this question is simple. I am trying to write an algorithm that calculates the difference in rates between 2017 and 2018 based on a user-inputted salary. I've gotten to the point where the algorithm does calculate a tax rate, however it seems to do it backwards, i.e. the lower the inputted income, the higher the tax owed, something that the government, for all its flaws, generally doesn't do. I've tried different things for the algorithm but I'm still not sure where I'm going wrong. Any ideas or advice would be greatly appreciated.
Thanks!
# of tax brackets
levels = 6
#2017 tax rates
rates2017 = [0, 10, 15, 25, 28, 33, 35]
#2018 tax rates
rates2018 = []
#2017 income tax thresholds
incomes2017 = [0, 9325, 37950, 91900, 191650, 416700, 418400]
# take in a value for net income and assign it to int
netincome = int(input('Please input an integer for income: '))
#initialize the variables used
tax_owed = 0
taxable_income = 0
netincomeleft = netincome - 6500
i = levels
#while loop calculates the income tax
while i >= 0:
taxable_income = netincomeleft - incomes2017[i]
tax_owed += taxable_income * (rates2017[i]/100)
netincomeleft = incomes2017[i]
i -= 1
#multiply tax owed by -1 to get a positive int for clarity
taxes_owed = tax_owed * -1
# print out the 2017 tax owed
print('tax owed on $', netincome, 'after standard deduction is ', taxes_owed)
*for the sake of clarity, I'm using Python 3 in a Jupyter notebook environment
You are working mostly with negative numbers....you don't check whether their income EXCEEDS the specific level, so with an income of 100 you charge them NEGATIVE tax rate of (418400 - 100) and so on.
You want to start your level at the first number EXCEEDING netincomeleft, and then not multiply by -1!
So for a small income "level" should start at 0 or 1, not at 6.
Edit: I did figure this out eventually. Turns out the government does not collect more tax than income, except for special people. Thanks everyone for your help!
levels = 6
rates2017 = [0, 10, 15, 25, 28, 33, 35]
rates2018 = []
incomes2017 = [0, 9325, 37950, 91900, 191650, 416700, 418400]
netincome = int(input('Please input an integer for income: '))
tax_owed = 0
taxable_income = 0
standard_deduction = 6500
netincomeleft = netincome - standard_deduction
i = 0
while levels >= 0 and taxable_income >=0 and netincomeleft >= 0:
if (netincomeleft - incomes2017[i]) < 0:
taxable_income = netincome - incomes2017[i-1] - standard_deduction
else:
taxable_income = netincomeleft - incomes2017[i]
tax_owed += (taxable_income * (rates2017[i]/100))
netincomeleft = netincomeleft - incomes2017[i]
i += 1
levels -= 1
taxes_owed = tax_owed
print('tax owed on $', netincome, 'after standard deduction is ', taxes_owed)
def USA():
tax=0
if salary<=9700:
tax=9700*0.1
elif salary<=39475:
tax=970+(salary-9700)*0.12
elif salary<=84200:
tax=4543+(salary-39475)*0.22
elif salary<=160725:
tax=14382.5+(salary-84200)*0.24
elif salary<=204100:
tax=32748.5+(salary-160725)*0.32
elif salary<=510300:
tax=139918.5+(salary-204100)*0.35
else:
tax=328729.87+(salary-510300)*0.37
return ('tax owed on $', salary, 'after standard deduction is ', tax, 'and your netincome is ', (salary-tax) )
salary=int(input('Please input an integer for income: '))
print(USA())