Python Forum
Assistance with making functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assistance with making functions
#4
@bobfat that's different from ichabod's suggestion because ichabod's will short-circuit. You see how you complete the whole loop even if the first character is a digit? What you can do instead is to just return True within your if, and return False if the loop exits without the inner return.

Also, this looks like homework, so even though the OP says it isn't, I'd recommend not providing a full function to do what they're looking for, unless it's trivial...

And if the OP isn't doing this as homework, and they can use whatever module they want, they can have a one-liner:
import re

def contains_digit(string):
   return re.match(r'.*\d', string) is not None
In use:
Output:
>>> contains_digit('abc 123') True >>> contains_digit('abc one two three') False
Reply


Messages In This Thread
Assistance with making functions - by edwdas - Nov-04-2019, 04:03 PM
RE: Assistance with making functions - by bobfat - Nov-04-2019, 05:30 PM
RE: Assistance with making functions - by micseydel - Nov-14-2019, 07:37 PM
RE: Assistance with making functions - by bobfat - Nov-23-2019, 08:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling functions by making part of their name with variable crouzilles 4 898 Nov-02-2023, 12:25 PM
Last Post: noisefloor

Forum Jump:

User Panel Messages

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