Python invalid Syntax Error at while line 7 - python-2.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.

Related

TypeError: 'int' object is not subscriptable

So basically I have to tell the user if the staff working in a phone shop has earned a bonus. If the staff sold 4 or more phones, they get a bonus.
Problem is I keep getting this error.
if list[member_number]>=4:
TypeError: 'int' object is not subscriptable
This is my code-
How_Many_Members= int(input("Enter the number of staff members:"))
list=[]
member=1
while len(list)!= How_Many_Members:
print("how many phones did the member", member,"sold?",end=" ")
Sales=int(input(""))
list.append(Sales)
member=member+1
member_number=0
for list in range(0,How_Many_Members,1):
if list[member_number]>=4:
print("member",member_number,"has earned a bonus")
member_number=member_number+1
else:
print("member",member_number,"has not earned a bonus")
member_number=member_number+1
You have two list objects. One is an array, and the other is an object in a for statement, here:
for list in range(0,How_Many_Members,1):
You are using duplicates, and that's not good and is the thing causing your program to spit the error. It is using that list instead of the array list. And since the list in the for loop is an integer object, the error is thrown. You are basically trying to use subscript on an array but it mistakes it as the integer from the for loop because it is ambiguous. Try the following:
How_Many_Members = int(input("Enter the number of staff members:"))
list = []
member = 1
while len(list) != How_Many_Members:
print("how many phones did the member", member, "sold?", end=" ")
Sales = int(input(""))
list.append(Sales)
member += 1
member_number = 0
for _ in range(0, How_Many_Members, 1):
if list[member_number] >= 4:
print("member", member_number + 1, "has earned a bonus")
member_number += 1
else:
print("member", member_number + 1, "has not earned a bonus")
member_number += 1
Something else, you misspelled member_number in a few places and I fixed that for you. I also shorted some statements and let it print member 1 instead of member 0.
Your problem is here:
for list in range(0,How_Many_Members,1):
if list[member_number]>=4:
print("member",memeber_number,"has earned a bonus")
member_number=member_number+1
else:
print("member",memeber_number,"has not earned a bonus")
member_number=member_number+1
You are saying for list in range(0, How_Many_Members), so list is taking an integer value from 0 to How_Many_Members-1. So it's not a list anymore and you can't do list[member_number].

I before E program not working

Just wrote a python program to determine how useful the mnemonic "I before E except after C" is.
With the input:
'I before e except when conducting an efficient heist on eight foreign neighbors. I believe my friend has left the receipt for the diet books hidden in the ceiling'
It would display:
Number of times the rule helped: 5
Number of times the rule was broken: 5
Changed a few things and thought I changed them back but the code is now broken, any advice will be helpful
while True:
line = input("Line: ")
count = 0
h = 0
nh = 0
words = line.split()
for x in range(0, len(words)):
word = words[count]
if "ie" in word:
if "cie" in word:
nh += 1
else:
h +=1
if "ei" in word:
if "cei" in word:
h += 1
else:
nh += 1
else:
h += 0
count += 1
print("Number of times the rule helped:",h)
print("Number of times the rule was broken:",nh)
print()
Good god I'm an idiot. I've probably spent a total of 3 hours or so trying to fix this thing.
for x in range(0, len(words)):
word = words[count]
if "ie" in word:
if "cie" in word:
nh += 1
else:
h +=1
if "ei" in word:
if "cei" in word:
h += 1
else:
nh += 1
count += 1
Can anyone spot the difference between this and the corresponding part of the old code? That 'count+=1' at the end is just indented an additional time. All those hours wasted... sorry if I wasted anyone else's time here :|
You'd be well-served to better articulate your test cases. I don't exactly understand what you're trying to do.
It seems like all you need is to see how many times 'cie' or 'cei' occurred in the text.
In which case:
for i in range(0, len(line)):
print("scanning {0}".format(line[i:i+3]))
if line[i:i+3].lower() == "cie":
nh += 1
if line[i:i+3].lower() == "cei":
h += 1

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
..

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

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.