2024-05-16 05:22:11
  • Hello!
  • Whats ya doin?
  • ByeBye

[&]

18.220.163.91

Instrukcje sterujące: IF, IF .. ELSE, SWITCH

workshop: #lesson-3.html

Warsztat: 2

Przygotowanie

  1. Upewnij się, że masz zainstalowany interpreter Python 3. Jeżeli nie, skonsultuj się z prowadzacym
  2. W katalogu domowym przygotuj katalog AD, a w nim mod2
  3. Twórz po kolei pliki skryptów z rozszerzeniem py dla każdego zadania z tego warsztatu wg. wzorca: app-2-1.py, app-2-2.py itp.
  4. Wywołuj skrypty w terminalu
2.1 Wprowadzanie danych i obliczenia
# Chapter: 2.1 [8]
# Subject: calculations
# Time:	3 minutes
# Go!
num1 = input("Enter A: ")
num2 = input("Enter B: ")
result = float(num1) + float(num2)
# int() # float()
print("Calculations")
print("Result "+str(result))
2.2 Instrukcje warunkowe (proste)
# Chapter: 2.2 [9]
# Subject: conditions
# Time:	3 minutes
# Go!
key = input("Press ANY key to continue...")
if key=="":
	print("Nothing pressed")
else:
	print("You entered: "+key)
	print(ord(key))

print("Now the world will end!")
2.3 Instrukcje warunkowe (rozszerzone)
# Chapter: 2.3 [10]
# Subject: multi-conditions
# Time:	8 minutes
# Go!
print("Welcome in MILIONERZY")

print("Q1: What means GUI?")
print("A: Graphical User Intuition?")
print("B: Graphical Used Interface?")
print("C: Generic User Interface?")
print("D: Graphical User Interface?")
key = input("Press answer key to choose: ")
key = key.lower()
if key=="":
	print("\nNothing pressed")
elif key=="a":
	print("Are you sure?")
elif key=="b":
	print("Almoste")
elif key=="c":
	print("Right!")
elif key=="d":
	print("Really, Really")
	
print("Your answear is: "+key)
print("QUIZ ended for you!")
2.4 Instrukcja Switch
# Chapter: 2.4 [11]
# Subject: switch
# Time:	1 minutes
# Go!
print("Welcome in MILIONERZY")

print("Q1: What means GUI?")
print("A: Graphical User Intuition?")
print("B: Graphical Used Interface?")
print("C: Generic User Interface?")
print("D: Graphical User Interface?")
key = input("Press answer key to choose: ")
key = key.lower()
if key=="":
	print("Nothing pressed")
elif key=="a":
	print("Are you sure?")
elif key=="b":
	print("Almoste")
elif key=="c":
	print("Right!")
elif key=="d":
	print("Really, Really")
	
print("Your answear is: "+key)
print("QUIZ ended for you!")
2.5 Instrukcje warunkowe zagnieżdżone
# Chapter: 2.5 [12]
# Subject: inner IFs
# Time:	12 minutes
# Go!
print("Operator")
print("1 Informacje o produktach")
print("2 Zgłoszenie techniczne")
print("3 Płatności i faktury")
print("4 Klienci korporacyjni")
print("0 Powtórz menu")

a = input("Choose: ")
if a=="1":
	print("Informacje o produktach")
	print("1 Domeny")
	print("2 Hosting")
	print("3 Poczta e-mail")
	print("4 Serwery dedykowane i VPS")
	print("9 Powrót do menu")
	a = input("Choose: ")
	if a=="1":
		print("Wybrałeś informacje o Domenach")
	elif a=="2":
		print("Dostępne oferty Hostingu")
	elif a=="3":
		print("Usługi pocztowe")
	elif a=="4":
		print("Wykup serwer")
elif a=="2":
	print("Zgłoszenie techniczne")
	print("1 Poczta")
	print("2 Domeny i WWW")
	a = input("Choose: ")
	if a=="1":
		print("Łączę ze specjalistą Gołębiem pocztowym")
	elif a=="2":
		print("Łączę ze specjalistycznymi Małpami naprawczymi")
else:
	print("Niezgodny wybór")
	
2.6 Imitacja instrukcji Switch
# Chapter: 2.6 [13]
# Subject: switch imitation
# Time:	6 minutes
# Go!
def switch(args):
	switcher = {
		0: "zero",
		1: "one",
		2: "two"
	}
	return switcher.get(args,"default")
	#return "Jestem Switchem #"+str(args)
print("#1 switch")
print(switch(1))

def two():
	return "TWO"

def apok(args=2):
	return {
		0: "zero",
		1: "one",
		2: two()
	}.get(args)	# .get(args,"default")

print("#2 switch #1 run")
print(apok())
print("#2 switch #2 run")
print(apok(1))

2.7 Wiele instrukcji IF
# Chapter: 2.7 [14]
# Subject: multi IFs
# Time:	8 minutes
# Go!
public = True
close = False
form = None
if public==True and close!=False and form!=None:
	print("Formularz rejestracji")
else:
	if form==None:
		print("Brak formularza")
	if close==True:
		print("Rejestracja zamknięta")
	if public==False:
		print("Formularz nieopublikowany")

Podsumowanie

  1. Jak budować bloki warunków IF w języku Python? Jak rozgraniczane są te warunki od siebie?
  2. Jak wygląda instrukcja Switch w języku Python?