Created
June 21, 2025 21:28
-
-
Save AlonaB1997/ec0913a71ab63fd70fc3b408be740348 to your computer and use it in GitHub Desktop.
Python HW_8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Завдання 1 | |
# Користувач вводить із клавіатури два числа. | |
# Потрібно порахувати окремо суму парних, непарних і чисел, | |
# кратних 9 у вказаному діапазоні, а також середньоарифметичне кожної групи. | |
num1=int(input("Enter number #1: ")) | |
num2=int(input("Enter number #2: ")) | |
sumEven=0 | |
sumOdd=0 | |
sum9=0 | |
countEven=0 | |
countOdd=0 | |
count9=0 | |
while num1<=num2: | |
if num1%2==0: | |
sumEven+=num1 | |
countEven+=1 | |
elif num1%2!=0: | |
sumOdd+=num1 | |
countOdd+=1 | |
if num1%9==0: | |
sum9+=num1 | |
count9+=1 | |
num1+=1 | |
print(f"Sum even: {sumEven}", f"Sum odd: {sumOdd}", f"Sum9: {sum9}", sep="\n") | |
if countEven!=0 and countOdd!=0 and count9!=0: | |
print(f"Average even: {sumEven/countEven}") | |
print(f"Average odd: {sumOdd/countOdd}") | |
print(f"Average 9s: {sum9/count9}") | |
else: | |
print("Enter bigger range!") | |
# Завдання 2 | |
# Користувач вводить з клавіатури довжину лінії та символ | |
# для заповнення лінії. | |
# Потрібно відобразити на екрані вертикальну лінію із введеного символу, | |
# зазначеної довжини. | |
length=int(input("Enter length of the line: ")) | |
symbol=input("Enter symbol of the line: ") | |
i=1 | |
while i<=length: | |
print(symbol) | |
i+=1 | |
# Завдання 3 | |
# Користувач вводить із клавіатури числа. | |
# Якщо число більше нуля потрібно вивести напис "Number is positive", | |
# якщо менше нуля "Number is negative", | |
# якщо дорівнює нулю "Number is equal to zero". | |
# Коли користувач вводить число 7 програма припиняє свою роботу | |
# і виводить на екран напис "Good bye!". | |
num=None | |
while True: | |
num=float(input("Enter number: ")) | |
if num==7: | |
print("Good bye!") | |
break | |
elif num==0: | |
print("Number is equal to zero") | |
elif num>0: | |
print("Number is positive") | |
else: | |
print("Number is negative") | |
# Завдання 4 | |
# Користувач вводить із клавіатури числа. | |
# Програма повинна підраховувати суму, максимум і мінімум, введених чисел. | |
# Коли користувач вводить число 7 програма припиняє свою роботу | |
# і виводить на екран напис "Good bye!". | |
# Коментар від Альони: програма розраховує результати без урахування 7. | |
number=float(input("Enter number: ")) | |
max=number | |
min=number | |
sum=0 | |
while True: | |
if number==7: | |
print("Good bye!") | |
break | |
sum+=number | |
number=float(input("Enter number number: ")) | |
if number==7: | |
print("Good bye!") | |
break | |
if max<number: | |
max=number | |
if min>number: | |
min=number | |
print(f"Max: {max}, min: {min}, sum: {sum}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment