#๐Ÿ”’ Why do I need to check if amount is >0 with .isdigit

58 messages ยท Page 1 of 1 (latest)

unkempt prawn
#

I have this piece of code from a yt tutorial from Tech with Tim.
Why do I need to check if the amount is greather than 0 ? Doesnt .isdigit check that alr?

harsh wadiBOT
#

@unkempt prawn

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

tiny hamlet
wooden axle
unkempt prawn
wooden axle
#

anyways, it's missing a colon on the end of the line with the last if statement

heavy dust
#

that just seems like a lot more work to me

tiny hamlet
heavy dust
wooden axle
wooden axle
unkempt prawn
wooden axle
fading flare
unkempt prawn
#

I do that in line 6, right?

fading flare
#

also you're missing a colon

unkempt prawn
#

Is there a more efficient way to do that?

fading flare
#

whats wrong with that method?

#

also note that int can raise a ValueError if given a non-int string

heavy dust
fading flare
wooden axle
wooden axle
# unkempt prawn Probably

if that is true, then you should just try to cast the string and then catch the exception if it can't

unkempt prawn
#

Alright thank you!

wooden axle
#
def deposit():
    while True:
        try:
            amount = int(input("What would you like to deposit? $"))
            if amount > 0:
                break
            print("The amount must be greater then 0")
        except ValueError:
            print("Input must be a valid integer")
    total += amount
wooden axle
#

and it's only partial code, if total is a global variable you would want a global statement at the beginning of the function as well, but i prefer to not use global variables, so consider this a code snippet taken out of its context

unkempt prawn
#

Thank u alot!

unkempt prawn
fading flare
#

amount will raise an error

#

just wrap only the int cast and return if it fails

fading flare
#

thats basically the only difference

#

you could check it with str.isdigit

wooden axle
fading flare
wooden axle
#

i would definitely have a small function for this that return the data or an error rather then doing it all in one big function

unkempt prawn
# wooden axle i would definitely have a small function for this that return the data or an err...

But doesnt it check the same thing?

if amount.isdigit():
this checks if the input is a positive number right?

amount = int (amount)
This turns the string into an integer right?

While writing this I believe in realising why your code is different. Your has like a way to handle any other input than s number right? Mine just states that it doesnt need a string or a negative number, but it doesnt handle the case of someone writing a string or a negative number in the input. Am I correct?

fading flare
#

honestly that's really all you need

#

as long as you dont let int go through if .isdigit fails

wooden axle
unkempt prawn
wooden axle
# unkempt prawn For example? Negative Numbers?

one example is the string "600_000_000" which int() will handle but be rejected by .isdigit() of you like that or not is another question, but it's easier to read in an instance then "60000000"

#

hint, u messed that up on purpose ๐Ÿ˜‰

#

did you catch that? ๐Ÿ˜

wooden axle
harsh wadiBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.