Yuklanmoqda...

6-Amaliy: Pythonda takrorlanuvchi jarayonlarni dasturlash.

For tsikllari berilgan ketma-ketlikda takrorlanadi. Mana bir misol:
primes = [2, 3, 5, 7] for prime in primes: print(prime) success_msg("To'g'ri javob!")
For tsikllari "diapazon" va "xrange" funksiyalaridan foydalangan holda raqamlar ketma-ketligini takrorlashi mumkin. Rang va xrange o'rtasidagi farq shundaki, diapazon funktsiyasi o'sha belgilangan diapazon raqamlari bilan yangi ro'yxatni qaytaradi, xrange
# Prints out the numbers 0,1,2,3,4 for x in range(5): print(x) # Prints out 3,4,5 for x in range(3, 6): print(x) # Prints out 3,5,7 for x in range(3, 8, 2): print(x) success_msg("To'g'ri javob!")
Ma'lum bir mantiqiy shart bajarilsa, while tsikllari takrorlanadi. Masalan:
# Prints out 0,1,2,3,4 count = 0 while count < 5: print(count) count += 1 # This is the same as count = count + 1 success_msg("To'g'ri javob!")
break for yoki while tsiklidan chiqish uchun ishlatiladi, davom esa joriy blokni o'tkazib yuborish va "for" yoki "while" ga qaytish uchun ishlatiladi. Bir necha misol:
# Prints out 0,1,2,3,4 count = 0 while True: print(count) count += 1 if count >= 5: break # Prints out only odd numbers - 1,3,5,7,9 for x in range(10): # Check if x is even if x % 2 == 0: continue print(x) success_msg("To'g'ri javob!")
C,CPP kabi tillardan farqli o'laroq, biz else for looplaridan foydalanishimiz mumkin. “For” yoki “while” operatorining sikl sharti bajarilmasa, “else”dagi kod qismi bajariladi. Agar for tsikli ichida break operatori bajarilsa, "else" qismi o'tkazib yubori
# Prints out 0,1,2,3,4 and then it prints "count value reached 5" count=0 while(count<5): print(count) count +=1 else: print("count value reached %d" %(count)) # Prints out 1,2,3,4 for i in range(1, 10): if(i%5==0): break print(i) else: print("this is not printed because for loop is terminated because of break but not due to fail in condition") success_msg("To'g'ri javob!")
Raqamlar ro'yxatidagi barcha juft raqamlarni qabul qilingan tartibda aylantiring va chop eting. Ketma-ketlikda 237 dan keyin keladigan raqamlarni chop qilmang.
numbers = [ 951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544, 615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941, 386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345, 399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217, 815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717, 958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470, 743, 527 ] # your code goes here for number in numbers: numbers = [ 951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544, 615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941, 386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345, 399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217, 815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717, 958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470, 743, 527 ] # your code goes here for number in numbers: if number == 237: break if number % 2 == 1: continue print(number) test_object("numbers") success_msg("To'g'ri javob!")