Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Control Flow in Python - Week 3 Summary

Tech 1
## 5. Using while Loops

print("-"*10+"Check if class is needed"+"-"*10)
ans=input("Is there a class today? y/n")
while ans=="y":
    print("Class is required")
    ans=input("Is there a class today? y/n")

print("-"*10+"Sum of 1 to 100"+"-"*10)
s=0
i=1
while i<=100:
    s+=i
    i+=1
print(s)

6. Simulating User Login with while Loop

i=0 while i<3: user_name=input("Enter your username:") pwd=input("Enter your password") if user_name=="ty" and pwd=="666666": print("Logging in...") i=4 else: if i<=2: print("Username or password incorrect, you have ",2-i," attempts left") i+=1

if i==3: print("All three login attempts failed")

## 7. Nested Loops for Printing Shapes

print("-"*10+"Print a 3x4 matrix"+"-"*10)
for i in range(1,4):
    for j in range(1,5):
        print("*",end="")
    print()

print("-"*10+"Print a right triangle with 5 rows"+"-"*10)
for i in range(1,6):
    for j in range(1,i+1):
        print("*",end="")
    print()

print("-"*10+"Print an inverted right triangle with 5 rows"+"-"*10)
for i in range(1,6):
    for j in range(1,7-i):
        print("*",end="")
    print()

print("-"*10+"Print an inverted isosceles triangle with 5 rows"+"-"*10)
for i in range(1,6):
    for j in range(1,6-i):
        print(" ",end="")
    for k in range(1,i*2):
        print("*",end="")
    print()

8. Nested Loops for Printing a Diamond

row=eval(input("Enter the number of rows:")) while row%2==0: print("Please enter an odd number!") row = eval(input("Enter the number of rows:")) top_row=(row+1)//2

for i in range(1,top_row+1): for j in range(1,top_row+1-i): print(" ",end="") for k in range(1,i2): print("",end="") print()

bottom_row=row//2 for i in range(1,bottom_row+1): for j in range(1,i+1): print(" ",end="") for k in range(1,2bottom_row-i2+2): print("*",end="") print()


## 9. Nested Loops for Printing an Hollow Diamond

row=eval(input("Enter the number of rows:"))
while row%2==0:
    print("Please enter an odd number!")
    row = eval(input("Enter the number of rows:"))
top_row=(row+1)//2

for i in range(1,top_row+1):
    for j in range(1,top_row+1-i):
        print(" ",end="")
    for k in range(1,i*2):
        if k<1 or k>i*2-1:
            print("*", end="")
        else:
            print(" ", end="")
    print()

bottom_row=row//2
for i in range(1,bottom_row+1):
    for j in range(1,i+1):
        print(" ",end="")
    for k in range(1,2*bottom_row-i*2+2):
        if k<1 or k<2*bottom_row-i*2+2-1:
            print("*", end="")
        else:
            print(" ", end="")
    print()

10. Using break and contiune

break

s=0 i=0 while i<20: s+=i if s>100: print("The current number when sum exceeds 100 is",i) break i+=1

s=0 for i in range(1,20): s+=i if s>100: break print("The current number when sum exceeds 100 is",i)

continue

i=0 s=0 while i<100: if i%2==1: i+=1 continue s+=i i+=1

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.