-3

This is my code : I attached my outputs and expected outputs as screen shots. I need to get the last expected output. Please see the screenshots I attached.Does anybody can help me?

This is the task.

Task 1: Get user input

Section 1:

Input Arithmetic operation Reset or Termination Section 2:

Input first operand Input second operand Reset or Termination Task 2: Implement functions that performs given arithmetic operation on the given operands and produces the result

Arithmetic operation and the two operands as parameters Return the result of the arithmetic operation

Task 3: Call the calculation function by passing user input to select_op(choice) and display the result from within the select_op(choice) function

    Here are some of the messages you might want to display to the users at certain occasions. Copy and paste them as necessary in your code in appropriate situations to help with auto-grading.  If there is any differences between the output of your code and the expected output, it will be displayed once you click the "Check" button. You can click on "Show differences" button to highlight the difference between outputs. This will be helpful for you to change your code to match the expected output.

def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

def multiply(x, y):
    return x * y

def divide(x, y):
    return x / y

def power(x, y):
    return x ** y

def remainder(x, y):
    return x % y

def select_op(choice):
    if choice == '+':
        num1 = int(input("Enter first number: "))
        print(num1)
        num2 = int(input("Enter second number: "))
        print(num2)
        print(float(num1), "+", float(num2), "=", float(add(num1, num2)))

    elif choice == '-':
        num1 = float(input("Enter first number: "))
        num2 = float(input("Enter second number: "))
        print(num1, "-", num2, "=", subtract(num1, num2))

    elif choice == '*':
        num1 = float(input("Enter first number: "))
        num2 = float(input("Enter second number: "))
        print(num1, "*", num2, "=", multiply(num1, num2))

    elif choice == '/':
        num1 = int(input("Enter first number: "))
        print(num1)
        num2 = int(input("Enter second number: "))
        print(num2)

        try:
            result = num1 / num2
            print(f"{num1} / {num2} = {result}")
        except ZeroDivisionError:
            print("float division by zero")
            print(float(num1), "/", float(num2), "=", "None")

    elif choice == '^':
        num1 = float(input("Enter first number: "))
        num2 = float(input("Enter second number: "))
        print(num1, "^", num2, "=", power(num1, num2))

    elif choice == '%':
        num1 = float(input("Enter first number: "))
        num2 = float(input("Enter second number: "))
        print(num1, "%", num2, "=", remainder(num1, num2))

    elif choice == '#':
        return -1

    elif choice == '$':
        return 0

    else:
        print("Invalid input")
        return 1


while True:
    print("Select operation.")
    print("1.Add      : + ")
    print("2.Subtract : - ")
    print("3.Multiply : * ")
    print("4.Divide   : / ")
    print("5.Power    : ^ ")
    print("6.Remainder: % ")
    print("7.Terminate: # ")
    print("8.Reset    : $ ")

    # take input from the user
    choice = input("Enter choice(+,-,*,/,^,%,#,$): ")
    print(choice)

    if select_op(choice) == -1:
        print("Done. Terminating")
        exit()

Output 1 : correct

(https://i.stack.imgur.com/8JYL1.png)

Output 2 : correct

(https://i.stack.imgur.com/tqokg.png)

Output 3 : correct

(https://i.stack.imgur.com/8Wcd3.png)

Output 4 : Wrong

(https://i.stack.imgur.com/UXx3i.png)

  • 1
    There are so many misindents. I can't tell which mistakes occur on your code itself, and which ones when you copy it here. – Felix Fourcolor Mar 18 '23 at 05:09
  • 1
    You didn't give any input in the second number – ItsMe Mar 18 '23 at 05:14
  • The code shown is not runnable. Please provide a minimally reproducible example of your problem. Hint: take a look at the *operator* module – DarkKnight Mar 18 '23 at 06:09
  • Welcome to Stack Overflow. Please read [ask] and take the [tour], and also read the [formatting help](https://stackoverflow.com/help/formatting) and [mre]. Please keep in mind that this is **not a discussion forum**; we [cannot](https://meta.stackoverflow.com/questions/284236) "help you" because that is **not a question**. Finally, [Please do not upload images of code/data/errors.](//meta.stackoverflow.com/q/285551) – Karl Knechtel Mar 18 '23 at 06:35
  • @DarkKnight while `operator` could *theoretically help* with implementing the *apparent* requirements for the overall program, it is completely unrelated to the problem OP is encountering. – Karl Knechtel Mar 18 '23 at 06:36
  • Hello there, Thank you for supporting me. I record a small video for more reference. https://mega.nz/file/bZQXhDLB#ppNQL7AkH9IkRwFjKdQU8CZ2uZ4IoZzL1g9lBfdV8Co – Maduranga Dinesh Mar 18 '23 at 06:39
  • @KarlKnechtel Yes it's unrelated to the fact that OP has no idea what she's doing. However, once she's got a grasp of Python fundamentals then the *operator* module will help to make a clean implementation. Did you notice that I prefixed my suggesting with **Hint:** – DarkKnight Mar 18 '23 at 06:41
  • Ok. Can you tell me how to get this 4th output? – Maduranga Dinesh Mar 18 '23 at 06:54

1 Answers1

-2

In the output number 4, in the second number input i can see 0$. What is the dollar symbol for? If you want to convert string to integer by using int(input("")), string/char you are giving as input must rapresent a number. So it cant contain any letters or symbols. That will always lead to an error and app crash when user gives wrong type of input. You should parse your string input before converting it to int. Store your input as string. Parse it, check if it doesn't contain any letters or symbols and then convert it to integer. It is wrong to convert users input directly to a certain data type. You cant control if user gives you input you wouldn't expect. The answer is simple, you are trying to give input as number and control input "$ symbol" in single line. That line is a string so when you try to convert it to int it gives you wrong literal error. No symbols, letters string can be converted to int. Also you dont have to use reset. Once you give him second number, the result will be printed and then everything starts again cuz it is in an endless loop until you give him # as control input.

  • Have you heard of try/except ? – DarkKnight Mar 18 '23 at 06:32
  • Yes, i know what try/except is. He uses it for zero division but the problem is different with his code. In the line num2 = int(input() ) he takes the string from user input line and converts it to int directly. So user can only input numbers. If you open screenshot number 4, you can see that he gave 0$. What is dolar for. If he would give just 0 it would work. It crashed because of that symbol. You can convert string to in but all chars must rapresent numbers. You cant convert string "letters" to int but only string "1234567890" – Igor Opalach Mar 18 '23 at 06:57
  • Let me know how do I change the code. I think the solution is available in "/" area – Maduranga Dinesh Mar 18 '23 at 10:31
  • If you want to reset the program while giving differnt input types in a single line, define a funcion for parsing string. When you ask input from user, dont convert it to int or float, just store is as string and pass it as parameter to your function. There are some python funcion to parse string. For example you can check if a string contains certain symbol. If you give me contact i can help you. – Igor Opalach Mar 18 '23 at 15:10