Python Forum

Full Version: I am asking for a review of my homework to confirm I completed all steps
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
#assignment instructions create 2 test cases that will trap the errors



# This program is a code of Functions for addition, subtraction, multiplication, and division
def Add():
    print (var1+var2)
def Subtract():
    print (var1-var2)
def Multiply():
    print (var1*var2)
def Divide():
    print (var1/var2)

def main():
    print("Welcome to my calculator")

# The exception below restarts the program if user doesn't enter an interger instead of showing an error.

    try:
                range1 = int(input('Enter Low Range:'))
                range2 = int(input('Enter High Range:'))
                var1 = int(input('Enter Your First Number: '))
                var2 = int(input('Enter Your Second Number: '))

    except ValueError:
                print("Please enter intergers only, re-enter.")
                main()

    if var1 < range1 or var2 > range2 or var1 > range2 or var2 < range1:
                print("Your number is not within the correct range, please try agian")

    else:

                print('{} + {} = '.format(var1, var2))
                print(var1 + var2)
                print('{} - {} = '.format(var1, var2))
                print(var1 - var2)
                print('{} * {} = '.format(var1, var2))
                print(var1 * var2)

#This is an exception to entering "0". By doing so, we will no longer get an error and get to restart the program.

    try:
            print('{} / {} = '.format(var1, var2))
            print(var1 / var2)
    except ZeroDivisionError:
            print("Zero is not allowed")
            again = input("Would you like to calculate again? y/n")
            if again == "y" or \
                again == "Y":
                main()
            else:
                print("Thanks for using my calculator!")

            again = input("Would you like to calculate again? y/n")
        
            if again == "y" or \
                again == "Y":
                main()
            else:
                print("Thanks for using my calculator!")
main()

#These are my ranges
x=0
y=1000


def IsinRange(number):

    if x <= number <= y:
        print('The number {} is in range ({}, {})'.format(number, x, y))
    else:
        print('The number {} is not in range ({}, {})'.format(number, x, y))

testNumbers = [int(input("Enter any number!:"))]

for a in testNumbers:
      IsinRange(a)
I am looking for advice on making this better.
It doesn't seem to display the statement "Would you like to play again?"
You don't use the functions defined on lines 2-9 anywhere. If you were calling them, you'd see the problem with them.

Also, line 73: testNumbers is only ever a list of 1 item. Is that what you wanted?
I suck at this I thought that's what I did from #22 to 42.
You'll get it. There are issues in this code though.
In line 24 you call main(). That makes the main() function recursive. Once you successfully pass and return it will come back to line 26.

Your functions don't return anything. Sometimes they print but never return a value. Better form is for a function to do a task, and allow the calling script to handle input/output.

Your initial functions have no arguments.

Not sure what you expect the code after line 59 to do. That code will execute after the main() function.

Generally bad idea to name a function main. As your skills develop you will learn about __main__ which would be easily confused with main().