Python Forum
How to Loop my calculator input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Loop my calculator input
#1
Hello guys, I created a addition and subtraction calculator and I am wondering how to get the calculator to keep looping where it keeps asking for an input and then carrying out the right operation. Right now, it takes the inputs, prints the result and then the application stops.

Here is the code:


a = float(input("what operation are you trying to : Select 1 for addition 2 for subtraction "))

while a >=3:
print("Wrong input")
a = float(input(" Please enter the right input: Select 1 for addition 2 for subtraction "))
if a < 3:
print("Thank you for your correct input ")

b = float(input("please enter the first number:"))
c = float(input("please enter the second number"))
d = float(input("Please enter the number you want for the operation "))
e = b + c
f = b - c

if d == 1:
print (e)

if d == 2:
print (f)


Thanks in advance!
Reply
#2
can you try below to see if that helps,Also later when used division and other operators, better we use try and except blocks too to catch the exceptions,

class calc():
	def calc_add(self,a,b):
		return a+b;
	def calc_sub(self,a,b):
		return a-b;

print("welcome i am calc")
while True:
	line=int(input("choose 1 : add  and 2 : sub   and 0 to exit  -->"))
	if line == 1:
		a=int(input("enter value a: "))
		b=int(input("enter value b: "))
		x=calc()
		print('result is : ',x.calc_add(a,b))
	elif line == 2:
		a=int(input("enter value a: "))
		b=int(input("enter value b: "))
		x=calc()
		print('result is : ',x.calc_sub(a,b))
	elif line == 0:
		break
	elif line > 2:
		print("wrong input given, please rety:")
    else:
		print("you have given negative number, please retry")
Best Regards,
Sandeep

GANGA SANDEEP KUMAR
Reply
#3
Thank Sandeep for sharing script. I made a little change to improve the code: can deal with cases where line < 1, and can calculate values which are not integer.

Best Regards,

class calc():
    def calc_add(self,a,b):
        return a+b;
    def calc_sub(self,a,b):
        return a-b;

print("welcome I am calc")
line=1
while True:
    line=int(input("choose 1: add and 2: sub and 0 to exit -->"))
    if line == 1:
        a=float(input("enter value a: "))
        b=float(input("enter value b: "))
        x=calc()
        print('result is :',x.calc_add(a,b))
    elif line == 2:
        a=float(input("enter value a: "))
        b=float(input("enter value b: "))
        x=calc()
        print('result is :',x.calc_sub(a,b))
    elif line == 0:
        break
    else:
        print("wrong input given, please retry:")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 991 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  Code won't break While loop or go back to the input? MrKnd94 2 906 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,434 Apr-05-2022, 06:18 AM
Last Post: C0D3R
Exclamation question about input, while loop, then print jamie_01 5 2,616 Sep-30-2021, 12:46 PM
Last Post: Underscore
  I want to check if the input is str or is int & if it's str repeat the loop HLD202 4 2,724 Nov-23-2020, 11:01 PM
Last Post: perfringo
  Loop back through loop based on user input, keeping previous changes loop made? hbkpancakes 2 2,884 Nov-21-2020, 02:35 AM
Last Post: hbkpancakes
  if-loop does not respond to input Kmarstein 1 2,195 Jan-28-2020, 09:29 PM
Last Post: Larz60+
  Nested Loop for user input Ads 2 3,525 Dec-30-2019, 11:44 AM
Last Post: Ads
  Server infinite loop input from user tomislav91 1 4,170 May-23-2019, 02:18 PM
Last Post: heiner55
  How to continue in loop until correct input received sunnyarora 10 9,775 May-04-2019, 02:37 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020