How do I replicate this try/except for all other inputs in the code?

Viewed 49

How do I replicate the same try/except you see for the first input in every other input?

import math
import sys
import time

while True: 
    try:
        Money = float(input("How much are you depositing?"))
    except:
        print("please enter numbers")
        continue         
    AIR = float(input("What is the annual interest rate?"))
    #Annual Interest Rate
    n = int(input("How many times a year do you want the interst compounded for?"))
    # number of times the interst compounds
    t = int(input("How many years are you investing/borrowing the money?"))
    # total number of years the money is being invested/borrowed for
    CCI = Money *(1+AIR/n)**(n*t)
    # Calculated Compound Interest
    print(math.trunc(CCI));
    # main program
3 Answers

Extract that logic into a function which you can then reuse:

def read(type_, prompt):  # using "type_" not to shadow built-in "type"
    while True:
        try:
            return type_(input(prompt))
        except (TypeError, ValueError):
            print("Please enter a {}".format(type_.__name__))

Money = read(float, "How much are you depositing?\n")
AIR = read(float, "What is the annual interest rate?\n")
n = read(int, "How many times a year do you want the interst compounded for?\n")
t = read(int, "How many years are you investing/borrowing the money?\n")

CCI = Money *(1+AIR/n)**(n*t)
print(math.trunc(CCI))

you can add them in the same try-except or write multiple times. if you do it in same you will be having the same error message but If you write multiple try-except then you can have different error messages

import math
import sys
import time

while True: 
            try:
                Money = float(input("How much are you depositing?"))
                n = int(input("How many times a year do you want the interst compounded for?"))
                # number of times the interst compounds
                t = int(input("How many years are you investing/borrowing the money?"))
 
                AIR = float(input("What is the annual interest rate?"))
                #Annual Interest Rate
            
           
            except:
                print("please enter numbers")
                continue         
            # total number of years the money is being invested/borrowed for
            CCI = Money *(1+AIR/n)**(n*t)
            # Calculated Compound Interest
            print(math.trunc(CCI));
            # main program

You can do something like this:

app.py

while True: 
        try:
            Money = float(input("How much are you depositing?"))
            AIR = float(input("What is the annual interest rate?"))
            #Annual Interest Rate
            n = int(input("How many times a year do you want the interst compounded for?"))
            # number of times the interst compounds
            t = int(input("How many years are you investing/borrowing the money?")) 
            # total number of years the money is being invested/borrowed for
            CCI = Money *(1+AIR/n)**(n*t)
            # Calculated Compound Interest
            print(math.trunc(CCI));
            # main program
            break
        except:
            print("please enter numbers")money?"))
        

The break tells python to get out of the loop

so if the code reaches break it means that it didn't throw any exceptions and that it is indeed a number

You put all your calculations in the try block so that it throws an exception when there's a mistake

Related