Minecraft PC IP: play.cubecraft.net

Trickmaster

Dedicated Member
May 23, 2014
1,175
958
188
Potato Heaven
Which function definition body is better/more likely to be used, even though they do the same task?

def count_to_first_vowel(s):
''' (str) -> str
Return the substring of s up to but not including the first vowel in s. If no vowel
is present, return s.
>>> count_to_first_vowel('hello')
'h'
>>> count_to_first_vowel('cherry')
'ch'
>>> count_to_first_vowel('xyz')
xyz
'''
substring = ''
for char in s:
if char in 'aeiouAEIOU':
return substring
substring = substring + char
return substring


or

def count_to_first_vowel(s):
''' (str) -> str
Return the substring of s up to but not including the first vowel in s. If no vowel
is present, return s.
>>> count_to_first_vowel('hello')
'h'
>>> count_to_first_vowel('cherry')
'ch'
>>> count_to_first_vowel('xyz')
xyz
'''
substring = ''
i = 0
while i < len(s) and not s in 'aeiouAEIOU':
substring = substring + s
i = i + 1
return substring


@alyphen @repository
 

Trickmaster

Dedicated Member
May 23, 2014
1,175
958
188
Potato Heaven
Code:
def count_to_first_vowel(s):
    '''  (str) -> str
    Return the substring of s up to but not including the first vowel in s. If no vowel
    is present, return s.
    >>> count_to_first_vowel('hello')
    'h'
    >>> count_to_first_vowel('cherry')
    'ch'
    >>> count_to_first_vowel('xyz')
    xyz
    '''
    substring = ''
    for char in s:
        if char in 'aeiouAEIOU':
            return substring
        substring = substring + char
    return substring

or

def count_to_first_vowel(s):
    '''  (str) -> str
    Return the substring of s up to but not including the first vowel in s. If no vowel
    is present, return s.
    >>> count_to_first_vowel('hello')
    'h'
    >>> count_to_first_vowel('cherry')
    'ch'
    >>> count_to_first_vowel('xyz')
    xyz
    '''
    substring = ''
    i = 0
    while i < len(s) and not s[i] in 'aeiouAEIOU':
        substring = substring + s
        i = i + 1
    return substring
 

not2excel

Well-Known Member
Jun 24, 2015
35
28
98
30
the main differences between the standard for (fori) loop and while loop are the reason you use them. If you know beforehand how many times you wish to loop, eg. the number of indices in a collection or just say 10 times. While loops are best for condition checking in which the condition could be false at any point in time over the course of the loop dispatchment
 
  • Like
Reactions: Trickmaster

alyphen

Well-Known Member
Jan 4, 2014
101
109
118
27
127.0.0.1
seventh-root.com
So, you would use a for loop in this situation? How could you control it so the function only iterates 10 times in a for loop?
In this situation you would use a for loop, since you're running a predetermined number of iterations (based on the string length)
If you are finding out how many iterations later (based on a condition), that is when you use the while loop.
This is pretty much what @not2excel outlined, but I hope it makes it clearer.
 

NanoNet

Dedicated Member
May 23, 2014
1,044
357
158
IKEA
alyphen gave the perfect answer...! But if your looking for a quicker, and easier way to get your questions answered, go to www.stackoverflow.com. If you post a question there, you will get an answer within minutes. It's helped me a ton in my adventure of learning basic knowledge to some higher level stuff :P
 
  • Like
Reactions: Trickmaster
Members Online

Team online

Latest profile posts

𝑷𝑹𝑶 𝑾𝑰𝑵𝑵𝑨𝑨𝑹 wrote on L1kii's profile.
👀
qKhalidd wrote on ignsinf's profile.
Hi
TheOrderOfSapphire wrote on ii6xxq's profile.
welcome to the forums! I hope you will have a nice stay here:D
This is YOUR daily dose of facts #41-
The moon has moonquakes, which are kind of like the earthquakes on the Earth, but weaker.
Basketman wrote on Eli's profile.
Favourite how to tame a dragon movie, shoot.
Top Bottom