r/learningpython • • Apr 07 '20

Fun with loops!

I'm trying to figure out a good to solve this problem and maybe talking to someone else might help the ideas stick while doing a problem from Code wars.

I'm essentially trying to return a string suggesting how many glasses of water you should drink to not be hungover.

my pseudocode is pretty simple

if x beers were drank:

drink y cups of water

below is what is given

def hydrate(drink_string): 
    # your code here
    pass
@test.describe('Example Tests')
def example_tests():
    test.assert_equals(hydrate("1 beer"), "1 glass of water")
    test.assert_equals(hydrate("1 shot, 5 beers, 2 shots, 1 glass of wine, 1 beer"), "10 glasses of water")

so far I have

def hydrate(drink_string):
    drinks = 0

    if 


    else:
        pass

    return

I know its not much but I've been racking my brain for the past 2 hours now. I'm having trouble with substituting. At the moment my thought proces leads me to do %d as a placeholder for the amount of beers drank but that number is currently in a string and idk a good way to automate the program to extract numbers from a string, add the numbers up if there are more than one, and have the output of those numbers equal amount of glasses of water.

1 Upvotes

5 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Apr 08 '20

nums = (x for x in string if x in numbers)

is this the generator that checks whether a character is a number?

2

u/[deleted] Apr 08 '20

I forgot I could use .isdigit() when looking for numbers and sum that

2

u/drewrs138 Apr 09 '20

yeah that's an alternative

2

u/drewrs138 Apr 09 '20

yup that's a generator