What is causing the Colon Expected Error in Python? - python

I am trying to get my head around Expected Colon Error in the following code. Any help appreciated.
The code is -
if df.loc['pivot'] > df.loc['Open'] :
df.loc['Signal'] = 1
elif df.loc[['Open'] > df.loc['IB'] and df.loc['Open'] > df.loc['pivot'] and df.loc['Open'] < df.loc['OB']:
df.loc['Signal'] = 0
elif (((df.loc['Open']) <= (((df.loc['2_pips']) - 5)/ 1000)) * (df.loc['pivot'])) and ((df.loc['Open']) >= (((df.loc['1_pips']) + 10)/ 1000 * (df.loc['pivot']))) and ((df.loc['Open']) >= (df.loc['pivot'])) :
df.loc['Signal'] = 1
elif (df.loc['Open'] <= ((df.loc['1_pips'] * df.loc['pivot']) + df.loc['pivot'] )) and (df.loc['Open'] > (((df.loc['1_pips'] - 10) * df.loc['pivot']) + df.loc['pivot'])) :
df.loc['Signal'] = 1
elif ((df.loc['Open'] < OB) and (df.loc['Open'] < df.loc['pivot'])):
df.loc['Signal'] = -1
elif ((df.loc['Open'] <= OB) and (df.loc['Open'] >= IB) and (df.loc['Open'] < df.loc['pivot'])):
df.loc['bs'] = 0
elif (df.loc['Open'] < ((df.Loc['2_pips'] - 5) * df.loc['pivot']) + df.loc['pivot']) and (df.loc['Open'] > ((df.loc['1_pips'] + 10) * pivot) + df.loc['pivot']) and (df.loc['Open'] < df.loc['pivot']):
df.loc['Signal'] = -1
elif (df.loc['Open'] <= (df.loc['1_pips'] * df.loc['pivot']) + df.loc['pivot']) and (df.loc['Open'] > ((df.loc['1_pips'] - 10) * df.loc['pivot']) + df.loc['pivot']):
df.loc['Signal'] = -1
else:
df.loc['Signal'] = 0
Thanks!

On line 4, you have df.loc[['Open'] which I guess is one square bracket too much.
You can also simplify that line as:
elif df.loc['Open'] > df.loc['IB'] and df.loc['pivot'] < df.loc['Open'] < df.loc['OB']:
And generally, you have way too many parentheses. I'm guessing that it's actually because you were trying to find the issue.
In general, when you get a compiler syntax error like an (un)expected token, you have a structural issue just above it. It is parsing the line it's complaining about as the continuation of the previous statement. Unmatched (), [] or {}. To find it, one could have reduced the code with the error and noticed that it was still failing with the same error, meaning that the actual error was in fact from the previous line(s).

Related

Exact Change in Python

I am trying to write a code in Python to where it outputs exact change using the fewest coins and one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. I also have to use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. When I input 45 and ran the code, I got an error saying (Your program produced no output). Here is my code:
total_change = int(input())
if total_change <= 0:
print('No change')
if total_change >= 100:
dollar = total_change//100
dollar_change = total_change % 100
if dollar == 1:
print(dollar + ' Dollar')
elif dollar > 1:
print(dollar + ' Dollars')
elif dollar_change >= 25:
quarter = dollar_change//25
quarter_change = dollar_change % 25
if quarter == 1:
print(quarter + ' Quarter')
elif quarter > 1:
print(quarter + ' Quarters')
elif quarter_change >= 10:
dime = quarter_change // 10
dime_change = quarter_change % 10
if dime == 1:
print(dime + ' Dime')
elif dime > 1:
print(dime + ' Dimes')
elif dime_change >= 5:
nickel = dime_change // 5
nickel_change = dime_change % 5
if nickel == 1:
print(nickel + ' Nickel')
elif nickel > 1:
print(nickel + ' Nickels')
elif nickel_change >= 1:
penny = nickel_change // 1
if penny == 1:
print(penny + ' Penny')
else:
print(penny + ' Pennies')
total = int(input())
if total == 0:
print("No change")
else:
denominations = [(100, "Dollar", "Dollars"), (25, "Quarter", "Quarters"), (10, "Dime", "Dimes"), (5, "Nickel", "Nickels"), (1, "Penny", "Pennies")]
for d in denominations:
coins = total // d[0]
total %= d[0]
if coins > 1:
print(f"{coins} {d[2]}")
elif coins == 1:
print(f"1 {d[1]}")
In this answer, I created a list full of tuples of all the coins and their values in cents. I then created a for loop which runs through all the tuples in the list, dividing the total removing the remainder for the number of coins
like this:
*coins = total // d[0] #returns the number of coins of the current iteration
Then, in order for the loop to continue to the next iteration and do the calculations correctly, I set the total in cents equal to the remainder of the total divided by the current iteration.
like this:
total %= d[0] #can also be written as total = total % d[0]
Then, I take the number of coins and check if the value is greater than one. If the conditional is met, it prints the number of coins followed by the corresponding "plural version" of the word.
like this:
if coins > 1:
print(f"{coins} {d[2]}")
#d[2] refers to the third item in the tuple of the current iteration
Finally, I use an else-if conditional to return 1 plus the "singular version" of the word
like this:
elif coins == 1:
print(f"1 {d[1]}")
#d[1] refers to the second item in the tuple of the current iteration
Your code has numerous problems that needed to be resolved, including the lack of a condition for input values 0 < total_change < 100, problems with indentation (the elif blocks should be aligned), unnecessary variables (you do not need variables like nickel_change and dime_change - total_change is all that matters), and you tried to print dollar + ' Dollar' even though dollar was a numeric variable.
I wanted to improve on the issues in your code and make it, well, functional, but without entirely rewriting your work. So, the basic framework of the code I'm going to provide is the same.
I used the method of recursion. I have the following function with all of your (cleaned) code:
def printCurrency(total_change):
dollar = total_change//100
dollar_change = total_change % 100
if dollar == 1:
print(str(dollar) + ' Dollar')
printCurrency(total_change-1*100)
elif dollar > 1:
print(str(dollar) + ' Dollars')
printCurrency(total_change-dollar*100)
elif dollar_change >= 25:
quarter = dollar_change//25
quarter_change = dollar_change % 25
if quarter == 1:
print(str(quarter) + ' Quarter')
printCurrency(total_change-1*25)
elif quarter > 1:
print(str(quarter) + ' Quarters')
printCurrency(total_change-quarter*25)
elif dollar_change >= 10:
dime = dollar_change // 10
dime_change = dollar_change % 10
if dime == 1:
print(str(dime) + ' Dime')
printCurrency(total_change-1*10)
elif dime > 1:
print(str(dime) + ' Dimes')
printCurrency(total_change-dime*10)
elif dollar_change >= 5:
nickel = dollar_change // 5
nickel_change = dollar_change % 5
if nickel == 1:
print(str(nickel) + ' Nickel')
printCurrency(total_change-1*5)
elif nickel > 1:
print(str(nickel) + ' Nickels')
printCurrency(total_change-nickel*5)
elif dollar_change >= 1:
penny = dollar_change // 1
if penny == 1:
print(str(penny) + ' Penny')
printCurrency(total_change-1*1)
else:
print(str(penny) + ' Pennies')
printCurrency(total_change-penny*1)
Notice how every time a line is printed, the function is ran again but after subtracting out the change we've already processed.
A few examples:
>>> printCurrency(45)
1 Quarter
2 Dimes
>>> printCurrency(101)
1 Dollar
1 Penny
>>> printCurrency(349)
3 Dollars
1 Quarter
2 Dimes
4 Pennies
And to tie this into your original framework with an input...
total_change = int(input())
if total_change <= 0:
print('No change')
if total_change >= 0:
printCurrency(total_change)
Let me know if you have any questions about the changes I've made to your code!
This is more of an answer that zybooks is looking for considering what it has taught up to this point. All that I have done here is decrement total_change each time I go down the list of coins. If there were no coins for that set then print a statement on the previous line.
total_change = int(input())
if total_change <= 0:
print('No change')
else:
dollar = total_change // 100
if dollar == 1:
print(dollar, 'Dollar')
total_change = total_change - (dollar * 100)
elif dollar <= 0:
print(end='')
else:
print(dollar, 'Dollars')
total_change = total_change - (dollar * 100)
quarter = total_change // 25
if quarter == 1:
print(quarter, 'Quarter')
total_change = total_change - (quarter * 25)
elif quarter <= 0:
print(end='')
else:
print(quarter, 'Quarters')
total_change = total_change - (quarter * 25)
dime = total_change // 10
if dime == 1:
print(dime, 'Dime')
total_change = total_change - (dime * 10)
elif dime <= 0:
print(end='')
else:
print(dime, 'Dimes')
total_change = total_change - (dime * 10)
nickel = total_change // 5
if nickel == 1:
print(nickel, 'Nickel')
total_change = total_change - (nickel * 5)
elif nickel <= 0:
print(end='')
else:
print(nickel, 'Nickels')
total_change = total_change - (nickel * 5)
penny = total_change // 1
if penny == 1:
print(penny, 'Penny')
total_change = total_change - (penny * 1)
elif penny <= 0:
print(end='')
else:
print(penny, 'Pennies')
total_change = total_change - (penny * 1)

calculate Output Change in the least amount of coins/notes for change using Python

The code provided below is not giving the output for "calculate Output Change in the least amount of coins/notes for change". I am new with Phyton and trying to learn, so anyone can help me spotting out and correcting what I have done wrong?. This application must- take inputs: Time in, Time Out; Output Cost, then ask for Payment in and the calculate Output Change in the least amount of coins/notes for change. The code is asking for inputs but not giving an output of calculating output change in least amount of coins/notes for change.
def print_euros(money, val):
while money <= val:
print("£" + str(val), end=", ")
money -= val
return money
def print_p(money, val):
while money <= val:
print(str(val), end="p,")
money -= val
return money
in_hour, in_min = map(int, input().split(':'))
out_hour, out_min = map(int, input().split(':'))
payment_in = input().split(',')
money_in = []
for i in payment_in:
money_in.append(int(i[1:]))
tot_money = sum(money_in)
tot_hours = 0
if out_hour <= in_hour:
tot_hours = 24 - in_hour + out_hour
else:
tot_hours = abs(out_hour - in_hour)
tot_mins = 0
if out_min <= in_min:
tot_mins = 60 - in_min + out_min
else:
tot_mins = abs(out_min - in_min)
if tot_mins == 60:
tot_hours += 1
tot_mins = 0
output_cost = tot_hours * 3
print("Output cost : £" + str(output_cost) + "." + str(tot_mins))
rem_change = 0
if tot_mins > 0:
rem_change = 100 - tot_mins
tot_money = tot_money - output_cost - 1
else:
tot_money -= output_cost
print("output change : ", end="")
tot_money = print_euros(tot_money, 20)
tot_money = print_euros(tot_money, 10)
tot_money = print_euros(tot_money, 5)
tot_money = print_euros(tot_money, 1)
rem_change = print_p(rem_change, 50)
rem_change = print_p(rem_change, 20)
rem_change = print_p(rem_change, 10)
rem_change = print_p(rem_change, 5)
rem_change = print_p(rem_change, 2)
rem_change = print_p(rem_change, 1)

Python Help - with “local variable referenced before assignment”

I have had problems with the shell saying local variable referenced before assignment and don't feel any previous answers have helped. Can I have some specific advice to this code:
Error : TotalExcessCharge = ExcessOneCharge + ExcessTwoCharge + ExcessThreeCharge + ExcessFourCharge + ExcessFiveCharge + ExcessPlusLimitCharge
UnboundLocalError: local variable 'ExcessThreeCharge' referenced before assignment
def BillingSystem(CustomerName,CustomerType,TotalGBUsed):
StandardCustomer = 1500
StandardQuota = 25
PremiumCustomer = 2500
PremiumQuota = 50
if (CustomerType == "Standard") or (CustomerType == "standard"):
if (TotalGBUsed > StandardQuota):
ExcessGB = TotalGBUsed - StandardQuota
for a in range(0, ExcessGB):
if (a <= 10):
ExcessOne = 250
ExcessOneCharge = a * ExcessOne
for b in range(0, ExcessGB):
if (b > 10) and (b <= 20):
ExcessTwo = 500
ExcessTwoCharge = b * ExcessTwo
for c in range(0, ExcessGB):
if (c > 20) and (c <= 30):
ExcessThree = 750
ExcessThreeCharge = c * ExcessThree
for d in range(0, ExcessGB):
if (d > 30) and (d <= 40):
ExcessFour = 1000
ExcessFourCharge = d * ExcessFour
for e in range(0, ExcessGB):
if (e > 40) and (e <= 50):
ExcessFive = 1250
ExcessFiveCharge = e * ExcessFive
for explus in range(0, ExcessGB):
if (explus > 50):
ExcessPlusLimit = 1500
ExcessPlusLimitCharge = explus * ExcessPlusLimit
TotalExcessCharge = ExcessOneCharge + ExcessTwoCharge + ExcessThreeCharge + ExcessFourCharge + ExcessFiveCharge + ExcessPlusLimitCharge
TotalCharge = StandardCustomer + TotalExcessCharge
print ("Total Excess Charge : " + str(TotalExcessCharge))
print ("Total Charge for this month : " + str(TotalCharge))
else:
print ("Total Excess Charge : 0")
print ("Total Charge for this month : " + str(StandardCustomer))
CName = input("[!] Customer Name : ")
CType = input("[!] Customer Type : ")
TotGB = int(input("[!] Total GB Usage : "))
BillingSystem(CName,CType,TotGB)
Obviously, at this point:
TotalExcessCharge = ExcessOneCharge + ExcessTwoCharge + ExcessThreeCharge + ExcessFourCharge + ExcessFiveCharge + ExcessPlusLimitCharge
your ExcessThreeCharge variable have not yet been assigned to, and that's because you assign to it under conditional:
for c in range(0, ExcessGB):
if (c > 20) and (c <= 30):
ExcessThree = 750
ExcessThreeCharge = c * ExcessThree
which might never be satisfied if ExcessDB is <= 20.
I'll not advise you how to fix it because, frankly, I do not understand the underlying logic of this code - it seems completely nonsensical to me.
The problem here is when your code doesn't go into the if conditions, your variables never get initiated but you have referred them at the end...So the error clearly tells you that you are calling the variable that you have never created or assigned. Always ensure that you refer the assigned variables!
And also you can make your code more easier to read like
using Excess# values directly inside the if conditions without assigning it to a variable.
using upper function on the input string and compare the value in one go..
def BillingSystem(CustomerName,CustomerType,TotalGBUsed):
StandardCustomer = 1500
StandardQuota = 25
PremiumCustomer = 2500
PremiumQuota = 50
ExcessOneCharge=0
ExcessTwoCharge=0
ExcessThreeCharge=0
ExcessFourCharge=0
ExcessFiveCharge=0
ExcessPlusLimitCharge=0
if (CustomerType.upper() == "STANDARD"):
if (TotalGBUsed > StandardQuota):
ExcessGB = TotalGBUsed - StandardQuota
for a in range(0, ExcessGB):
if (a <= 10):
ExcessOneCharge = a * 250
elif (a > 10) and (a <= 20):
ExcessTwoCharge = (a - 10) * 500
elif (a > 20) and (a <= 30):
ExcessThreeCharge = (a - 20) * 750
elif (a > 30) and (a <= 40):
ExcessFourCharge = (a - 30) * 1000
elif (a > 40) and (a <= 50):
ExcessFiveCharge = (a - 40) * 1250
elif (a > 50):
ExcessPlusLimitCharge = (a - 50) * 1500
TotalExcessCharge = ExcessOneCharge +
ExcessTwoCharge +
ExcessThreeCharge +
ExcessFourCharge +
ExcessFiveCharge +
ExcessPlusLimitCharge
TotalCharge = StandardCustomer + TotalExcessCharge
print ("Total Excess Charge : ", TotalExcessCharge)
print ("Total Charge for this month : ", TotalCharge)
else:
print ("Total Excess Charge : 0")
print ("Total Charge for this month : ", StandardCustomer)
CName = input("[!] Customer Name : ")
CType = input("[!] Customer Type : ")
TotGB = int(input("[!] Total GB Usage : "))
BillingSystem(CName,CType,TotGB)
And also instead of creating ExcessOneCharge, ExcessTwoCharge variables etc... You can do something like :
TotalExcessCharge = 0 #don't forget to initiate the variable at the beginning of the function
#then inside the if conditions
TotalExcessCharge += a*Excess#
This is just an example of how to write a cleaner code...logics you can apply as per your requirements!
Note : I'm typing everything in mobile, so pls ignore typos...
Because your definition of each of the "Excess#Charge" variables are within if statements, they seem not to be running for some reason. To fix this, I recommend defining all of the variables as 0 at the start so that if there is no excess value, it will simply be defined as 0. For example, at this at the top of the class:
ExcessOneCharge = 0
ExcessTwoCharge = 0
ExcessThreeCharge = 0
ExcessFourCharge = 0
ExcessFiveCharge = 0
ExcessPlusLimitCharge = 0

Longest Domino Sequence

Edit: New code. Both solutions were working perfectly, but for some reason did not improve my time results and that doesn`t make sense to me just yet.
Seeing my desperation, a friend PMed me his solution, which is working fine with the time given, but somehow fails on this specific input:
9
1 2 2 3 3 4 4 5
3 6 6 7 3 8 8 9 9 10
It gives "9" instead of "5". Any ideas why that could be?
maxNum = int(0)
idxArr = []
def ReadInput():
global array
global firstNum
arr = input().split()
firstNum = int(arr[0])
while int(arr[0]) * 2 + 1 > len(arr):
tempArray = input().split()
arr = arr + tempArray
iterator = int(0)
array = [0] * int(arr[0])
for i in range(1, int(arr[0]) * 2, 2):
tempArray = [0] * 3
tempArray[0] = int(arr[i])
tempArray[1] = int(arr[i + 1])
tempArray[2] = None
array[iterator] = tempArray
iterator+=1
def SortArray(array):
array.sort(key=lambda x: x[1])
array.sort(key=lambda x: x[0])
def MergeDominos(array):
tempNum = int(0)
for item in array:
if (item[2] == None):
iterator = tempNum
counter = int(0)
tempBool = False
try:
while item == array[iterator]:
if (tempBool):
array.pop(iterator)
counter += 1
else:
iterator += 1
counter += 1
tempBool = True
except IndexError:
True == True
if (counter % 2 == 1):
item[2] = counter
tempNum += 1
else:
tempItem = item.copy()
array.insert(tempNum + 1, tempItem)
array[tempNum + 1][2] = int(1)
item[2] = counter - 1
tempNum += 1
else:
tempNum += 1
def GetLengthOfArray(array):
counter = int(0)
for item in array:
counter += item[2]
return counter
def SwitchPlaces(item):
item[0], item[1] = item[1], item[0]
def GetMaxLength(array, tempArray, left, right):
global maxNum
# print("This is temp: ", tempArray)
for i in range(len(array)):
# print("Testing: ", array[i], "Iteration: ", i)
# print("IdxArr: ", idxArr)
if (len(array) <= len(idxArr)):
#print("BREAKING HERE BREAKING HERE BREAKING HERE BREAKING HERE BREAKING HERE BREAKING HERE BREAKING HERE BREAKING HERE BREAKING HERE BREAKING HERE BREAKING HERE BREAKING HERE BREAKING HERE BREAKING HERE BREAKING HERE ")
break
if (i not in idxArr):
#print("Condition:")
if (left == array[i][0]):
#print("LL")
if (i in idxArr):
break
else:
idxArr.append(i)
SwitchPlaces(array[i])
if (len(array) >= len(idxArr)):
tempArray.insert(0, array[i])
if (GetLengthOfArray(tempArray) > maxNum):
maxNum = GetLengthOfArray(tempArray)
if (len(array) >= len(idxArr)):
GetMaxLength(array, tempArray, tempArray[0][0], tempArray[len(tempArray) - 1][1])
if (left == array[i][1]):
#print("LR")
if (i in idxArr):
break
else:
idxArr.append(i)
if (len(array) >= len(idxArr)):
tempArray.insert(0, array[i])
if (GetLengthOfArray(tempArray) > maxNum):
maxNum = GetLengthOfArray(tempArray)
if (len(array) >= len(idxArr)):
GetMaxLength(array, tempArray, tempArray[0][0], tempArray[len(tempArray) - 1][1])
if (right == array[i][0]):
#print("RL")
if (i in idxArr):
break
else:
idxArr.append(i)
if (len(array) >= len(idxArr)):
tempArray.append(array[i])
if (GetLengthOfArray(tempArray) > maxNum):
maxNum = GetLengthOfArray(tempArray)
if (len(array) >= len(idxArr)):
GetMaxLength(array, tempArray, tempArray[0][0], tempArray[len(tempArray) - 1][1])
if (right == array[i][1]):
#print("RR")
if (i in idxArr):
break
else:
idxArr.append(i)
SwitchPlaces(array[i])
if (len(array) >= len(idxArr)):
tempArray.append(array[i])
if (GetLengthOfArray(tempArray) > maxNum):
maxNum = GetLengthOfArray(tempArray)
if (len(array) >= len(idxArr)):
GetMaxLength(array, tempArray, tempArray[0][0], tempArray[len(tempArray) - 1][1])
#else:
# print("No condition BIG OOOF")
ReadInput()
SortArray(array)
MergeDominos(array)
for i in range(len(array)):
#print("iter num: ", i)
tempArray = []
idxArr = []
idxArr.append(i)
tempArray.append(array[i])
if (GetLengthOfArray(tempArray) > maxNum):
maxNum = GetLengthOfArray(tempArray)
GetMaxLength(array, tempArray, tempArray[0][0], tempArray[len(tempArray) - 1][1])
print(maxNum)
E1: The input is made in this weird way, because the first item of the list gives the number of dominoes, and also, the input can come in multiple rows and then I create list item pairs.
Example input:
5 1 2
1 2
2 3
2
17
2 17
And the dominoes are:
[('1','2'),('1','2'),('2','2'),('2','17'),('2','17')]
Expected result:
5
(3,2)-(2,1)-(1,2)-(2,17)-(17-2)
The following is a rewrite of your solution with one significant change:
if maximum == len(listOfDominos) + len(tempList):
break
This prevents the code from exploring any further once it has a maximum that it knows it can't improve on. For the example input you provided, it reduced the number of searches by 20x:
def find(listOfDominos, tempList):
maximum = len(tempList)
for currentDominoIndex, domino in enumerate(listOfDominos):
if maximum == len(listOfDominos) + len(tempList):
break # we can't do any better, so why try?
remainingDominos = listOfDominos[:currentDominoIndex] + listOfDominos[currentDominoIndex+1:]
if tempList:
backwardDomino = domino[::-1]
head, tail = tempList[0], tempList[-1]
if domino[1] == head[0]:
maximum = max(find(remainingDominos, [domino] + tempList), maximum)
elif backwardDomino[1] == head[0]:
maximum = max(find(remainingDominos, [backwardDomino] + tempList), maximum)
elif domino[0] == tail[1]:
maximum = max(find(remainingDominos, tempList + [domino]), maximum)
elif backwardDomino[0] == tail[1]:
maximum = max(find(remainingDominos, tempList + [backwardDomino]), maximum)
else:
maximum = max(find(remainingDominos, [domino]), maximum)
return maximum
listOfNumbers = input().split()
numberOfDominos = int(listOfNumbers.pop(0))
while numberOfDominos * 2 > len(listOfNumbers):
listOfNumbers += input().split()
listOfDominos = list(zip(listOfNumbers[0::2], listOfNumbers[1::2]))
print(find(listOfDominos, []))
Give this a try to see if it improves performance without introducing any bugs in the process.
Try this solution:
def solution(dominoes):
if len(dominoes) == 0:
return 0
def get_sequence(d, sol=None):
if sol is None:
sol = []
if len(d) == 0:
return
for i in range(len(d)):
rest = d[:i] + d[i+1:]
d1, d2 = d[i], d[i][::-1]
if d1 == d2:
if (not sol) or (sol[-1][1] == d1[0]):
yield sol + [d1]
yield from get_sequence(rest, sol + [d1])
else:
if (not sol) or (sol[-1][1] == d1[0]):
yield sol + [d1]
yield from get_sequence(rest, sol + [d1])
if (not sol) or (sol[-1][1] == d2[0]):
yield sol + [d2]
yield from get_sequence(rest, sol + [d2])
return(len(max(get_sequence(dominoes), key=len)))
dominoes = [('1','2'),('1','2'),('2','2'),('2','17'),('2','17')]
print(solution(dominoes))
Prints:
5

When making an and statement, I get an Syntax Error

When making a tax calculator, I seem to continue to be getting an error at the "=" in the following line of code:
elif int(vermogen) >= 30001 and <= 100800:
But I can't see a way to fix this. I've tried removing the integers but that didn't help, I've also tried to change my symbols to > and < instead of <= >= but that also didn't help. I think I made an obvious mistake but I don't seem to find it. When removing the "=" behind the "<" The syntax error moves from the "=" to the "<". I hope to hear from you soon. Here is the code I've been using/ It's in dutch, but that shouldn't affect anything.
vermogen = input('Hoeveelheid vermogen: ')
N_schijf1 = 30000
N_schijf2 = 70800
N_schijf3 = 877200
P_schijf2 = 424.8
P_schijf3 = 11403.6
if int(vermogen) <= 30000:
print("Je betaalt 0 euro aan vermogensbelasting")
elif int(vermogen) >= 30001 and <= 100800:
vermogen_betalen = int(vermogen) - N_schijf1
betalen_vermogen = vermogen_betalen * 0.006
print("Je betaalt",betalen_vermogen,"euro aan vermogensbelasting")
elif int(vermogen) >= 100801 and <= 978000:
vermogen_betalen = int(vermogen) - N_schijf1 - P_schijf2
betalen_vermogen = vermogen_betalen * 0.013
tebetalen_vermogen = P_schijf2 + betalen_vermogen
print("Je betaalt",tebetalen_vermogen,"euro aan vermogensbelasting")
elif int(vermogen) >= 978001:
vermogen_betalen = int(vermogen) - N_schijf1 - P_schijf2 - P_schijf3
betalen_vermogen = vermogen_betalen * 0.0161
tebetalen_vermogen = P_schijf2 + P_schijf3 + betalen_vermogen
print("Je betaalt",tebetalen_vermogen,"euro aan vermogensbelasting")
print("Test")
You must have a complete condition on each side of the and. Operands don't carry across the and into the next condition.
You could write:
int(vermogen) >= 30001 and int(vermogen) <= 100800
Or just:
30001 <= int(vermogen) <= 100800
since Python supports chaining of comparisons
you need to change your if from:
elif int(vermogen) >= 100801 and <= 978000:
elif int(vermogen) >= 30001 and <= 100800:
to
elif int(vermogen) >= 100801 and int(vermogen) <= 978000:
elif int(vermogen) >= 30001 and int(vermogen) <= 100800:

Categories