Python Forum

Full Version: checking if there are numbers in the string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
flyTo = input("Where would you like to fly? ")
if flyTo == flyTo.isdigit():
    print("You accidentally added a number")
elif flyTo == flyTo.isalpha():
       print("Okay, we are going to {} ".format(flyTo))
else:
    print("??")
k don't laugh

no matter what is entered,
it says: "??"

I want it to check if there are numbers.. and if so, to say "you accidentally added a number"
how would i proceed with allowing letters, not numbers?

thanks
Hi Sp00f,
What do you expect your line 2 to do exactly, step by step?
Same question for line 4.
You can test these things easily in python interactive console.
(Apr-05-2017, 07:20 PM)Kebap Wrote: [ -> ]Hi Sp00f,
What do you expect your line 2 to do exactly, step by step?
Same question for line 4.
You can test these things easily in python interactive console.

hello,
i want it to check the flyTo = input()
to see if there are numbers, what so ever.. and if so, to say:
print("there was a number in your flyTo = input()")
and then continue with what it is doing..

and as for line 4, I guess the exact opposite.. i want it to check and see if there are letters
>>> x='test'
>>> x==x.isdigit()
False
>>> x==x.isalpha()
False
>>> x.isalpha()
True
>>> y='123'
>>> y.isdigit()
True
(Apr-05-2017, 07:39 PM)buran Wrote: [ -> ]
>>> x='test'
>>> x==x.isdigit()
False
>>> x==x.isalpha()
False
>>> x.isalpha()
True
>>> y='123'
>>> y.isdigit()
True
i was thinking i understand that part of it.. but how would i execute it in my particular code?

for some reason it just skips to the end, and prints "??"
you are comparing string (the user input) with boolean (the result of isdigit and isalpha).
That would always evaluates to False and your else is executed.

flyTo = input("Where would you like to fly? ")
if flyTo.isalpha():
   print("Okay, we are going to {} ".format(flyTo))
elif any(c.isdigit() for c in flyTo):
   print("You accidentally added a number")
else:
   print("??")
however I would skip the elif altogether
(Apr-05-2017, 08:03 PM)buran Wrote: [ -> ]you are comparing string (the user input) with boolean (the result of isdigit and isalpha).
That would always evaluates to False and your else is executed.

flyTo = input("Where would you like to fly? ")
if flyTo.isalpha():
   print("Okay, we are going to {} ".format(flyTo))
elif any(c.isdigit() for c in flyTo):
   print("You accidentally added a number")
else:
   print("??")
however I would skip the elif altogether

oh, i was comparing a string to a boolean?
can you elaborate on, "any(c.isdigit() for c in flyTo):"
thanks a lot
(Apr-05-2017, 08:17 PM)Sp00f Wrote: [ -> ]can you elaborate on,  "any(c.isdigit() for c in flyTo):"
thanks a lot

In slo-mo:
  • for c in flyTo in an iterator... it iterates each element of flyTo. Since flyTo is a string the elements are characters
  • c.isdigit() is True if c is a digit
  • for c in flyTo builds a list of True or False, once for each character
  • any(some list) is True if there is at least one element that evaluates to True in the list
So any(c.isdigit() for c in flyTo) evaluates to True is at least one character of flyTo is a digit.

A different way of doing it:

len(flyTo)!=len(flyTo.translate(None,'0123456789'))
oh, i see..

why put c.digit()?

i tried x.digit(), and that worked as well.

can you explain what this is doing?
(Apr-05-2017, 10:56 PM)Sp00f Wrote: [ -> ]i tried x.digit(), and that worked as well.
The name is not important "rubberduck" is now temporarily assigning to each element in string flyTo.
>>> flyTo = 'yes9'
>>> any(rubberduck.isdigit() for rubberduck in flyTo)
True
When say "not important" so is c much better than rubberduck when iterate over characters.
To see each value can use list comprehension.
>>> flyTo = 'yes9'
>>> [rubberduck.isdigit() for rubberduck in flyTo]
[False, False, False, True]
Interactive shell can give info about a lot of stuff.
>>> help(any)
Help on built-in function any in module builtins:

any(...)
    any(iterable) -> bool
    
    Return True if bool(x) is True for any x in the iterable.
    If the iterable is empty, return False.

>>> flyTo = 'yes9'
>>> help(flyTo.isdigit)
Help on built-in function isdigit:

isdigit(...) method of builtins.str instance
    S.isdigit() -> bool
    
    Return True if all characters in S are digits
    and there is at least one character in S, False otherwise. 
Pages: 1 2