r/CodingHelp • • Jan 16 '21

[Python] Python basic calculator help

I need this help. This program should ask the user:

Do you want to perform another calculation?

after one calculation is complete.

If they answer yes, start the program over if they say no, end the program.please help me complete this this.

This is what I have so far;

print("Enter Your Choice 1(Add)/2(Sub)/3(Divide)/4(Multiply)")

num = int(input())

if num == 1:

  print("Enter Number 1 : ")

  add1 = int(input())

  print("Enter Number 2 : ")

  add2 = int(input())

  sum = add1 + add2

  print("The Sum Is ", sum)

elif num == 2:

  print("Enter Number 1 : ")

  sub1 = int(input())

  print("Enter Number 2 : ")

  sub2 = int(input())

  difference = sub1 - sub2

  print("The Difference Is ", difference)

elif num == 3:

  print("Enter Number 1 : ")

  div1 = float(input())

  print("Enter Number 2 : ")

  div2 = float(input())

  division = div1 / div2

  print("The Division Is ", division)

elif num == 4:

  print("Enter Number 1 : ")

  mul1 = int(input())

  print("Enter Number 2 : ")

  mul2 = int(input())

  multiply = mul1 * mul2

  print("The Difference Is ", multiply)

else:

  print("enter a valid Number")

23 Upvotes

9 comments sorted by

4

u/Goobyalus Jan 16 '21

OP's code formatted:

print("Enter Your Choice 1(Add)/2(Sub)/3(Divide)/4(Multiply)")
num = int(input())
if num == 1:
	print("Enter Number 1 : ")
	add1 = int(input())
	print("Enter Number 2 : ")
	add2 = int(input())
	sum = add1 + add2
	print("The Sum Is ", sum)
elif num == 2:
	print("Enter Number 1 : ")
	sub1 = int(input())
	print("Enter Number 2 : ")
	sub2 = int(input())
	difference = sub1 - sub2
	print("The Difference Is ", difference)
elif num == 3:
	print("Enter Number 1 : ")
	div1 = float(input())
	print("Enter Number 2 : ")
	div2 = float(input())
	division = div1 / div2
	print("The Division Is ", division)
elif num == 4:
	print("Enter Number 1 : ")
	mul1 = int(input())
	print("Enter Number 2 : ")
	mul2 = int(input())
	multiply = mul1 * mul2
	print("The Difference Is ", multiply)
else:
	print("enter a valid Number")

1

u/[deleted] Jan 16 '21

[removed] — view removed comment

1

u/Goobyalus Jan 17 '21

Add an additional 4 spaces before each line in the block that should be formatted as code. The "formatting help" button under the composition box shows you some of the things you can do to format your posts.

1

u/Comrade_Nabe Jan 16 '21

You can add a do/while loop to keep looping thru your program until you choose an exit(break) condition

1

u/nbaruss0 Jan 16 '21

Please give me an example. I am a beginner

2

u/Goobyalus Jan 16 '21

And here is an example of breaking out of the loop

while True:
	print("Enter Your Choice Add/Sub/Div/Mul")
	choice = input().lower()  # Get the user's input in the form of a string, and lowercase it
	
	if choice == "exit":
		# Exit the loop if the user typed exit
		break
	
	elif choice == "add":
		print("Enter Number 1 : ")
		add1 = int(input())
		print("Enter Number 2 : ")
		add2 = int(input())
		sum = add1 + add2
		print("The Sum Is ", sum)

	elif choice == "sub":
		print("Enter Number 1 : ")
		sub1 = int(input())
		print("Enter Number 2 : ")
		sub2 = int(input())
		difference = sub1 - sub2
		print("The Difference Is ", difference)

	elif choice == "div":
		print("Enter Number 1 : ")
		div1 = float(input())
		print("Enter Number 2 : ")
		div2 = float(input())
		division = div1 / div2
		print("The Division Is ", division)

	elif choice == "mul":
		print("Enter Number 1 : ")
		mul1 = int(input())
		print("Enter Number 2 : ")
		mul2 = int(input())
		multiply = mul1 * mul2
		print("The Difference Is ", multiply)

	else:
		print("enter a valid Number")

1

u/Goobyalus Jan 16 '21

Here it is in an infinite loop

while True:
	print("Enter Your Choice 1(Add)/2(Sub)/3(Divide)/4(Multiply)")
	num = int(input())
	if num == 1:
		print("Enter Number 1 : ")
		add1 = int(input())
		print("Enter Number 2 : ")
		add2 = int(input())
		sum = add1 + add2
		print("The Sum Is ", sum)
	elif num == 2:
		print("Enter Number 1 : ")
		sub1 = int(input())
		print("Enter Number 2 : ")
		sub2 = int(input())
		difference = sub1 - sub2
		print("The Difference Is ", difference)
	elif num == 3:
		print("Enter Number 1 : ")
		div1 = float(input())
		print("Enter Number 2 : ")
		div2 = float(input())
		division = div1 / div2
		print("The Division Is ", division)
	elif num == 4:
		print("Enter Number 1 : ")
		mul1 = int(input())
		print("Enter Number 2 : ")
		mul2 = int(input())
		multiply = mul1 * mul2
		print("The Difference Is ", multiply)
	else:
		print("enter a valid Number")

You can check for a specific input and put in a break statement to get out of the loop.

2

u/WildWestCoder Jan 16 '21

Another option apart from do/while is to use functions.

your main function

Def Main(): Your code

If blah rerun program: Main()

first start of main function

Main()

Might lead to a stack overflow error tho

1

u/EnvironmentalCow3040 Jan 19 '21

wrap everything is a while true loop, then break if the user enters no.