How do I take already calculated totals that are in a loop and add them together? - python-2.7

I created this program in Python 2.7.3
I did this in my Computer Science class. He assigned it in two parts. For the first part we had to create a program to calculate a monthly cell phone bill for five customers. The user inputs the number of texts, minutes, and data used. Additionaly, there are overage fees. $10 for every GB of data over the limit, $.4, per minute over the limit, and $.2 per text sent over the limit. 500 is the limit amount of text messages, 750 is the limit amount of minutes, and 2 GB is the limit amount of data for the plan.
For part 2 of the assignment. I have to calculate the total tax collected, total charges (each customer bill added together), total goverment fees collected, total customers who had overages etc.
Right now all I want help on is adding the customer bills all together. As I said earlier, when you run the program it prints the Total bill for 5 customers. I don't know how to assign those seperate totals to a variable, add them together, and then eventually print them as one big variable.
TotalBill = 0
monthly_charge = 69.99
data_plan = 30
minute = 0
tax = 1.08
govfees = 9.12
Finaltext = 0
Finalminute = 0
Finaldata = 0
Finaltax = 0
TotalCust_ovrtext = 0
TotalCust_ovrminute = 0
TotalCust_ovrdata = 0
TotalCharges = 0
for i in range (1,6):
print "Calculate your cell phone bill for this month"
text = input ("Enter texts sent/received ")
minute = input ("Enter minute's used ")
data = input ("Enter Data used ")
if data > 2:
data = (data-2)*10
TotalCust_ovrdata = TotalCust_ovrdata + 1
elif data <=2:
data = 0
if minute > 750:
minute = (minute-750)*.4
TotalCust_ovrminute = TotalCust_ovrminute + 1
elif minute <=750:
minute = 0
if text > 500:
text = (text-500)*.2
TotalCust_ovrtext = TotalCust_ovrtext + 1
elif text <=500:
text = 0
TotalBill = ((monthly_charge + data_plan + text + minute + data) * (tax)) + govfees
print ("Your Total Bill is... " + str(round(TotalBill,2)))
print "The toatal number of Customer's who went over their minute's usage limit is... " ,TotalCust_ovrminute
print "The total number of Customer's who went over their texting limit is... " ,TotalCust_ovrtext
print "The total number of Customer's who went over their data limit is... " ,TotalCust_ovrdata
Some of the variables created are not used in the program. Please overlook them.

As Preet suggested.
create another variable like TotalBill i.e.
AccumulatedBill = 0
Then at the end of your loop put.
AccumulatedBill += TotalBill
This will add each TotalBill to Accumulated. Then simply print out the result at the end.
print "Total for all customers is: %s" %(AccumulatedBill)
Note: you don't normally use uppercase on variables for the first letter of the word. Use either camelCase or underscore_separated.

Related

Adding elements in a row corresponding to a column

I'm extracting data from a CSV with 5 rows and 5 columns.
For example
print("Year Age Scholarship Academic Stipend")
print("1982 20 $20000.00 $1000.00")
print("1983 21 $25000.00 NA")
print("1984 22 $30000.00 $500.00")
print("1982 20 $16000.00 $200.00")
print("1983 21 $17500.00 $600.00")
I extracted individual lists with all these elements:
Year = [1982,1983,1984,1982,1983]
Age = [20,21,22,20,21]
Scholarship = [20000, 25000, 30000, 16000, 17500]
Stipend_Amount = [1000, NA, 500, 200, 600]
I want to group all my years together. How do I add the corresponding elements in column 4, corresponding only to the elements in Year?
For example. I want to be able to print
#Year Total_Scholarship_Granted
#1982 36000.00
But my for loop below is just adding all the elements together:
Start_Fund = 0
for i in range(len(year)):
Start_Fund += Scholarship[i]
print(year[i],Start_Fund)
#1982 108500
I want my results to be:
1982 36000
(which is acquired by adding all amounts from 1982)
You are missing an if statement, to check if the year in the loop is the year you want (in your case 1982). So your pseydocode should look like this:
Start_Fund = 0
my_year=1982
for i in range(len(year)):
if (year[i]==my_year)
Start_Fund += Scholarship[i]
print(my_year,Start_Fund)

C++ Data Parsing query

Hello guys i hope you could enlighten me with this issue i am facing!
Initial Output:
The sample text file is below and the format is as follow (Item Desc:Price:Quantity:Date).
STRAW:10:10:11NOV1991
BARLEY:5.10:5:19OCT1923
CHOCOLATE:50:50:11NOV1991
I am required to print out a daily summary report of the total amt of sales on that day. Based on the sample above, the result will be on 11 NOV 1991 the total amt of sales(2 items) will be 60 while on 19OCT1923 the total amt of sales(1 item) will be 5.
Desired Output:
11Nov1991 Total amt of sales:60
19Oct1923 Total amt of sales:5
My question is, how do i generate the code to show only one unique date with the total amount of sales? I have created a loop to check if that a certain year exist for testing purposes but it isn't working. I want it to be able to iterate through a file and check if a certain year exist and if it exist already, the next vector element that has the same year won't be written to a file but instead only the item price will be added. Below is the code i am trying to implement.
ifstream readReport("DailyReport.txt");
ofstream writeDailyReport("EditedDailyReport.txt");
string temp1 = "";
//Read from empty report.txt
while(getline(readReport,temp1))
{
for(int i=0; i < itemDescVec.size(); i++)
{
stringstream streamYearSS;
streamYearSS << itemDescVec[i].dateYear;
string stringYear = streamYearSS.str();
size_t found1 = temp1.find(stringYear);
//If can find year
if (found1 != string::npos )
{
cout << "Can find" << endl;
}
//If cannot find year
else if (found1 == string::npos )
{
cout << "Cannot find" << endl;
writeDailyReport << itemDescVec[i].itemDescription << ":" << itemDescVec[i].unitPrice << ":"
<< itemDescVec[i].quantity << ":" << itemDescVec[i].dateDay << "/" << itemDescVec[i].dateMonth << "/" << itemDescVec[i].dateYear
<< endl;
}
}
}
readReport.close();
writeDailyReport.close();
remove("DailyReport.txt");
rename("EditedDailyReport.txt", "DailyReport.txt");
It would be better to use your own object to store the data - then you can write, for example, salesRecord.quantity (or salesRecord.getQuantity() if you have written a getter) instead of salesrecord.at(2). This is a lot more readable and is better practice than remembering that 2 = quantity (etc.).
Now as to your actual question... I would say something like this:
Iterate over the list. For each item:
Check if the date of the item has already been added to your new list.
If the date does not already exist, simply add the item in its entireity.
If it does already exist, edit the existing item such that previousQuantity += quantityToAdd
On the sorted list
BARLEY:5.10:5:19OCT1923
STRAW:10:10:11NOV1991
CHOCOLATE:50:50:11NOV1991
this would work as follows:
Try to add barley data - nothing for that date so far, so add it:
19OCT1923 - 5 units
Try to add strawberry data - nothing for that date so far, so add it:
19OCT1923 - 5 units
11NOV1991 - 10 units
Try to add chocolate data - 11NOV1991 already exists, so add 50 to the 10 that's already there:
19OCT1923 - 5 units
11NOV1991 - 60 units

Python invalid Syntax Error at while line 7

def main():
print "This Program will calculate the amount of parking charges by hours using a given list: "
ticket = raw_input("Please enter ticket. If lost, Please enter no")
if ticket in ['No','no','N','n']
hour = float(input("Enter total hour at parking deck: ")
while(hour <= 0 or hour > 24):
hour = int(input("Enter an integer between 1-24 (hour): "))
The code above has a syntax error at line 6 at the word while
There is a bracket/brace missing in the below line. Add the brace and the error should go away.
hour = float(input("Enter total hour at parking deck: ")
Also the if needs a colon at the end. Below are the corrected lines
if ticket in ['No','no','N','n']:
hour = float(input("Enter total hour at parking deck: "))
Statements that start a new block such as if and while need a semicolon at the end.
if ...:
...
while ...:
...
And their blocks need to be indented one level as well.

Python 2.7x: How do I convert int of lets say 4.5 to 4.50

I am new (like 2 weeks) trying to learn Python 2.7x.
I am trying to do a basic program that has a user input a cost of a meal and it outputs how much it would be with a .15 tip. I want the output to look like 23.44 (showing 2 decimals)
My code:
MealPrice = float(raw_input("Please type in your bill amount: "))
tip = float(MealPrice * 0.15,)
totalPrice = MealPrice+tip
int(totalPrice)
print "Your tip would be: ",tip
print "Yout total bill would be: ",totalPrice
my output:
Please type in your bill amount: 22.22
Your tip would be: 3.333
Yout total bill would be: 25.553
You want to format your float value for printing only; use formatting:
print "Your tip would be: {:.2f}".format(tip)
print "Your total bill would be: {:.2f}".format(totalPrice)
The .2f is a formatting mini language specification for a floating point value of 2 digits after the decimal.
You need to remove the int() call to preserve those digits after the decimal. You don't need to call float() so much either:
MealPrice = float(raw_input("Please type in your bill amount: "))
tip = MealPrice * 0.15
totalPrice = MealPrice + tip
print "Your tip would be: {:.2f}".format(tip)
print "Your total bill would be: {:.2f}".format(totalPrice)
Demo:
Please type in your bill amount: 42.50
Your tip would be: 6.38
Your total bill would be: 48.88
You can further tweak the formatting to align those numbers up along the decimal point too.

if statement on discount

A_quantity = 10
B_quantity = 20
the quantity amount
N = float (input('please enter the quantity of package: '))
X_total = float (N*99.00)
the fee from input
Q_discount = (0.2*X_total)
W_discount = (X_total*0.3)
discounts from input total
Y_total = (X_total-Q_discount)
M_total = (X_total-W_discount)
the fee with the discount
def main ():
if N >= A_quantity:
print ('the total cost is $', \
format (Y_total, ',.2f'))
else:
if N >= B_ quantity:
print ('the total cost is $', \
format (M_total, ',.2f'))
main ()
the results should be 10 packages for $792.00
and 20 packages for $1,380.00
yet the second statement gets the 20% discount also which total to $1549.00, when it should get only 30% discount
I don't know which language it is, but it's an algorithm problem : you should first try for the highest value cause the way it is designed now, if N = 30 , you will always enter the "if" , never the "else" , and if N=5 , you will enter the "else" , but the the if inside it...
let me try although I don't know the language:
def main ():
if N >= B_quantity:
print ('the total cost is $', \
format (M_total, ',.2f'))
else:
if N >= A_quantity:
print ('the total cost is $', \
format (Y_total, ',.2f'))
main ()
take the value of the product divided by 100 and multiplied by the discount
and then get this result and the value of the product subitrair
var SomaPercent = ValorUnit/100 * descont;
var result_fim = ValorUnit-SomaPercent;
you can change the if condition to
if N >= A_quantity && N < B_quantity ...
if N >= B_quantity
..