What is wrong with this code?

print('What would you like to do today?')
print('--------------------------------')
print('1. Check Balance')
print('2. Deposit Cash')
print('3. Withdraw Cash')
print('4. Display transaction records')
print('5. End session') 
print('---------------- ----------------')
balance = 1000
choice = True
choice = 10

While choice > 0: <----- (What is wrong with this?)

  choice = input('Choose an option from (1 - 5)')
  choice = int([choice])
  if choice < 2:
    print('Your balance is: $', balance)
  elif choice < 3:
    deposit = input(' Enter deposit amount: ')
    deposit = int(deposit)
    balance += deposit
    print('Your balance is now: $', balance)
  elif choice < 4:
    withdrawal = input('Enter withdrawal amount: ')
    withdrawal = int(withdrawal)
    if withdrawal <= balance:
      balance -= withdrawal
      print('Your balance is now: $', balance)
    else:
      print('You do not have enough in your balance.')
  elif choice < 5:
    if balance > 1000:
      print('You have deposited $', deposit)
    elif balance < 1000:
      print('You have withdrawn $', withdrawal)
    else:
    print('There are no transaction records.')
  elif choice < 6:
    print('Have a nice day ahead, goodbye!')
else:
  print('invalid choice.')

Next time could you please put your code in a code block using three backticks then enter and put three backticks at end again.
Here is the fixed code, although I am not sure what you were asking to fix:

print("What would you like to do today?")
print("--------------------------------")
print("1. Check Balance")
print("2. Deposit Cash")
print("3. Withdraw Cash")
print("4. Display transaction records")
print("5. End session")
print("---------------- ----------------")

balance = 1000
choice = 10

while choice > 0:
    choice = input("Choose an option from (1 - 5)")
    choice = int(choice)
    
    if choice == 1:
        print("Your balance is: $", balance)
    elif choice == 2:
        deposit = input("Enter deposit amount: ")
        deposit = int(deposit)
        balance += deposit
        print("Your balance is now: $", balance)
    elif choice == 3:
        withdrawal = input("Enter withdrawal amount: ")
        withdrawal = int(withdrawal)
        if withdrawal <= balance:
            balance -= withdrawal
            print("Your balance is now: $", balance)
        else:
            print("You do not have enough in your balance.")
    elif choice == 4:
        if balance > 1000:
            print("You have deposited $", deposit)
        elif balance < 1000:
            print("You have withdrawn $", withdrawal)
        else:
            print("There are no transaction records.")
    elif choice == 5:
        print("Have a nice day ahead, goodbye!")
        break
    else:
        print("Invalid choice.")

Errors that were there:

  • No break statement after exiting, so program would not end
  • A while loop needs to have lowercase W, as with most python functions
  • You can also use withdrawal = int(input(‘Enter withdrawal amount: ‘)) instead of
    withdrawal = input('Enter withdrawal amount: ') withdrawal = int(withdrawal)
    and the same thing for deposit, you’ll just have to add error prevention by putting a else aligned with the while loop and an option to re-input.
1 Like

The problem is that you’re using a capital W. In Python, case matters.

I decided to format the code properly and run it incase there were any problems and, sure enough there were problems (read the comments):

print('What would you like to do today?')
print('--------------------------------')
print('1. Check Balance')
print('2. Deposit Cash')
print('3. Withdraw Cash')
print('4. Display transaction records')
print('5. End session')
print('---------------- ----------------')

balance = 1000
choice = 10

while choice > 0: # lowercase w
	choice = input('Choose an option from (1 - 5)')
	choice = int([choice]) # remove the [], you can only use int() on a string, not a list
	if choice < 2:
		print('Your balance is: $', balance)
	elif choice < 3:
		deposit = input(' Enter deposit amount: ')
		deposit = int(deposit)
		balance += deposit
		print('Your balance is now: $', balance)
	elif choice < 4:
		withdrawal = input('Enter withdrawal amount: ')
		withdrawal = int(withdrawal)
		if withdrawal <= balance:
			balance -= withdrawal
			print('Your balance is now: $', balance)
		else:
			print('You do not have enough in your balance.')
	elif choice < 5:
		if balance > 1000:
			print('You have deposited $', deposit)
		elif balance < 1000:
			print('You have withdrawn $', withdrawal)
		else:
			print('There are no transaction records.')
	elif choice < 6:
		print('Have a nice day ahead, goodbye!') # forgot to use the break command to break out of the while loop
	else:
		print('invalid choice.')
3 Likes

Your while statement starts with a capital W, Python is case sensitive, so I believe this will not work since while needs tobe lowercase, however I don’t know if that is the issue you are talking about.

4 Likes

if I find something doesn’t work I run it thru chatGPT
according to GPT here is the fixed code

print('What would you like to do today?')
print('--------------------------------')
print('1. Check Balance')
print('2. Deposit Cash')
print('3. Withdraw Cash')
print('4. Display transaction records')
print('5. End session') 
print('---------------- ----------------')

balance = 1000

while True:
  choice = int(input('Choose an option from (1 - 5): '))

  if choice == 1:
    print('Your balance is: $', balance)
  elif choice == 2:
    deposit = int(input('Enter deposit amount: '))
    balance += deposit
    print('Your balance is now: $', balance)
  elif choice == 3:
    withdrawal = int(input('Enter withdrawal amount: '))
    if withdrawal <= balance:
      balance -= withdrawal
      print('Your balance is now: $', balance)
    else:
      print('You do not have enough in your balance.')
  elif choice == 4:
    if balance > 1000:
      print('You have deposited $', deposit)
    elif balance < 1000:
      print('You have withdrawn $', withdrawal)
    else:
      print('There are no transaction records.')
  elif choice == 5:
    print('Have a nice day ahead, goodbye!')
    break
  else:
    print('Invalid choice. Please enter a number between 1 and 5.')

what GPT thinks was wrong

  1. The choice variable was assigned True and then immediately overwritten with 10. It should be assigned the result of the input function to get the user’s choice.
  2. The int function was being passed a list [choice], but it should just be passed choice directly.
    The if statements were checking if choice was less than 2, 3, 4, 5, or 6. But the expected inputs are integers from 1 to 5. So the conditions should be if choice == 1, elif choice == 2, and so on.
  3. The else statement at the end of the code was not indented correctly, and should be inside the while loop to be executed when none of the if conditions are met.
  4. The break statement was missing at the end of the if choice == 5 block to end the loop.
  5. The variable deposit and withdrawal were not initialized before they were used in the if choice == 4 block.
  6. In the if balance > 1000 block, the deposit variable is used even though it may not have been set in the current iteration of the loop.
1 Like

ChatGPT is unreliable and just bad for coding. Give it a simple problem, sure, it might work, but don’t rely on it.

3 Likes

yah but I mean it kinda works most of the time

2 Likes