I'm fairly new to programming and I'm learning python. I've defined a function for a dice roll and set it up to roll multiple.

def d20(num):
    rolls = 0
    for i in range(num):
        rolls += randint(1,20)
    return rolls

Thonny and my assistant tab gives me a warning:

Line 3: Unused variable 'i'

The code works fine but I like a clean workspace. I was wondering if there is an alternate way to write the range loop or is this just something I need to learn to deal with?

  • 4
    \$\begingroup\$ return sum(randint(1, 20) for _ in range(num)) \$\endgroup\$ Oct 5 '20 at 23:27
  • 1
    \$\begingroup\$ I'd turn the thole thing in a roll_dice(num, dice) function that takes d20 as an argument as well. \$\endgroup\$
    – Mast
    Oct 6 '20 at 3:56

Use an underscore _ instead of i


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.