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
#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


Messages In This Thread
RE: checking if there are numbers in the string - by Sp00f - Apr-05-2017, 08:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pulling Specifics Words/Numbers from String bigpapa 2 819 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,608 Apr-12-2023, 10:39 AM
Last Post: jefsummers
  Checking if a string contains all or any elements of a list k1llcod3 1 1,180 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  Find and Replace numbers in String giddyhead 2 1,288 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  Finding line numbers starting with certain string Sutsro 3 2,602 Jun-27-2020, 12:36 PM
Last Post: Yoriz
  checking a string for two instances of .. Skaperen 2 1,625 May-13-2020, 09:58 PM
Last Post: Skaperen
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,795 May-09-2019, 12:19 PM
Last Post: Pleiades
  String format checking? MuntyScruntfundle 1 2,466 Mar-06-2019, 12:01 PM
Last Post: buran
  Extract numbers from string ian 1 2,845 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