How do I assign a value to a word? - string

I'm creating a calculating list for the user to select from such as: pepsi, sprite, coke
and after user selects their fav drink, it will calculate: the calories(they all have diff. calories) + plus how many they drink per week + per year and then Print how many total calories they consume per year.
I got the math part down but I can't figure out how to add the "drink" they select into the final output.
EXAMPLE:
pepsi 100
sprite 200
coke 300
So, if they select pepsi, it should then take variable pepsi, then add how many per day + week + year then print total calories for year. Make sense?
right now it just prints the same number for any selection. when clearly the calories will vary.
Here is how I tried coding it:
cans_per_day = int(input("\nHow many cans per day?\n"))
sodas_per_week = int(input("How many cans do you drink per week?\n"))
drink_of_choice = input("What's your drink of choice?\n\n" + "Pepsi\nWild Cherry Coke\nOrange Fanta\nMountain Dew\nMello Yello\nSprite\n\n")
drink_of_choice = input + 41 * cans_per_day * sodas_per_week * 4 * 12 / (453.592)
print(drink_of_choice)

You can just check which drink is selected with something like this (if you use python):
if drink == "pepsi":
calories = 100
elif drink == "sprite":
calories = 200
elif drink == "coke":
calories = 300
then you can just multiply with calories in your calculation

Related

Correct Change python program

I am supposed to
Design and write a Python program that gives exact change for an item purchased with $5 or less. Use a float data type to represent money (You may want to multiply this number by 100 so that you get an integer for your calculation, such that $5.00 represents 500 cents, $2.35 represents 235 cents, etc.)
Below is my code:
amount = float(input('Enter item cost: '))
tender = float(input('Enter amount tendered: '))
change = int((tender - amount)*100)
dollars = change // 100
change = change % 100
quarters = change // 25
change = change % 25
dimes = change // 10
change = change % 10
nickels = change // 5
change = change % 5
pennies = change
#output
print('Dollar bills:', dollars)
print('Quarters:', quarters)
print('Dimes:', dimes)
print('Nickels:', nickels)
print('Pennies:', pennies)
An expected output for an item that costs $2.71 when the amount tendered is $5.00, would be:
Dollars: 2
Quarters: 0
Dimes: 2
Nickels:1
Pennies:4
but my output is giving me:
Enter item cost: 2.71
Enter amount tendered: 5.00
Dollars: 2
Quarters: 1
Dimes: 0
Nickels: 0
Pennies: 4
What am I doing wrong? Any help would be appreciated
Thanks!

Why I am getting StatisticsError: no unique mode; found 2 equally common values while creating a pivot table?

Suppose I have this (randomic) df_bnb:
neighbourhood room_type price minimum_nights
0 Allen Pvt room 38 5
1 Arder Entire home/apt 90 2
2 Arrochar Entire home/apt 90 2
3 Belmont Shared Room 15 1
4 City Island Entire home/apt 100 3
Every row represents an Airbnb's booking.
I hope to generate a pivot_table in which Index is the column neighbourhood and columns are others data frame columns ['room_type', 'price', 'minimun_nights'].
I want entries of the abovementioned columns as mean, expect for room_type where I wish to have the mode. Like the following dataframe's example:
room_type price minimum_nights
Allen room type mode for Allen price mean for Allen mean min nights for Allen
Arder room type mode for Arder price mean for Arder mean min nights for Arder
Arrochar room type mode for Arrochar price mean for Arrochar mean of min nights for Arrochar
Belmont room type mode for Belmont price mean for Belmont mean of min nights for Belmont
City Island room type mode for City Island price mean fot City Is. mean of min nights for City Island
This is the code I try so far:
bnb_pivot = pd.pivot_table(bnb,
index = ['neighborhood'],
values = ['room_type', 'price',
'minimum_nights','number_of_reviews'],
aggfunc = {'room_type': statistics.mode,
'price' : np.mean,
'minimum_nights': np.mean,
'number_of_reviews': np.mean})
This is the error that I am getting:
StatisticsError: no unique mode; found 2 equally common values
I try to search for other sources, but I don't how to treat statistic.mode() while creating a pivot_table.
Many thanks in advance for any helpful indication!

Excel formula for reverse percentage calculation

I have a calculation which I am unable to crack.
Lets say my Cost Price is 300. I want to sell the item at No Profit or No Loss. My total commission/expenses will be 30%. So it means i need to sell the item at 390.
But if I do 390 - 30% = 273.
How can I see the item, so that if I minus 30% to it. My Revenue will still be 300.
the formula you want is
=300/0.7
or
=300/(1-30%)
basically it is 300= x*(1-.30) where the (1-.30) is the amount that wants to be kept after the commision of 30%. Solving for x we get the above formula.
You want Sell Price - 30% Sell Price = Cost Price.
Combining the left two, you have 70% Sell Price = Cost Price.
Divide both sides by 70% and you get Sell Price = (1/0.7) Cost Price.
The fraction 1/0.7 is approximately 1.42857.
I was looking for a similar equation/formula, but then I built it in Excel.
So if you have, say, an item with
CostPrice = 200
Then you add a ProfitMargin = 20% so the
SellPrice = 200 + 20% = 240
Then the reverse equation for this will be
CostPrice = ( SellingPrice * 100 ) / ( ProfitMargin + 100 )
// In your case it should be
CostPrice = ( 240 * 100 ) / ( 20 + 100 )
= 200
Or Simply do the following:
CostPrice = SellingPrice / 1.2 = 240 / 1.2 = 200 <-- This will reduce the added 20%
I was looking for the same thing. None of the people here seemed to understand exactly what you were going for.
Here is the formula I used to get 300 as the answer.
=390/(1+30%)

Python loop statements

I have been working on a program for my homework:
Write a program for a car salesperson who works a five day week. The program should prompt for how many cars were sold on each day and then prompt for the selling price of each car (if any) on that day. After the data for all five days have been entered, the program should report the total number of cars sold and the total sales for the period. See example output. NOTE: duplicate the currency format shown for the total sales,
Example Output
How many cars were sold on day 1? 1
Selling price of car 1? 30000
How many cars were sold on day 2? 2
Selling price of car 1? 35000
Selling price of car 2? 45000
How many cars were sold on day 3? 0
How many cars were sold on day 4? 1
Selling price of car 1? 30000
How many cars were sold on day 5? 0
You sold 4 cars for total sales of $140,000.00
I do have some code that I have worked on but I am stuck. I can figure out how to get the program to prompt the user for how many cars were sold on day 2 and so on. Any help would be appreciated!
Here is my code, I am also taking a basic python course so I am new to this!!
def main () :
cars_sold = []
num_days = int(input('How many days do you have sales?'))
for count in range(1, num_days + 1):
cars = int(input('How many cars were sold on day?' + \
str(count) + ' '))
while (cars != cars_sold):
for count in range(1, cars + 1):
cars_sold = int(input('Selling price of car' ' ' + \
str(count) + ' '))
main ()
For this you would want to use nested for loop to prompt for each car entered.
def main():
cars_sold = 0
total = 0
num_days = int(input('How many days do you have sales? '))
# for each day
for i in range(1, num_days + 1):
num_cars = int(input('How many cars were sold on day {0}? '.format(i)))
cars_sold += num_cars
# for each car of each day
for j in range(1, num_cars + 1):
price = int(input('Selling price of car {0}? '.format(j)))
total += price
# Output number of cars and total sales with $ and comma format to 2 decimal places
print('You sold {0} cars for total sales of ${1:,.2f}'.format(cars_sold, total))
# Output
>>> main()
How many days do you have sales? 5
How many cars were sold on day 1? 1
Selling price of car 1? 30000
How many cars were sold on day 2? 2
Selling price of car 1? 35000
Selling price of car 2? 45000
How many cars were sold on day 3? 0
How many cars were sold on day 4? 1
Selling price of car 1? 30000
How many cars were sold on day 5? 0
You sold 4 cars for total sales of $140,000.00

Python doing calculations over a list and adding into the list

I'm trying to run a model in Python that will apply certain calculations to 3 items in a list and then keep applying those calculations for a set number of iterations.
The code below is what I've got so far. Do I need to use something like modulo or index() to the keep the if/elif keep growing? Or would I be better feeding into a function?
# Nonsense Model for Fish numbers ###
''' Fish numbers, in 100,000 tonnes in coastal waters. Those in the North Sea
increase by 10% every year.
Those in the Irish Sea increase by 15%. Those in the Channel are currently decreasing
the difference by 5%. Also want a total for each year for 10 year prediction.'''
#master list
fish = []
#ask questions
northFish = int(input('How many fish in first year the North Sea?'))
irishFish = int(input('How many fish in first year the Irish Sea?'))
channelFish = int(input('How many fish in first year in The Channel?'))
# add to the list
fish.extend((northFish,irishFish,channelFish))
# display the start
print (fish)
year = int(input('how many years would you like to model?'))
#run the model
for i in range(1,year):
newFish = []
if i == 0:
n = fish[0] * 1.1
newFish.append(n)
elif i == 1:
ir = fish[1] * 1.15
newFish.append(ir)
elif i == 2:
c = fish[2] * 0.95
newFish.append(c)
fish.extend(newFish) # add generated list to the master list
#total = n + i + c
#print('The total for year', i, 'is', total)
print(fish)
EDIT: this is how you can also save the trend for every year. The list fish contains the population of the current year and the list of lists fish_trend contains the fish population of every year. for example fish_trend[5][1] would return the irish fish population after 5 years.
# Nonsense Model for Fish numbers ###
''' Fish numbers, in 100,000 tonnes in coastal waters. Those in the North Sea
increase by 10% every year.
Those in the Irish Sea increase by 15%. Those in the Channel are currently decreasing
the difference by 5%. Also want a total for each year for 10 year prediction.'''
# master list
fish = []
# ask questions
northFish = int(input('How many fish in first year the North Sea?'))
irishFish = int(input('How many fish in first year the Irish Sea?'))
channelFish = int(input('How many fish in first year in The Channel?'))
# add to the list
fish = [northFish, irishFish, channelFish]
# display the start
print(fish)
year = int(input('how many years would you like to model?'))
# run the model
factors = (1.1, 1.15, 0.95)
# initialize fish_trend with population of first year
fish_trend = [tuple(fish)]
# iterate through years
for _ in range(year):
# iterate through fish types
for i in range(len(fish)):
fish[i] *= factors[i]
# append current population to fish_trend
fish_trend.append(tuple(fish))
# you can use pprint for better visualization
import pprint
pprint.pprint(fish_trend)
# if you want the sum of the population per year, you can do that easily by using this:
sum_per_year = [sum(f) for f in fish_trend]
print(sum_per_year)

Resources