Задача 6 от Лаб-а
Едно трето решение, което също дава 100 точки в джъдж:
num = int(input())
if num < 100:
print('Less than 100')
elif num <= 200:
print('Between 100 and 200')
else:
print('Greater than 200')
Едно трето решение, което също дава 100 точки в джъдж:
num = int(input())
if num < 100:
print('Less than 100')
elif num <= 200:
print('Between 100 and 200')
else:
print('Greater than 200')
Колеги, разцъквам и гледам, че при мене е възможно на една променлива да се присвои стойност в if / else конструкция и после да се ползва извън конструкцията.
Примерно кода по-долу работи. На discount се присвоява стойнст и после се ползва след конструкцията. Тъй като и в давата случая на discount ще се присвои някаква стойност, това не създава проблем за по-нататък
total_numer_toys = float(input('Enter amount: '))
if total_numer_toys >= 100:
discount = 0.75
else:
discount = 1
total_numer_toys = total_numer_toys * discount
print('Discount applied', discount)
print(total_numer_toys)
Един по-добър пример може би. Този код по-долу си ми работи без да съм дефинирал area горе над if-овете. И после си печата area извън if-овете
import math type = input() if type == ('square'): side = float(input()) area = side * side elif type == ('rectangle'): side1 = float(input ()) side2 = float(input()) area = side1 * side2 elif type == ('circle'): r = float(input()) area = + math.pi * r * r elif type == ('triangle'): side = float(input()) h = float(input()) area = side * h / 2print("%.3f" % area)