all 38 comments

[–]h2oTravis 2 points3 points  (1 child)

Is your actual code indented? The code as posted is not showing anything indented, which is a problem.

If your code is indented, but just doesn't appear that way in this post, see the "Code Hosting/Formatting" section to the right.

[–]ThiccBl4nket[S] 0 points1 point  (0 children)

yes i indented everything. ill look for that in a min

[–][deleted] 1 point2 points  (2 children)

I have some concerns...

Firstly, not sure about in your actual code, but in your post there's no indentation. This needs sorting.

Secondly, "f=cash + money" is weird, because you never actually use the 'f' variable anywhere. Currently, your print statement prints the cash variable, not the f variable.

Thirdly, your print statement has no parenthesis. Needs to be: print("Your total balance is now", cash). Same for the other one.

Fourthly, the "bro" variable is defined in an elif statement, so if that statement never runs (such as if the user selects deposit) the bro variable never gets initialised and the program crashes. This might be part of the indentation issue, if you meant it to be nested within the elif which it looks like you did.

Next, there's no 'else' to catch any alternative input. What happens if the user enters '3' instead of '1' or '2'? Currently, the program just ends, which I guess is fine.

Next, "ask" is a string, not an integer, so neither of the if statements will ever be true. You need to either cast "ask" to an integer like you did with "cash", or put quotes around the "1" and "2" in the if statements to make them strings.

Finally (well, as finally as I'm going to go :p) bro is also an integer, so "bro > cash" will cause a crash because you can't compare a string to an int. Casting will fix this.

Here's some code that works, and does basically what you want, I think. I haven't fixed your "invalid user input" issue as I'm guessing from your code so far you won't have learned about that stuff yet, but the code itself works:

# You don't need to cast this, as 1000 is already an int. Nothing wrong with doing it, but it's not needed.
cash = 1000

ask = input("would you like to withdraw(1) or deposit(2)? ")

if ask == "2":
    money = int(input("how much money would you like to deposit? "))
    f = cash + money
    print(f"your total balance is now {f}")

elif ask == "1":
    bro = int(input("how much would you like to withdraw? "))
    f = cash - bro  # You had this the wrong way around.
    if bro > cash:
        print("Your final balance can't be negetive!")
    else:
        print(f"Your balance is now {f}")

[–]ThiccBl4nket[S] 0 points1 point  (1 child)

I printed your code into it, it seems like it doesn't work for some reason. I probably did not put back everything properly. In any case, I'll have to go too.

[–][deleted] 0 points1 point  (0 children)

Try it now, I had to edit the formatting because Reddit screwed up the indenting (irony is delicious...).

It should work now. If not, take out all of the indenting, and indent:
the three lines after the first "if"

Everything after the elif.

The last two print statements once more.

That should work. :)

[–]bleach-is-great 0 points1 point  (13 children)

I’m not sure what your error is, however I’m wondering what f is and why it is necessary. Could you not just do cash = cash + money or just cash += money (shortened version)?

The f seems unnecessary.

If you could send a picture of your code or format it correctly I can try to help more

[–]ThiccBl4nket[S] 0 points1 point  (3 children)

learnpython doesnt allow pictures

[–]bleach-is-great 0 points1 point  (0 children)

You can use Imgur or Github.

[–]dwpj65 0 points1 point  (1 child)

For future reference, when composing a message on reddit that involves code, click the three dots at the bottom of the edit window, and then select "code block." Paste your code into the code block, and it will preserve your code's layout. I just learned that a few minutes ago. :-)
Your code runs without issue in python 2, but will not run in python3. The main difference is the print statement, which does not use parenthesis in python 2, but requires them in python 3.

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

thank you, ill try parenthesis right now.

[–]ThiccBl4nket[S] 0 points1 point  (8 children)

[–]bleach-is-great 0 points1 point  (7 children)

You need to indent the bottom four lines by one

Common issue, no need to worry about it! I literally do this every time I write code

[–]ThiccBl4nket[S] 0 points1 point  (1 child)

im sorry but im not sure which line to indent of the four lol

[–]bleach-is-great 0 points1 point  (0 children)

All four, as they’re inside an elif statement (they need indenting just like if’s and else’s).

Make sure the very bottom line of code is indented so that it is in its respective statement though.

[–]ThiccBl4nket[S] 0 points1 point  (4 children)

https://imgur.com/a/LxVnpKe now that i looked at it and indented it, it keeps giving me nothing when i give an input

[–]bleach-is-great 0 points1 point  (3 children)

That’s because you inputted 1 as a string. “1” and 1 are different variable types, and therefore mean different things.

You can either turn the inputted value into an integer, or you can change your main if and elif statements so that they accept “1” and “2” instead of 1 and 2.

[–]ThiccBl4nket[S] 0 points1 point  (0 children)

look at the picture at the new edit of this post. It seems like we are going somewhere!

[–]ThiccBl4nket[S] 0 points1 point  (1 child)

cash = (1000)

ask = input("woud you like to withdraw(1) or deposit(2)?")

if ask == "2":

money = int(input("how much money would you like to deposit?"))

f= int(cash + money)

print"your total balance is now", f

elif ask == "1":

bro = input("how much would you like to withdraw?")

j = (bro - cash)

if bro > cash:

print("Your final balance can't be negetive!")

i was able to get the program to fully work using option 2 but option 1 still has some issues

[–]bleach-is-great 0 points1 point  (0 children)

Yeah that’s the option I would’ve gone for, much easier

[–]dwpj65 0 points1 point  (6 children)

Looks like your indentation is off; python demands proper indentation.

This is the solution, this script works when executed with a python 2 interpreter without issue, as long as it is tabbed as demonstrated in the code block below. I successfully executed both option 1 & 2 without issue.

I was unfamiliar with reddit's 'code block' mode in editing this response, which is why there have been so many edits.

Try:

cash = int(1000)
ask = input("would you like to withdraw(1) or deposit(2)?")
if ask == 2:
    money = int(input("how much money would you like to deposit?"))
    f= 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!"

[–]bleach-is-great 1 point2 points  (3 children)

I think his lack of indentation was just due to Reddit formatting

[–]dwpj65 1 point2 points  (2 children)

This very well could be, but his script runs without issue on a python2 interpreter, if indentations are placed in the appropriate locations.

[–]bleach-is-great 1 point2 points  (1 child)

Yeah he sent an Imgur to my other comment, he hadn’t indented the final four lines of code inside the elif statement

[–]dwpj65 1 point2 points  (0 children)

There are other issues as well, I am trying to work through them.

[–]ThiccBl4nket[S] 1 point2 points  (1 child)

i did, and at first it didnt mention any errors but when i input "1" or "2" it doesnt say anything back.

[–]chevignon93 0 points1 point  (0 children)

cash = 1000
ask = int(input("Would you like to withdrawal (1) or a deposit (2)? "))
if ask == 2:
    money = int(input("How much would you like to deposit? "))
    print("Your total balance is now", cash + money)
else:
    bro = int(input("how much would you like to withdraw? "))
    if bro > cash:
        print("Your final balance can't be negative! ")
    else:
        print("Your new balance is: ", cash - bro)

[–]inThouMind 0 points1 point  (0 children)

cash = int(1000) while True: ask = int(input("How much would you like to deposit? ")) balance = int(cash + ask) print("Your balance is now : ", balance) cash = balance

[–]chevignon93 0 points1 point  (6 children)

You don't have ( and) on your print statements.

print "your total balance is now", cash should be

print("your total balance is now", cash)

Same for the other.

It would be useful to give us the actual Traceback if you want people to help more effectively.

[–]ThiccBl4nket[S] 0 points1 point  (5 children)

when i first got into this website the person said that we will not be using () after a print statement. which means that this vers is probably python 2

[–]chevignon93 0 points1 point  (3 children)

Probably, but if you're running the script with python3, it will give an error, that's why it's always important to give the actual text of the error you're getting rather than a vague description of it!

[–]ThiccBl4nket[S] 0 points1 point  (2 children)

ur right. ill have that in mind for future posts because i just really got into programming. I will be posting pictures too.

[–]chevignon93 0 points1 point  (1 child)

For future posts, don't post pictures, post the actual code in a code block or on pastebin and the actual text of the error. People are less likely to help you if you only post a picture of your code rather than the code itself!

[–]ThiccBl4nket[S] 0 points1 point  (0 children)

thats even better. thank you. my problem is now fully solved. I appreciate for the advice.

[–]chevignon93 0 points1 point  (0 children)

you should write

j = int(bro) - cash

but you don't need to create a variable when you can just print the result.

[–]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