1
def cont(caracter):
digito = ord(caracter)
return digito == 0 or digito == 9
def main():
usuario = str(input())
print(cont(usuario))
if __name__ == '__main__':
main()
Where is the error in that code?
1
def cont(caracter):
digito = ord(caracter)
return digito == 0 or digito == 9
def main():
usuario = str(input())
print(cont(usuario))
if __name__ == '__main__':
main()
Where is the error in that code?
3
There are some mistakes, and some things too complex.
You are transforming the character typed in its number into the table that determines which are the characters (ASCII or Unicode). So you have to buy with this number and not the number equivalent to the character. Maybe it wasn’t even what I wanted to do, but since you did I kept it, I just compared it to the numbers at table to be correct (48 is character 0 and 57 is character 9). I interpreted that the track is fully inclusive, but the question does not make this clear.
Another problem is that you’re just checking the tips, the first or the last, and the numbers that are in the middle of that, where do you compare? So I changed the operator to buy a range of numbers. There are other ways to do this.
I took some things that aren’t necessary, and I changed the function name because cont
doesn’t mean anything.
I did not solve all problems of that, read hkotsubo’s comment. If you do not want to give error you would have to make validations, even if you change the form of conversion of values. Because every data entry should be validated when there are restrictions on what you can type.
def verifica_digito(caractere):
digito = ord(caractere)
return 48 <= digito <= 57
print(verifica_digito(input()))
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
I got, thank you so much for your help
0
Can be done with integer conversion also. I made some modifications to the code to make it simple. Note that the function now returns True or False depending on what was typed.
def cont(number):
return number > 0 and number < 9
print(cont(int(input())))
Browser other questions tagged python operators condition
You are not signed in. Login or sign up in order to post.
I believe the question calls for amid 0 and 9 and not 0 or 9. Try
digito >= 0 and digito <= 9
. I didn’t understand the use of the functionord
, would not suffice aint(caracter)
?– anonimo
input
already returns a string, sostr(input())
is redundant and unnecessary. Just doinput()
that is sufficient to have a string– hkotsubo
It is also worth remembering that
ord
error if the string has more than one character, so a suggestion would be to check the size of the string (len(caracter)
) or capture theTypeError
– hkotsubo