Logic Building in Python

Logic building in Python
Python is among the most popular programming languages at this time. It is a fact that if you want to learn programming, the most important thing is to build your logic. Some people find problems in getting logic building programs. So, here are a few of my personal logic building programs you can see and build you rlogic. First, read the top comments (starting with # symbol at the top of programs). They tell what the below lines are going to do. Try to build your own logic after reading those comments. Then you can read my code to get some extra information. So, below are the programs you can have a look.

# program 1: that finds whether the entered no. is 1/2/3 digits
a = int(input("Enter a no.: "))

if a < 0 or a > 999:
    print("Please enter a no. in range 1 to 999")
else:
    if a<10:
        print("The no. is a 1-digit number")
    elif a<100:
        print("The no. is a 2-digit number")
    elif a<1000:
        print("The no. is a 3-digit number")
        
        
# program 2: ask user to input numbers until entered 'done', then print the sum of all entered numbers.
c=0

while True:
    a = input("Enter a no.: ")
    if a == 'done':
        print(c)
        break
    elif a != 'done' and a.isnumeric() != True:
        print("invalid key command")
        break
    else:
        c += int(a)


# Program 3: ask the day no. and the first day of a year. Then print the day on the day no. provided
_day = int(input("Enter the day no. from 2 to 365: "))
day_n = str(input("Enter the first day of that year: "))
day_list = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]

if day_n in day_list:
    a = _day % 7
    for i in range(0, 7):
        day_list_i = day_list[i]
        if day_list_i == day_n:
            for j in range (i, i+7):
                day_var = day_list[(a+j)%7]
            print(day_var)
else:
    print("Please don't try to fool the program :')")


# Program 4: miles to k.m. conversion table

print("miles\t|\tk.m.")
for data in range(1, 11):
    print(f"{data}\t|\t{data * 1.609}")
    

# Program 5: Print the stats of entered string (like total lowercase/uppercase characters adn no. of alphabets and numbers)
l = input("Enter a string: ")
lowercount = uppercount = alphabet = numbers = 0
for a in l:
    if a.islower():
        lowercount += 1
    if a.isupper():
        uppercount += 1
    if a.isalpha():
        alphabet += 1
    if a.isnumeric():
        numbers += 1

print(f"Lowercase: {lowercount}\nUppercase: {uppercount}\nAlphabets: {alphabet}\nNumbers: {numbers}")



So, I think that's it for this post. If you want to learn react follow the above playlist. See you in the next post.

2 Comments