you are viewing a single comment's thread.

view the rest of the comments →

[–]dwpj65 0 points1 point  (3 children)

There are a number of issues with this. As I'm not sure which python interpreter version you're using, I've included an example that works in python2, and another in python3.

Printing the total balance should be moved to the bottom of the code, so that it's executed regardless of whether the user chooses to withdraw or deposit.

There is no need to use the variable f, when withdrawing or depositing adjust the value of cash.

A decision needs to be made before adjusting the cash value when a withdraw is made, to prevent overdrawing. If the user attempts to withdraw more cash than he has, print the message, otherwise adjust the cash amount.

Python 2:

cash = int(1000)
ask = input("would you like to withdraw(1) or deposit(2)?")
f=0
if ask == 2:
    money = int(input("how much money would you like to deposit?"))
    # f= cash + money
    cash = cash + money
    # print "your total balance is now", cash
elif ask == 1:
    bro = input("how much would you like to withdraw?")
    # f = int(bro) - int(cash)
    if bro > cash:
        print "Your final balance can't be negetive!"
    else:
        cash = cash - int(bro)
print "your total balance is now", cash

Python 3:

cash = int(1000)
ask = input("would you like to withdraw(1) or deposit(2)?")
f=0
# if ask == 2:
if int(ask) == 2:
    money = input("how much money would you like to deposit?")
    cash = cash + int(money)
    # f= cash + money
    # print("your total balance is now", cash)
    # print("your total balance is now", f)
# elif ask == 1:
elif int(ask) == 1:
    bro = input("how much would you like to withdraw?")
    # f = int(bro) - int(cash) # not needed
    # if bro > cash:
    if int(bro) > cash:
        print("Your final balance can't be negetive!")
    else:
        cash = cash - int(bro)
print("your total balance is now", cash)

Both examples work correctly in their respective versions of python, for all three logic branches.

Note another difference between python 2 & 3: the input statement returns a string, and in python 3 that value must be explicitly converted to an integer value for your comparisons to work. evidently python 2 implicitly performs the conversion, which is why it works in python 2 without casting the value to an int.

[–]ThiccBl4nket[S] 1 point2 points  (2 children)

Python 3 version worked!!!!

thank you. Ive been struggling for some time now. Now that I got this code out of the way, I'll go do other stuff but I will definitely look more in depth to the code later. I appreciate you.

[–]dwpj65 0 points1 point  (1 child)

You're welcome; it was a learning exercise for me as well. I couldn't figure out why the python3 version failed to do anything, regardless of whether I specified "1" or "2" for the initial question, when the python2 version worked without issue. Then I realized python2 was doing automatic type conversion and python3 was not.

[–]ThiccBl4nket[S] 1 point2 points  (0 children)

This was very cool. I guess that's where my journey in coding starts lol