как заменить комментарий в следующем коде циклом while в python - PullRequest
1 голос
/ 11 марта 2019

как заменить комментарий в следующем коде циклом while в python.

Комментарий: объединить X в toPrint numXs раз

numXs = int(input('How many times should I print the letter X? '))
toPrint = ''
if numXs == 1:
    toPrint = 'X'
elif numXs == 2:
    toPrint = 'XX'
elif numXs == 3:
    toPrint = 'XXX'
print(toPrint)

1 Ответ

1 голос
/ 11 марта 2019

Вы можете уменьшить numXs для каждой итерации, пока она не станет больше 0:

numXs = int(input('How many times should I print the letter X? '))
toPrint = ''
while numXs > 0:
    toPrint += 'X'
    numXs -= 1
print(toPrint)
...