Search the Community

Showing results for tags 'calculator'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • General
    • General
    • Show Us Your Creations
    • Blog
    • Recommended Reading
  • One Stop Programming Tutorials
    • Python Beginner Series
  • Programming Languages
    • Python
    • C++
    • C#
    • Arduino
    • Other Languages

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


AIM


MSN


Website URL


ICQ


Yahoo


Jabber


Skype


Location


Interests

Found 7 results

  1. Hey guys, I have been watching kjdelectronics on his python tutorial videos and have recently gotten to the part where you build a calculator, but I'm having trouble in the finalization process of it all. My code seems to be flawed - I keep getting a syntax error and have no idea why because I have copied the code exactly as he presents it in the video. I was wondering if anyone could look at it and tell me where my issue (bug) is located. Here is my code and a screenshot of the syntax error. #Returns the sum of num1 and num2 def add(num1, num2): return num1 + num2 #Returs the result of subtracting num1 - num2 def sub(num1, num2): return num1 - num2 #Returns the result of multiplying num1 * num2 def mul(num1, num2): return num1 * num2 #Returns the result of dividing num1 / num2 def div(num, num2): return num1 / num2 def main(): operation = input("What do you want to do? (+, -, *, /): ") if(operation != '+' and operation != '-' and operation != '*' and operation != '/'): #invalid operation print("You must enter a valid operation") else: var1 = int(input("Enter num1: ") var2 = int(input("Enter num2: ") if(operation == '+'): print(add(var1, var2)) elif(operation == '/'): print(div(var1, var2)) elif(operation == '-'): print(sub(var1, var2)) else: print(mul(var1, var2)) main()
  2. Apollo

    Calculator

    Made my first program using KJDElectronics' video tutorials of Python! This is the edited and cleaned up version i made after reading some advice about splitting up functions so that they can become more usable. Really happy with it and I'm still waiting on some more constructive criticism and ways to improve it! Here it is! """Calculator""" # Modules: import time # used as an illusion of loading for effects import random # used for random numbers in random_operation function import sys # used to exit program when needed # Operations: def add(num1, num2): # Defines addition return num1 + num2 def sub(num1, num2): # Defines subtraction return num1 - num2 def mul(num1, num2): # Defines multiplication return num1 * num2 def div(num1,num2): # Defines division try: return num1 / num2 except ZeroDivisionError: # Handles division by zero print("Division by zero cannot be done. Returning zero...") return 0 def exp(num1, num2): # Defines exponentiation return num1 ** num2 # Run operation function: def run_operation(operation, num1, num2): # Determine operation if operation == int("1"): print("Adding...") time.sleep(1) print(num1, "+", num2, "=", add(num1, num2)) elif operation == int("2"): print("Subtracting...") time.sleep(1) print(num1, "-", num2, "=", sub(num1, num2)) elif operation == int("3"): print("Multiplying...") time.sleep(1) print(num1, "*", num2, "=", mul(num1, num2)) elif operation == int("4"): print("Dividing...") time.sleep(1) print(num1, "/", num2, "=", div(num1, num2)) elif operation == int("5"): print("Raising to the power...") time.sleep(1) print(num1, "^", num2, "=", exp(num1, num2)) else: print("I don't understand") time.sleep(1) # Random operation function def random_operation(operation): for loop in range(4): # repeats random calculations 4 times valid_input = False while not valid_input: # while loop for invalid inputs try: operation = (input("What would you like me to do? add, subtract, multiply, divide, or " "raise to the power?...")) valid_input = True except ValueError: print("I don't understand...") time.sleep(1) # Defines random calculations random_num1 = random.randint(1, 100) random_num2 = random.randint(1, 100) if operation == str("add"): print("Randomizing") time.sleep(1) print(random_num1, "+", random_num2, "=", add(random_num1, random_num2)) time.sleep(1) elif operation == str("subtract"): print("Randomizing") time.sleep(1) print(random_num1, "-", random_num2, "=", sub(random_num1, random_num2)) time.sleep(1) elif operation == str("multiply"): print("Randomizing") time.sleep(1) print(random_num1, "*", random_num2, "=", mul(random_num1, random_num2)) time.sleep(1) elif operation == str("divide"): print("Randomizing") time.sleep(1) print(random_num1, "/", random_num2, "=", div(random_num1, random_num2)) time.sleep(1) elif operation == str("raise to the power"): print("Randomizing") time.sleep(1) print(random_num1, "^", random_num2, "=", exp(random_num1, random_num2)) time.sleep(1) else: print("I don't understand") time.sleep(1) print('\nPLEASURE CALCULATING WITH YOU.') time.sleep(1) print('GOODBYE...') time.sleep(1) sys.exit() # Exits program # Main calculator function: def main(): user_continue = True while user_continue: # Loops if user would like to restart program valid_input = False while not valid_input: # Loops if user enters an invalid input. Avoids error try: # Acquire user input num1 = int(input("What is number 1?...")) num2 = int(input("What is number 2?...")) operation = int(input("What would you like to do? 1. Addition, 2. Subtraction, 3. Multiplication, " "4. Division, 5. Exponentiation... Choose a number...")) valid_input = True except ValueError: # Handles any value errors print("invalid input, please try again...") time.sleep(1) run_operation(operation, num1, num2) time.sleep(1) # Ask if user wants to do random calculations or exit: restart_random = (str(input("Would you like to make more calculations? (y) for yes, anything else for no..." "\n OR would you like a random calculation?...(r) for random calculations then " "exit...anything else for no..."))) if restart_random != "y" and restart_random != "r": print("PLEASURE CALCULATING WITH YOU!") time.sleep(1) print("\nGOODBYE!") time.sleep(1) user_continue = False elif restart_random == "r": time.sleep(1) random_operation(operation) # intro-like sequence print('\n\n Calculator\n\n') time.sleep(1) print(' ---LOADING---\n') time.sleep(1) print('HI. I AM YOUR PERSONAL CALCULATOR BEEP BOOP. \n WHAT WOULD YOU LIKE ME TO CALCULATE?..\n') time.sleep(1) # call main function to start program main() # Turns my program into a module capable of being imported: if __name__ == '__calculator__': main() calculator.py
  3. I was making a calculator that takes BEDMAS (Proper order of operations) into account. I'm very new (I just started today) but I managed to find a simple code for an ordinary calculator (like the ones that you find in phones, computers, etc) that works in a command line, and basing my code off of that. The problem is that I am trying to display the 'operationanswer2' of what I can assume is an integer or a variable (idk man, I'm not very good at this) but it keeps giving me a error message saying 'This inspection detects statements without any effect' He is the code. Due to fear of plagiarism I will only give you 3 parts of it that gives you how it's defined, and what I'm using to print it. operationanswer1 = (number_1 + number_2) if operation2 == '+' and operation1 == '+': operationanswer2 = (operationanswer1 + number_3) print!=operationanswer2 This is not the full code I am just afraid of plagiarism. Also I have school tomorrow so I will only be able to get back to you next evening.
  4. pyNoob

    Calculator debug

    I've run into an issue with the calculation function (python 3.3) where addition works but sub, mul and div each return: Traceback (most recent call last): File "./calc.py", line 22, in <module> main() File "./calc.py", line 15, in main print(subtract(var1, var2)) NameError: global name 'sub' is not defined I have also tried the entire word 'subtract' but no diff. Here is the relevant code snippet: else: var1 = int(input("Enter first value:")) var2 = int(input("Enter second value:")) if(operation == '+'): print(add(var1, var2)) elif(operation == '-'): print(sub(var1, var2)) elif(operation == '*'): print(mul(var1, var2)) else: print(div(var1, var2))
  5. Greetings! I'm brand new to Python, and I've been following the One Stop Programming YouTube tutorial to learn how to use it. Unfortunately, I've come across a problem that I absolutely cannot comprehend as a new programmer, and I was looking for assistance. I'm sure the problem is only minor and will be immediately obvious to anyone with more than a few hours experience. If you look at my attachment you will see a simple calculator, made with the mentoring of the online tutorial. (https://youtu.be/Ow3CcOGNIss?t=1247) I have followed the tutorial, almost to the letter, but my variables are different, and so are some of my prints, but those don't matter as far as I know. The problem is with Y. Highlighted red. It is an unrecognised syntax and I have no idea why, as the tutorial shows exactly the same thing, but with valid syntax. Can anyone shed any light on this situation? Thanks a bunch in advance!
  6. Please help me to find the error! I cuoldnt fix it # for addition def add(num1 , num2): return num1 + num2 #for subraction def sub(num1 , num2): return num1 - num2 #for multiplication def mul(num1 , num2): return num1 * num2 #for divisionion def div(num1 , num2): return num1 / num2 def main(): calculator = input("Enter any of these operation (+,-,*,/) :") if(calculator != '+' and calculator != '-' and calculator != '*' and calculator != '/'): print("invalid operation Enter a valid one :") else: v1 = int(input("Enter number 1 :")) v2 = int(input("Enter number 2 :")) if(calculator == '+'): print(add(v1 , v2)) elif(calculator == '-'): print(sub(v1 , v2)) elif(calculator == '*'): print(mul(v1 , v2)) else: print(div(v1 , v2)) main()
  7. Hey, my name's Brett and I'm new to the forum and Python. I've finished the coding for the Calculator Kevin made on YouTube. I'm having trouble running it. Can anyone help? Here's the code - #returns the sum of num1 and num2 def add(num1, num2): return num1 + num2 #returns the result of subtracting num1 and num2 def sub(num1, num2): return num1 - num2 #returns the result of multiplying num1 and num2 def mul(num1, num2): return num1 * num2 #returns the result of dividing num1 and num2 def div(num1, num2): return num1 / num2 def main(): while True: #cause loop operation = input("What do you want to do (=,-,*,/): ") if(operation!= '+' and operation!= '-' and operation != '*' and operation != '/'): #invalid operation print("You must enter a valid operation") else: var1 = int(input("Enter num1: ")) var2 = int(input("Enter num2: ")) if(operation == '+'): print(add(var1, var2)) elif(operation == '-'): print(sub(var1, var2)) elif(operation == '/'): print(div(var1, var2)) else: print(mul(var1, var2)) main() And when I try and execute the file this happens Thanks in advance, Brett.