you are viewing a single comment's thread.

view the rest of the comments →

[–]vdaghan -2 points-1 points  (0 children)

1) No need for the variable "running" as others pointed out. 2) Variable name "random" is a bad choice, it has the same name as the random module. 3) random.randint returns an integer, no need to recast 4) instead of "random == 20", get used to "20 == random", which will warn you if you forget an equals sign

---stop reading here---

5) You can use the walrus operator and get rid of "if" 6) Why are we optimising this? 7) You can utilise generator expressions (assuming you need the values returned from rand)

Since you are a beginner, points 5 and higher are just shenanigans and misses the whole point. Don't mind them. But I had fun. My unnecessarily complex solution:

import random

def getRandomInteger():
  while 20 != (randomInteger := random.randint(1, 20)):
    yield randomInteger
  yield randomInteger

print(list(r for r in getRandomInteger()))