Python Forum
I am asking for a review of my homework to confirm I completed all steps
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am asking for a review of my homework to confirm I completed all steps
#1
#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)
Reply
#2
I am looking for advice on making this better.
Reply
#3
It doesn't seem to display the statement "Would you like to play again?"
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#4
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?
Reply
#5
I suck at this I thought that's what I did from #22 to 42.
Reply
#6
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().
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Requesting homework help and maybe a little code review… JonnyEnglish 0 1,580 Jan-16-2020, 04:34 PM
Last Post: JonnyEnglish

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020