#๐ Why do I need to check if amount is >0 with .isdigit
58 messages ยท Page 1 of 1 (latest)
@unkempt prawn
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.
It should, ya. It's arguably good to make that check explicit though. It's not clear from amount.isdigit alone that you're also ensuring that the number was positive
i would say that it isn't very pythonic to write the code like that
it's preferred to just cast the data with int() and catch the exception
and if it can be converted then check if it is > 0
So is the code the person in the tutorial wrote bad? Bc im watching the video to learn how to write efficient/good python code if you could call it that
anyways, it's missing a colon on the end of the line with the last if statement
why? this works perfectly fine
that just seems like a lot more work to me
A lot of YT tutorials show bad practices. The bar for entry is so low, anyone can make videos.
especially for someone who (i assume) is pretty new to python
Oh, ok
at least i don't think it's his specialty
it kind of works but doesn't take into account other possible valid ways to enter a number
What does casting the data with int() mean?
like?
in this instance it's about parsing the string to an integer if possible
turn it into an integer
I do that in line 6, right?
Is there a more efficient way to do that?
not really
whats wrong with that method?
also note that int can raise a ValueError if given a non-int string
colon*

it depends on if you expect most inputs to be valid numbers or not
Probably
if that is true, then you should just try to cast the string and then catch the exception if it can't
Alright thank you!
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
untested code above written directly here in the chat on my phone
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
Thank u alot!
May I ask, in contrast to my code above, what makes this code superior? Is there smth about your code, that prevents errors or smth?
that's unbound btw
amount will raise an error
just wrap only the int cast and return if it fails
yours just doesn't catch the ValueError from the int cast
thats basically the only difference
you could check it with str.isdigit
as i said, untested and taken out of context, i also tried to conform to how the original function worked (not returning out of the function anywhere), it's not how I would choose to write this myself

didnt see this myself because i was on my phone too
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
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?
both of your checks are correct, yes
honestly that's really all you need
as long as you dont let int go through if .isdigit fails
but .isdigit() may reject valid numbers that int() is able to convert
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? ๐
you were asking for a more efficient way
this way you don't need to first check if every character is a number and then convert it
if it was efficiency you were after
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.