Assignment
University
American Baptist CollegeCourse
CSCI 1534 | Data Analysis and VisualizationPages
3
Academic year
2023
Muthia Marhamah
Views
0
Answer this question. Make a program in python to request 3 inputs. Input1 = first number Input2 = second number Input 3 = operator (+ - * /) Source Code : number1 = int(input("Enter the first number: ")) # Variable number1 stores the user's input as the first number number2 = int(input("Enter the second number: ")) # Variable number2 stores the user's input as the second number operator = input("Enter the operator (+ - * /): ") # The operator variable allows the user to choose the arithmetic operation # Processing result = 0 # Initialize the result variable to 0 display_number1 = number1 # Variable display_number1 takes its value from the input number1 display_number2 = number2 # Variable display_number2 takes its value from the input number2 if operator == '+': # Selection result = number1 # Initialize the result with the value of number1 for i in range(1, number2 + 1): # Loop from 1 to number2 (inclusive)
result += 1 # Increment the result by 1 for each iteration elif operator == '-': for i in range(1, number2 + 1): # Loop from 1 to number2 (inclusive) number1 -= 1 # Decrement number1 by 1 for each iteration result = number1 # Set the result to the updated value of number1 elif operator == '*': for i in range(1, number2 + 1): # Loop from 1 to number2 (inclusive) result += number1 # Add number1 to the result for each iteration elif operator == '/': result_difference = number1 # Initialize a variable to store the difference while result_difference > 0: # Continue looping until result_difference is greater than 0 result += 1 # Increment the result by 1 result_difference -= number2 # Subtract number2 from result_difference # Output print(display_number1, operator, display_number2, "=", result) # Display the result in the format: number1 operator number2 = result The output : 1. Operator + 2. Operator –
3. Operator * 4. Operator /
Python Practice #3 (Arithmetic Program)
Please or to post comments