Skip to content

Instantly share code, notes, and snippets.

@Fiyin-Anne
Last active December 10, 2019 17:00
Show Gist options
  • Save Fiyin-Anne/6d936105ef231388ede4ce48a707394e to your computer and use it in GitHub Desktop.
Save Fiyin-Anne/6d936105ef231388ede4ce48a707394e to your computer and use it in GitHub Desktop.
Calculator program using Python
def calc():
	try:
		operators = ['+', '-', '*', '/', 'q']
		print(f'Operators available: +, -, *, /')
		num1 = int(input('num1: '))
		operator = input()
		num2 = int(input('num2: '))
		while operator not in operators:
			operator = input('Please enter a valid operator: ')
		else:
			if operator == '+':
				num1 += num2
			elif operator == '-':
				num1 -= num2
			elif operator == '*':
				num1 *= num2
			elif operator == '/':
				num1 /= num2
				while True:
					print(num1)
					operator = input()
					if operator == ' ' or operator == '':
						return num1
						break
					elif operator == '+':
						print(f'{num1} + ', end='')
						num2 = int(input())
						num1 += num2
					elif operator == '-':
						print(f'{num1} - ', end='')
						num2 = int(input())
						num1 -= num2
					elif operator == '*':
						print(f'{num1} * ', end='')
						num2 = int(input())
						num1 *= num2
					elif operator == '/':
						print(f'{num1} / ', end='')
						num2 = int(input())
						num1 /= num2
    except (TypeError, NameError) as err:
	    print(err)
	    return 'Something went wrong...'
    except ZeroDivisionError:
	    print("You can't divide by 0")
	    return num1
    except ValueError:
	    print('Not a number')
	    return num1```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment