0

Update: Re did my code. Now **I am unable to find the answer to this issue no 1). ** Newbie here. I spent the whole night trying to figure this out but I feel stuck if anyone can kindly guide me. Below is the question and Code I did for python. Thanks in advance.

  1. Limit the input range from -70 to 80 for each input number
  2. Prevent dividing by zero (Do not use try-except)
  3. Instead of hard-coding values from -70 to 80, let the user enter the range
  4. check to make sure that the lower range in < the higher range
    while True:

    print("what do you want to do?")
    print("1 Addition")
    print("2 Subtraction")
    print("3 Multiplication")
    print("4 Division")
    print("Enter Q to Quit.")



    def addition(firstNumber,secondNumber):
        result = firstNumber + secondNumber
        print("{0} + {1} = {2}".format(firstNumber,secondNumber,result))

    def subtraction(firstNumber,secondNumber):
        result = firstNumber - secondNumber
        print("{0} - {1} = {2}".format(firstNumber,secondNumber,result))    

    def multiplication(firstNumber,secondNumber):
        result = firstNumber * secondNumber
        print("{0} * {1} = {2}".format(firstNumber,secondNumber,result))

    def divide(firstNumber,secondNumber):
        if secondNumber == 0.0:
            print("You no divide by Zero my friend")
        else:
            result = firstNumber / secondNumber
            print("{0} / {1} = {2}".format(firstNumber,secondNumber,result))

    choice = input("Enter your choice: ")
    if choice == "q" or choice == "Q":
        break
    
    firstNumber = float(input("enter the firstNumber: "))
    secondNumber = float(input("enter the secondNumber: "))
    
    if firstNumber <= -70 or firstNumber >= 80:
        print("invalid choice enter the input again")
        firstNumber =float(input())
    else :
        print("\n")

    if secondNumber <= -70 or secondNumber >= 80:
        print("invalid choice enter the input again")
        secondNumber =float(input())
    else :
         print("\n")
    if choice == "1":
        addition(firstNumber,secondNumber)
    elif choice == "2":
        subtraction(firstNumber,secondNumber)
    elif choice == "3":
        multiplication(firstNumber,secondNumber)
    elif choice == "4":
        divide(firstNumber,secondNumber)
    else:
        print("Invalid Choice")


  • Can you clarify the issue you're having? It might be obvious from running your program, but for us who can't do that immediately, it's a lot of code to read through to try to see a problem. [Here are](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) some [good guides](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems) on asking homework-related questions on Stack Overflow and related sites. – Blckknght Jul 17 '22 at 07:40
  • if(secondNumber==0): print("Operation not allowed.") # Division not allowed with Zero although I set this condition but when I use 0(zero) as a second number I get error message instead of "Operation not allowed" message and It does not prompt to enter secondNumber again. Thanks. – defaultJohnSmith Jul 17 '22 at 07:44
  • I'd suggest editing the question with the problem, so that it's more clear. You might consider cutting down your code to only show a [mcve] (though this may not be easy if you're really new to programming). This seems like a data validation issue, which [this [previous question](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) might help with. – Blckknght Jul 17 '22 at 08:40
  • Your test for 0 occurs before the user has input their value. I suggest moving the test down to just before the division is attempted. – quamrana Jul 17 '22 at 09:02
  • @Blckknght I redid the whole code. I fixed the issue with zero. Now I am unable to set range for firstNumber and secondNumber. so that when user input any number out of the range(-70, 80) they get prompted to input firstNumber again. quamrana thank you. – defaultJohnSmith Jul 17 '22 at 09:47

1 Answers1

0

You have not specified or put any check on the input. You can do that as shown below

counter = 100
while counter:
    counter -=1
    if firstNumber <= -70 or firstNumber >= 80:
        print("invalid choice enter the input again")
        firsNumber =float(input())
    else :
        break
counter2 = 100
    while counter2:
        counter2 -=1
    if secondNumber <= -70 or firstNumber >= 80:
        print("invalid choice enter the input again")
        secondNumber =float(input())
    else :
        break 

You can reduce the counter value depending upon how many times a user is allowed to give correct input. If you would like to take input from user until they enter correct input put True in place of counter and counter2.

Both of these checks can be done in one loop also I did it this way assuming you would like to ask correct input from user as soon as they enter something invalid.

Hope this helps.