Help me to correct my code of simple calculator. My two outputs are correct while third output is throwning error. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Help me to correct my code of simple calculator. My two outputs are correct while third output is throwning error.

Write a program that performs the tasks of a simple calculator. The program should first take an integer as input and then based on that integer perform the task as given below. 1. If the input is 1, then 2 integers are taken from the user and their sum is printed. 2. If the input is 2, then 2 integers are taken from the user and their difference(1st number - 2nd number) is printed. 3. If the input is 3, then 2 integers are taken from the user and their product is printed. 4. If the input is 4, then 2 integers are taken from the user and the quotient obtained (on dividing 1st number by 2nd number) is printed. 5. If the input is 5, then 2 integers are taken from the user and their remainder(1st number mod 2nd number) is printed. 6. If the input is 6, then the program exits. 7. For any other input, then print "Invalid Operation". Note: Each answer in next line.

3rd Jul 2021, 2:04 PM
Student
Student - avatar
17 Answers
+ 4
Before: elif n (>) 1 or n>6 After: elif n (<) 1 or n>6
4th Jul 2021, 5:40 AM
Simba
Simba - avatar
4th Jul 2021, 6:01 AM
Simba
Simba - avatar
+ 2
https://code.sololearn.com/cu2Z65r94evj/?ref=app This is another code runs quite well. But not on sololearn.
4th Jul 2021, 11:09 AM
Student
Student - avatar
+ 1
Result My Output 2 2 Incorrect Output Expected Output 2 2 5
3rd Jul 2021, 2:05 PM
Student
Student - avatar
3rd Jul 2021, 3:37 PM
Student
Student - avatar
+ 1
It's working fine on other ide(pyroid). But sololearn doesn't efficient to infinite loops. That's why it's showing eof error. Somehow, you can try this Student I have just edited my code. Check it out https://code.sololearn.com/cZG16g0JXA6U/?ref=app
3rd Jul 2021, 4:35 PM
Simba
Simba - avatar
+ 1
@Simba Please answer this question that on taking an input of 7 compiler is giving no output instead of printing " invalid operation" It's similar to that of previous code.Can you tell me what changes you have made in this code. try: while True: n = int(input()) num1 = int(input()) num2 = int(input()) if n==1: print(num1 + num2) elif n == 2: print(num1 - num2) elif n == 3: print(num1 * num2) elif n == 4: print(num1 / num2) elif n == 5: print(num1 % num2) elif 1 > n or n > 6: print("Invalid Operation") except: exit()
4th Jul 2021, 5:01 AM
Student
Student - avatar
+ 1
https://code.sololearn.com/c0pjDQJqW410/?ref=app See after this output is showing no output. When we give input of 7
4th Jul 2021, 5:47 AM
Student
Student - avatar
+ 1
https://code.sololearn.com/c8096b3AkgNd/?ref=app Try this, all test cases will run. But it's throwing error on sololearn. My problem has been shorted thank you for your efforts Happy coding!
4th Jul 2021, 8:14 AM
Student
Student - avatar
+ 1
Glad to hear that. Yeah, interactive programs don't play well with codeplayground, as it needs all inputs required by the program upfront. For small no of inputs the following workaround works quite well :) 1 2 3
4th Jul 2021, 8:23 AM
Simba
Simba - avatar
+ 1
while True: choice = int(input()) if (choice>=1 and choice<=5): num1 = int(input()) num2 = int(input()) if choice == 1: res = num1 + num2 print(res) elif choice == 2: res = num1 - num2 print(res) elif choice == 3: res = num1 * num2 print(res) elif choice == 4: res = num1 // num2 print(res) elif choice == 5: res = num1 % num2 print(res) elif choice == 6: exit() else: print("Invalid Operation")
31st Aug 2021, 8:51 AM
Aditya Gulhane
Aditya Gulhane - avatar
+ 1
while True: a = int(input()) if a>0 and a<6: x=int(input()) y=int(input()) if a==1: o=x+y elif a==2: o=x-y elif a==3: o=x*y elif a==4: o=x/y else: o=x%y print(int(o)) elif a==6: quit() else: print("Invalid Operation")
20th Apr 2022, 2:36 PM
Ajay Rawtani
0
n = int(input()) num1 = int(input()) num2 = int(input()) if n>=1 and n<=5: print(num1 and num2) if n==1: print(num1 + num2) elif n == 2: print(num1 - num2) elif n == 3: print(num1 * num2) elif n == 4: print(num1 / num2) elif n == 5: print(num1 % num2) elif n == 6: exit() else: print("Invalid Operation")
3rd Jul 2021, 2:04 PM
Student
Student - avatar
0
May be you are solving and putting all the operators correctly but forgot to handle the division by 0 error.
5th Jul 2021, 2:54 AM
Sumit Kumar
Sumit Kumar - avatar
0
Or check this out May be you get some idea 💡 https://youtu.be/qLz1PEEyNjM
5th Jul 2021, 2:56 AM
Sumit Kumar
Sumit Kumar - avatar
0
z = int(input("input operation from 1 to 6: ")) if (z>=1 and z<=5) : x = int(input("Enter first number: ")) y = int(input("Enter second number: ")) if(z==1): sum = x+y print("the sum of two number is: ",sum) elif(z==2): difference = x-y print("the difference of two number is: ",difference) elif(z==3): product = x*y print("the product of two number is: ",product) elif(z==4): divide = x/y print("the divide of two number is: ",divide) elif(z==5): remainder = y%x print("the remainder of two number is: ",remainder) elif(z==6): exit() else: print("Invalid Operation") Please, suggest me what I am doing wrong?
22nd Jul 2021, 5:37 PM
Amit Negi
Amit Negi - avatar
0
n = int(input()) num1 = int(input()) num2 = int(input()) if n==1: print(num1 + num2) elif n == 2: print(num1 - num2) elif n == 3: print(num1 * num2) elif n == 4: print(num1 // num2) elif n == 5: print(num1 % num2) elif 1 > n or n > 6: exit() else: print("Invalid Operation")
4th Dec 2021, 9:12 AM
satya sriya