-3

I'm getting a IndexError: tuple index out of range.

main.py file:

import Bank
import Input
balance = Bank.ATM.get_balance()
amongus = str.format("Starting Balance: {:.2f}")
print(amongus)
amount = Input.Validator("Please enter deposit amount ($0.00 - $1000.00): ",0.00,1000.00)
deposit()

Bank.py file:

class ATM:
    def deposit(amount):
        amongus = str.format("Depositing {:.2f}")
        print(amongus)
        balance = balance + amount
        return balance
    def withdraw(amount):
        if balance >= amount:
            amongus = str.format("Withdrawing {:.2f}")
            print(amongus)
            balance = balance - amount
        else:
            print("Insufficient funds")
            return balance
    def get_balance():
        return balance

balance = 20.00

Input.py file:

class Validator:
  def get_integer(prompt, min, max):
    while True:
      try:
        inputString = input(prompt)
        inputInt = int(inputString)

        if (inputInt >= min) and (inputInt <= max):
          return inputInt
      except:
         continue
  def get_float(prompt, min, max):
    while True:
      try:
        inputString = input(prompt)
        inputFloat = float(inputString)

        if (inputFloat >= min) and (inputFloat <= max):  # verify range
          return inputFloat
      except:
         continue

The error I'm currently getting with my code is this:

Traceback (most recent call last):
File "main.py", line 4, in <module>
amongus = str.format("Starting Balance: {:.2f}")
IndexError: tuple index out of range

amongus being just what I used as a temporary variable name until I come up with a better one.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    Seems like you're not using `format()` correctly, see https://docs.python.org/3/library/stdtypes.html#str.format – Random Davis Sep 13 '21 at 21:49
  • 1
    `str.format("Starting Balance: {:.2f}")` - so, what _is_ the balance? Where's the variable you're trying to put into the string? – ForceBru Sep 13 '21 at 21:50
  • Does this answer your question? ["IndexError: tuple index out of range" when formatting string in Python](https://stackoverflow.com/questions/42705962/indexerror-tuple-index-out-of-range-when-formatting-string-in-python) – Rayan Hatout Sep 13 '21 at 21:50
  • Off-topic: Strongly suggest your read and start following [PEP 8 - Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/). – martineau Sep 14 '21 at 00:38
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Sep 20 '21 at 11:30

1 Answers1

0

You have not specified what you want to insert in your format statement. What you want to do is probably:

amongus = str.format("Starting Balance: {:.2f}", balance)
Martin Dinov
  • 8,757
  • 3
  • 29
  • 41