Saturday, November 20, 2021

Python - for loop with inflation as an example for 10 years

#for loop 11/20
#print numbers from 1 to 10
#i=0
#for i in range(1):
# print(i)

# Find inflation based for an amount assuming a percentage of 8%
noOfYears=10
print('Enter inflation percentage')
inflationPercentage=float(input())
print ('Inflation Percentage is :'+str(inflationPercentage))
print('Enter amount ')
amount=float(input())
totalAmount=float(amount)
i=0
for i in range(noOfYears):
totalAmount=totalAmount+(float(totalAmount)*(inflationPercentage/100))
xTimes=int(totalAmount / amount)
#print(xTimes)
#print(totalAmount)

if xTimes==2:
print('Amount is 2x')
elif xTimes==3:
print('Amount is 3x ')
elif xTimes== 4:
print('Amount is 4x')
else:
print('Amount hasn\'t doubled yet')

print(str(totalAmount)+' after '+str(i)+' year')
print('Total inflated amount after '+str(noOfYears)+' \n for amount : '+str(amount)+' is : '+str(totalAmount))

---

Output

Enter inflation percentage

Enter inflation percentage

8

Inflation Percentage is :8.0

Enter amount

20000

Amount is 2x

43178.49994545574 after 9 year

Total inflated amount after 10

for amount : 20000.0 is : 43178.49994545574

Friday, November 19, 2021

Python WHILE CONTINUE and BREAK Example

# CONTINUE to allow user to withdraw and display balance until value 0 is used to exit(BREAK)

 balance=10000;
while True:
    print('****Continue example *****')
    print('Enter amount to withdraw :')
    val=input()
    if(int(val)<0):
        continue
    elif(int(val)==0):
        print('Exiting program')
        break
    else:
        balance=balance-int(val)
        print('balance is now :'+str(balance))

print('****End of balance****')

Python IF ELSE WHILE BREAK

Breaks out of the loop

 while True:
    print('***** BEGIN ******')
    print('Program to find if the person is a Minor, Teenager or Adult.')
    print('Enter a value :')
    age=int(input())
    
    if(age>0 and age<13):
        print('Participant is a Child')
    elif(age>13 and age <18):
        print('Participant is a Teenager')
    elif(age>18):
        print('Participant is an Adult')
    elif(age==0):
        break;

print('*** EXIT END OF PROGRAM *****')

Python Flow statements IF ELSE ELIF

 print('***** BEGIN ******')
    print('Program to find if the person is a Minor, Teenger or Adult.')
    print('Enter a value :')
    age=int(input())
    
    if(age>0 and age<13):
        print('Participant is a Child')
    elif(age>13 and age <18):
        print('Participant is a Teenager')
    elif(age>18):
        print('Participant is an Adult')
    elif(age==0):
        break;

Tuesday, November 16, 2021

Unix shell variables parameters and loops

 The following site is a good link for someone who is aware of programming but wants a quick insight into the shell constructs of programming

https://www.baeldung.com/linux/use-command-line-arguments-in-bash-script