Python Forum
checking if there are numbers in the string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
checking if there are numbers in the string
#1
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
Reply
#2
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.
Reply
#3
(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
Reply
#4
>>> x='test'
>>> x==x.isdigit()
False
>>> x==x.isalpha()
False
>>> x.isalpha()
True
>>> y='123'
>>> y.isdigit()
True
Reply
#5
(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 "??"
Reply
#6
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
Reply
#7
(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
Reply
#8
(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'))
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#9
oh, i see..

why put c.digit()?

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

can you explain what this is doing?
Reply
#10
(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. 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pulling Specifics Words/Numbers from String bigpapa 2 766 May-01-2023, 07:22 PM
Last Post: bigpapa
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 1,542 Apr-12-2023, 10:39 AM
Last Post: jefsummers
  Checking if a string contains all or any elements of a list k1llcod3 1 1,108 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  Find and Replace numbers in String giddyhead 2 1,244 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  Finding line numbers starting with certain string Sutsro 3 2,558 Jun-27-2020, 12:36 PM
Last Post: Yoriz
  checking a string for two instances of .. Skaperen 2 1,595 May-13-2020, 09:58 PM
Last Post: Skaperen
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,742 May-09-2019, 12:19 PM
Last Post: Pleiades
  String format checking? MuntyScruntfundle 1 2,437 Mar-06-2019, 12:01 PM
Last Post: buran
  Extract numbers from string ian 1 2,825 Apr-28-2018, 10:40 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020