1

I'm trying to make a code for my python class and i'm making a wallet management system basic. but i cant get this infinite loop to work. What am i doing wrong?

def main():
# Initial balance
 balance = input ('Wallet balance: ')

# This constructs the infinite loop
while true:

    # Initial balance
     balance = input ('Wallet balance: ')

    # Withdraw amount from wallet balance
     withdraw= input ('Withdraw amount: ')

    #If there is no balance to withdraw from
     if balance < 1:
         print ('You have no funds to withdraw.')

    # Withdraw process and new balance display
     if balance > 0:
         new = balance - withdraw
         print ('Your new balance: '), new
main()
  • 3
    What do you mean by "cant get it to work"? – OptimusCrime Aug 26 '14 at 23:16
  • 4
    In python True is capitalized – asdf Aug 26 '14 at 23:16
  • 2
    Your indentation is also all over the place. – Crowman Aug 26 '14 at 23:19
  • In case you have the same indentation in your original file not that your while loop is not in the main function. – user3885927 Aug 26 '14 at 23:20
  • @OptimusCrime. When I press run I get an error saying "there's an error in your program: univalent does not match any outer indentation level" so I tried playing around with the indents and I could figure it out. – MrSassyBritches Aug 26 '14 at 23:21
  • @BadKarma thank you. I didn't not know that. I capitalized it. But didn't fix this problem. – MrSassyBritches Aug 26 '14 at 23:22
  • You might want to run your code through [pyflakes](https://pypi.python.org/pypi/pyflakes) and [pep8](https://pypi.python.org/pypi/pep8). For example, on line 3 you retrieve balance from the user, but never use it. – Brad Beattie Aug 26 '14 at 23:22
  • @TollyBear - That means that your indention is wrong. Please se my answer. – OptimusCrime Aug 26 '14 at 23:22
  • I checked your indentation in eclipse and noticed you had 5 spaces for everything. I'm not sure if this is something that you did in your code or in moving it to stack overflow. But if you copy my below solution into whatever IDE you're using it should work. – asdf Aug 26 '14 at 23:24
  • By the way, there's a flaw in your logic. You never check if the balance has enought money to withdraw what the user inputs, only if the balance is more than zero. – OptimusCrime Aug 26 '14 at 23:24
  • Oh holy crap. All I needed was a space lol. It took me a minute to process what everyone was saying. So simple. Thanks for your help everyone. Are there any other problems you see with my code that I can't see? – MrSassyBritches Aug 26 '14 at 23:24
  • @OptimusCrime what is a way I can do this? I've thought about it but couldn't think of how to really put that in the code. – MrSassyBritches Aug 26 '14 at 23:26
  • 1
    On top of the other two problems, `input` returns a string, so `balance < 1` is just going to raise a `TypeError: unorderable types: str() < int()`. – abarnert Aug 26 '14 at 23:35
  • And one more: `print ('Your new balance: '), new` isn't going to print `'Your new balance: 123'`, it's going to print `'Your new balance: '` and then evaluate the tuple `(None, 123)` and do nothing with it. Arguments to functions have to go inside the parentheses. – abarnert Aug 26 '14 at 23:36
  • `input` will give you a string, so cast it to an int. – corvid Aug 26 '14 at 23:47

2 Answers2

3

Fixed it for you, it had a few errors

True must be capitalized in Python

If you are passing an int into a str you need to cast the int as a str

Also, I'm not sure why what you're using the main function for? It doesn't pass the initial value into the infinite loop and seems redundant.

def main():
    # Initial balance
    balance = input ('Wallet balance: ')

# This constructs the infinite loop
while True:

    # Initial balance
    balance = input('Wallet balance: ')

    # Withdraw amount from wallet balance
    withdraw= input('Withdraw amount: ')

    #If there is no balance to withdraw from
    if balance < 1:
        print('You have no funds to withdraw.')

    # Withdraw process and new balance display
    if balance > 0:
        new = balance - withdraw
        print('Your new balance: ' + str(new))
main()

However, I'm assuming you're trying to build a functional wallet that can withdraw and deposit. I'm not sure how comfortable you are with python and class construction, but I built a fully functional wallet for you to check out and learn from.

class Wallet:

    #Grab the initial balance in the account
    def __init__(self, initial_balance):
        self.balance = initial_balance

    #method that deposits funds to the balance
    def deposit(self, deposit_amount):
        self.balance += deposit_amount

    #method that attempts to withdraw from the balance
    def withdraw(self, withdraw_amount):
        if withdraw_amount <= self.balance:
            self.balance -= withdraw_amount
        else:
            print("Insufficient funds. You have " + str(self.balance) + " in your account. Please deposit more or withdraw less.")

    #display the balance
    def display_balance(self):
        print("The current balance in the account is " + str(self.balance))
        print("-----")

#Check if this module is the one currently being run
if __name__ == "__main__":
    #Ask user for initial balance
    initial_balance = int(input("Initial balance in account: "))
    #initiate the class, passing the initial balance
    wallet = Wallet(initial_balance)
    wallet.display_balance()
    while True:
        #Pick an option to withdraw, deposit or exit
        print("1: Deposit funds")
        print("2: Withdraw funds")
        print("3: Exit")
        type = int(input("Please select an option (1, 2 or 3): "))
        if type == 1:
            deposit_amount = int(input("Please specify amount to deposit: "))
            wallet.deposit(deposit_amount)
            wallet.display_balance()
        elif type == 2:
            withdraw_amount = int(input("Please specify amount to withdraw: "))
            wallet.withdraw(withdraw_amount)
            wallet.display_balance()
        elif type == 3:
            break
        else:
            print("Invalid selection, please type either 1, 2 or 3")
asdf
  • 2,927
  • 2
  • 21
  • 42
  • Karma so am i able to just remove the def main? – MrSassyBritches Aug 26 '14 at 23:28
  • Yup, if all you want is to ask the user for their balance and how much they want to withdraw each time it loops then yes you can remove it. However, if you want to have a set amount in the wallet and be able to "withdraw" from it then you'll need to completely rearrange your code. – asdf Aug 26 '14 at 23:33
  • @TollyBear check out the edit I just posted. I'm assuming you're trying to build a functional wallet. I provided a little code I built for you to use and learn from – asdf Aug 26 '14 at 23:51
0

I don't have python3, but this worked in python2 and I added the parentheses around the print-statement after.

def main():
    # Initial balance
    balance = int(input('Wallet balance: '))

    # This constructs the infinite loop
    while True:
        # Withdraw amount from wallet balance
        withdraw = int(input('Withdraw amount: '))

        #If there is no balance to withdraw from
        if balance < 1:
            print ('You have no funds to withdraw.')

        # Withdraw process and new balance display
        if balance > 0:
            new = balance - withdraw
            print ('Your new balance: ', new)
main()
OptimusCrime
  • 14,662
  • 13
  • 58
  • 96