Using Loops and Functions
Create an n x n Multiplication table using Nested FOR Loop. The user must enter the number of rows and columns that will be displayed in the Table.
rows = int(input("How many rows: "))
cols = int(input("How many cols: "))
print("----------------- Multiplication Table ---------------------")
for r in range(1, rows + 1):
for c in range(1, cols + 1):
print(f"{r * c:5d}", end="")
print()
How many rows: 10
How many cols: 10
----------------- Multiplication Table ---------------------
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
How many rows: 5
How many cols: 5
----------------- Multiplication Table ---------------------
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Create a bank program that will allow the user to perform the following: Use Functions as necessary.
def show_balance(balance):
print("********************************")
print(f"Your balance is ${balance:.2f}")
print("********************************")
def deposit(balance):
amount = float(input("Enter an amount to be deposited: "))
if amount > 0:
balance += amount
else:
print("Invalid amount!")
return balance
def withdraw(balance):
amount = float(input("Enter an amount to be withdrawn: "))
if amount > balance:
print("Insufficient funds!!!")
elif amount <= 0:
print("Invalid amount! Amount must be greater than 0.")
else:
balance -= amount
return balance
def main():
balance = 0
while True:
print("********************")
print(" XYZ ATM ")
print("********************")
print("1. Show Balance")
print("2. Deposit")
print("3. Withdraw")
print("4. Exit")
print("********************")
choice = int(input("Enter your choice (1-4): "))
if choice == 1:
show_balance(balance)
elif choice == 2:
balance = deposit(balance)
elif choice == 3:
balance = withdraw(balance)
elif choice == 4:
print("Exiting Program.")
break
else:
print("*************************************")
print("Invalid input! Select a valid option.")
print("*************************************")
print("*************************************")
print(" Thank you for using XYZ ATM ")
print("*************************************")
main()
********************
XYZ ATM
********************
1. Show Balance
2. Deposit
3. Withdraw
4. Exit
********************
Enter your choice (1-4): 1
********************************
Your balance is $0.00
********************************
********************
XYZ ATM
********************
1. Show Balance
2. Deposit
3. Withdraw
4. Exit
********************
Enter your choice (1-4): 2
Enter an amount to be deposited: 1000
********************
XYZ ATM
********************
1. Show Balance
2. Deposit
3. Withdraw
4. Exit
********************
Enter your choice (1-4): 1
********************************
Your balance is $1000.00
********************************
********************
XYZ ATM
********************
1. Show Balance
2. Deposit
3. Withdraw
4. Exit
********************
Enter your choice (1-4): 3
Enter an amount to be withdrawn: 350
********************
XYZ ATM
********************
1. Show Balance
2. Deposit
3. Withdraw
4. Exit
********************
Enter your choice (1-4): 1
********************************
Your balance is $650.00
********************************
********************
XYZ ATM
********************
1. Show Balance
2. Deposit
3. Withdraw
4. Exit
********************
Enter your choice (1-4): 4
Exiting Program.
*************************************
Thank you for using XYZ ATM
*************************************