on writing a while loop for rolling two dice

David Raymond David.Raymond at tomtom.com
Mon Aug 30 09:11:53 EDT 2021


> def how_many_times():
>   x, y = 0, 1
>   c = 0
>   while x != y:
>     c = c + 1
>     x, y = roll()
>   return c, (x, y)

Since I haven't seen it used in answers yet, here's another option using our new walrus operator

def how_many_times():
    roll_count = 1
    while (rolls := roll())[0] != rolls[1]:
        roll_count += 1
    return (roll_count, rolls)


More information about the Python-list mailing list