0

I'm new to coding and Python is the first language I am learning. I'm trying to develop a basic calculator. The numbers can't be equal to zero and the user must use numbers 1-4 to choose between addition, subtraction, multiplication and division.

My challenge is: when I input a number equal to zero or a number that is not 1-4, the functions should prompt me to enter the right value, then update the variable to continue accordingly with the calculation. The code generates the error message and asks for a new input for the number but it is not saving the new input so it doesn't produce the right outputs.

For example, if I enter a number == 0, it prompts me to enter a new one, but then it keeps zero as the first number. Then if I do the same for the second number, the program stalls and it doesn't calculate anything.

If anyone could help me, I would appreciate tremendously... I apologize if this is not the most elegant code ever, but please keep in mind this is my first try...:-))

I posted this code on dpaste in case seeing it here doesn't work: strong texthttps://dpaste.de/hyKa

def number (n):
    while n == 0:
        print ("ERROR - number must be different than zero")
        n = float(input("Enter a different number. \n"))
        number (n)
        return n

x = int(input("Enter your first number. \n"))

number (x)

def operator_control (user_choice):
    while user_choice > 4 or 1 > user_choice:
         print ("ERROR - you must choose either 1, 2, 3, or 4 depending on the operation you want")
         user_choice = int(input("Let's try again - enter another number for the operator  \n"))
         return user_choice
    return user_choice

user_choice = int(input("Now you will choose the operation! Ready? Choose 1 for addition, 2 for subtraction, 3 for multiplication and 4 for division \n"))

operator_control (user_choice)

z = int(input("Please choose your second number as the operand for the calculation. \n"))

number (z)

operator = user_choice

if operator == 1:
   result = x + z
   print ("Ok - you chose to add your first number," , x , "to" , z , "and the result of this calculation is" , result)

if operator == 2:
   result = x - z
   print ("Awesome - you chose to subtract your second number," , z , "from" , x , "and the result of this calculation is" , result)

if operator == 3:
   result = x * z
   print ("Fantastic! You chose to multiply your first number," , x , "by the second one" , z , "and the result is" , result)

if operator == 4:
   result = x / z
   print ("Fabulous! You chose to divide your first number," , x , "by the second one" , z , "and the result is" , result)
  • I am not sure why my question was marked as a duplicate. My issue has nothing to do with the age question someone else posted a while ago. My code is not updating the values after the user fixes it and it doesn't finalize the computation. The input format is correct and requests float and integers as numbers. – user9120138 Dec 19 '17 at 22:28
  • Your code has two errors. You do not save the return of the function anywhere, and the return of number() has a bad indentation. – Manuel Dec 19 '17 at 22:34
  • 1
    The accepted answer to the question this was marked as a duplicate of illustrates how to accomplish what you are trying to do. @Manuel pointed out some specific issues with your implementation. However, I suggest you review the accepted answer to the linked question again and try to apply it to what you are trying to do. It shows a cleaner way to go about this kind of problem. – Galen Dec 19 '17 at 22:40
  • Thank you so much, Galen and @Manuel. I will fix the indentation issue with the return but I still don't understand what Manuel said about saving the return of the function. I thought by telling the code to "return" it would save the new variable - am I thinking incorrectly about this? I had read that posting a while ago when I tried to find answers and read it again now but couldn't understand how it would relate to my situation. I will take another look at it. – user9120138 Dec 19 '17 at 23:02
  • @user9120138 When to call a function, the return has to be assigned to variable, change "number (x)" by "x =number(x)". https://www.tutorialspoint.com/python/python_functions.htm "The return Statement" – Manuel Dec 19 '17 at 23:07
  • Thank you so much!!! This may help. It makes total sense and I didn't think of that. :-) – user9120138 Dec 19 '17 at 23:12
  • @Manuel - you're awesome!!! Your proposal to save the new variable was all I was missing in my code and it worked!!! Can't thank you enough. – user9120138 Dec 20 '17 at 02:18

0 Answers0