G
G
godwell2021-11-30 04:43:15
Python
godwell, 2021-11-30 04:43:15

So did you do how to remove a lot of repetitions in a loop?

"""
The user enters two numbers from the keyboard (the beginning and the end of the range).
It is required to analyze all the numbers in this range.
The display must follow the rules indicated below.
If the number is a multiple of 3 (divided by 3 without a remainder), you need to output the word Fizz If the
number is a multiple of 5, print the word Buzz
If the number is a multiple of 3 and 5, print the Fizz Buzz
If the number is not a multiple of 3 and 5, print the number itself
.

try:
    # 1
    print('Введите два числа (начало и конец диапазона): ')
    a = int(input('начало диапазона = '))
    b = int(input('конец диапазона = '))
    print()

    if a == b:
        raise RuntimeError('Числа не могут быть одинаковыми')

    # 3
    for i in range(a, b):
        if i % 3 == 0:
            i = 'Fizz'
            print(i, end=' ')
    print()

    for i in range(a, b):
        if i % 5 == 0:
            i = 'Buzz'
            print(i, end=' ')
    print()

    for i in range(a, b):
        if i % 3 == 0 and i % 5 == 0:
            i = 'Fizz Buzz'
            print(i, end=' ')
    print()

    for i in range(a, b):
        if i % 3 != 0 and i % 5 != 0:
            print(i, end=' ')
    print()


except ValueError:
    print('ошибка формата ввода')
except RuntimeError as err:
    print(err)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaratPetrov96, 2021-11-30
@MaratPetrov96

No, it's not like that.
It turns out that the numbers are sorted each time anew.

try:
# 1
print('Введите два числа (начало и конец диапазона): ')
a = int(input('начало диапазона = '))
b = int(input('конец диапазона = '))
print()

if a == b:
raise RuntimeError('Числа не могут быть одинаковыми')

# 3
for i in range(a, b):
    if i % 3 == 0:
        i = 'Fizz'
    elif i % 5 == 0:
        i = 'Buzz'
    elif i % 15 == 0:
        i = 'Fizz Buzz'
    print(i,end='\n\n')

except ValueError:
print('ошибка формата ввода')
except RuntimeError as err:
print(err)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question