185k views
14 votes
Can someone help me figure out why my else statment is not working?

----------------------------------------------------------------------------------------------------------------------------
print("\tBank of The Funny Money Bunny Farm\\")
print("Client\t\tDeposits\tWithdrawals\tBalance\t\tNotes:")
print("__________________________________________________________________________________________________________________________")

names = ['Judith', 'Jimmie', 'Johnny', 'Joseph', 'Jeffry', 'Buddie', 'Charlie', 'Maxine', 'Jannis', 'Bodine']
deposists = [255.23, 525.55, 323.21, 651.48, 507.11, 101.11, 253.36, 323.22, 611.45, 252.91]
withdrawals = [255.76, 128.65, 156.22, 175.33, 390.12, 255.41, 128.22, 156.23, 728.98, 251.43]
balance = []
notes = []

for i in range(len(names)):
balance.appened(deposits[i] - withdrawals[i])
if(balance < 0):
print((names[i] + ", you are overdrawn. Your account balance is " + "$%.2f"%(balance[i]))
else:
print("" + names[i] + "\t\t" + "%.2f"%(deposits[i]) + "\t\t" + "%.2f"%(withdrawals[i]) + "\t\t" + "%.2f"%(balance[i]) + "\t\t" + notes[i])

print("__________________________________________________________________________________________________________________________")
print("\© Copyright 2021 Funny Bunny Money Productions")

#Main

print("\tBank of The Funny Money Bunny Farm\\")
account = int(input("How many new accounts are needed: "))
for i in range(account):
input("Enter the name of the client: ")
input("Enter the deposist of the client: ")
input("Enter the withdraral of the client: ")

2 Answers

4 votes

Final answer:

The problem with the else statement not working is likely due to a typo in 'balance.appened', the misuse of the 'balance' list in the if condition, and an IndexError due to an empty 'notes' list.

Step-by-step explanation:

The issue with your else statement not executing as expected might be due to several potential problems in the code. The first one is a typo in the balance.append method where it's incorrectly spelled as balance.appened. This will cause a runtime error before the else statement is even reached.

Secondly, the condition if(balance < 0): assumes balance is a single number, but balance is actually a list, so you need to compare the balance for each individual client within the loop using balance[i] instead. The if-else block also needs to be inside the for loop so that it checks each client's balance after it is calculated.

Finally, there is a logical error since the notes list is empty, indexing it with notes[i] within the else block will cause an IndexError.

User Heracek
by
5.2k points
9 votes

Answer:

below is a version of your code that runs.

I'm not sure if it does what you expect it to do.

Can someone help me figure out why my else statment is not working? ---------------------------------------------------------------------------------------------------------------------------- print-example-1
Can someone help me figure out why my else statment is not working? ---------------------------------------------------------------------------------------------------------------------------- print-example-2
User CptAJ
by
5.3k points