I'm very new to coding and just started recently. I made a very basic calculator and tried to make it into an exe file so that I could run it on a system without python. But after making it into an exe file using pyinstaller, it says it cannot execute script. Please help!
print("Welcome to Subahs Calculator")
print("Choose an operation to perform:")
print("1.Addition")
print("2.Substraction")
print("3.Multiplication")
print("4.Division")
print("5.Square")
print("6.Square Root")
operation = input()
if operation == "1":
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
result = float(num1) + float(num2)
print("The result is " + str(result))
elif operation == "2":
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
result = float(num1) - float(num2)
print("The result is " + str(result))
elif operation == "3":
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
result = float(num1) * float(num2)
print("The result is " + str(result))
elif operation == "4":
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
result = float(num1) / float(num2)
print("The result is " + str(result))
elif operation == "5":
num1 = input("Enter the number: ")
result = pow(float(num1), 2)
print("The result is " + str(result))
elif operation == "6":
num1 = input("Enter the number: ")
result = sqrt(float(num1))
print("The result is " + str(result))
else: print("Invalid Operation")
k = input("Press Enter to exit.")```
Related
This question already has answers here:
Correct way to write line to file?
(17 answers)
Closed 2 years ago.
f = open("calculator.txt", "a+")
a = True
while a == True:
operation = input("Please input an operation (+, -, *, /): ")
num1 = float(input("Please input number 1: "))
num2 = float(input("Please input number 2: "))
if operation == "+":
result = num1 + num2
elif operation == "-":
print(num1 - num2)
elif operation == "*":
print(num1 * num2)
elif operation == "/":
print(num1 / num2)
answer = input("Run again? (Yes/No): ")
if answer == "Yes":
continue
else:
print("Goodbye ")
exit()
How do i go about writing the results of the calculations in a file? Say the user inputs 5 + 5 and the program returns 10, how would I save "10" in a text file? If possible the entire equation "5+5 = 10"
You can use str.format to format result string and then use print() with file= parameter to write to file. For example:
with open('operations.txt', 'a') as f_out:
while True:
operation = input("Please input an operation (+, -, *, /): ")
num1 = float(input("Please input number 1: "))
num2 = float(input("Please input number 2: "))
if operation == "+":
result = num1 + num2
elif operation == "-":
result = num1 - num2
elif operation == "*":
result = num1 * num2
elif operation == "/":
result = num1 / num2
result_string = '{} {} {} = {}'.format(num1, operation, num2, result)
# print result string to screen:
print(result_string)
# save result string to file
print(result_string, file=f_out)
answer = input("Run again? (Yes/No): ")
if answer == "Yes":
continue
else:
print("Goodbye ")
break
Prints:
Please input an operation (+, -, *, /): +
Please input number 1: 1
Please input number 2: 2
1.0 + 2.0 = 3.0
Run again? (Yes/No): Yes
Please input an operation (+, -, *, /): /
Please input number 1: 2
Please input number 2: 4
2.0 / 4.0 = 0.5
Run again? (Yes/No): no
Goodbye
And saves operations.txt:
1.0 + 2.0 = 3.0
2.0 / 4.0 = 0.5
You could do it like this:
f.write(f'{num1} {operation} {num2} = {result}') # for example, '5 + 5 = 10'
if answer == "Yes":
continue
else:
print("Goodbye ")
you can change your program like this:
f = open("calculator.txt", "a+")
a = True
while a:
operation = input("Please input an operation (+, -, *, /): ")
num1 = float(input("Please input number 1: "))
num2 = float(input("Please input number 2: "))
if operation == "+":
result = num1 + num2
elif operation == "-":
result = num1 - num2
elif operation == "*":
result = num1 * num2
elif operation == "/":
result = num1 / num2
print(result)
f.write(f"{num1}{operation}{num2} = {result}\n") #this will save the whole equation
answer = input("Run again? (Yes/No): ")
if answer == "Yes":
continue
else:
print("Goodbye ")
a = False
Just do like this:
with open("calculator.txt", "a+") as myfile:
myfile.write("\n" + result)
Inside if block
You can open the file and write into it like this:
f = open("calculator.txt", "a+")
f.write(f"{num1} {operation} {num2} = {result}")
I can't quite figure out why my code won't work.
Whenever I click run, it doesn't follow back with a traceback error, it just says process finished with exit code 0.
I thought it might be the casefold but then when I applied it to "Y". casefold it wouldn't work full stop.
def calculate():
operator = input("please select the kind of maths you would like to do")
if operator == "+":
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))
print('{} + {} ='.format(num1, num2))
print(num1 + num2)
elif operator == "-":
num1 = int(input("enter first number: "))
num2 = int(input("enter second number: "))
print("{} - {} =".format(num1, num2))
print(num1 - num2)
elif operator == "*":
num1 = int(input("enter first number: "))
num2 = int(input("enter second number: "))
print("{} * {} =".format(num1, num2))
print(num1 * num2)
elif operator == "/":
num1 = int(input("enter first number: "))
num2 = int(input("enter second number: "))
print("{} / {} =".format(num1, num2))
print(num1 / num2)
else:
_exit = input("would you like to exit? type Y for YES and N for NO")
if _exit.casefold() == "y":
sys.exit()
else:
calculate()
Just add calculate() at the very end to call the function.
Put function calling calculate() at the end without any indent. Your function isn't even getting called, thus giving no error.
You need to call this calculate() function first (at least it's not being executed in your code sample).
def calculate()
# func code here
#Exec this function
calculate()
I guess it should be like this:
def calculate():
operator = input("please select the kind of maths you would like to do")
if operator == "+":
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))
print('{} + {} ='.format(num1, num2))
print(num1 + num2)
elif operator == "-":
num1 = int(input("enter first number: "))
num2 = int(input("enter second number: "))
print("{} - {} =".format(num1, num2))
print(num1 - num2)
elif operator == "*":
num1 = int(input("enter first number: "))
num2 = int(input("enter second number: "))
print("{} * {} =".format(num1, num2))
print(num1 * num2)
elif operator == "/":
num1 = int(input("enter first number: "))
num2 = int(input("enter second number: "))
print("{} / {} =".format(num1, num2))
print(num1 / num2)
else:
_exit = input("would you like to exit? type Y for YES and N for NO")
if _exit.casefold() == "y":
sys.exit()
else:
calculate()
calculate()
I'm fairly new to python and have tried to develop a calculator. I have created it so that it keeps asking you questions until you press 9 and exits. I have made an error while doing this and it keeps asking me to enter first number and keeps looping that
loop = 1
oper = 0
while loop == 1:
num1 = input("Enter the first number: ")
print num1
oper = input("+, -, *, /,9: ")
print oper
num2 = input("Enter the second number: ")
print num2
if oper == "+":
result = int(num1) + int(num2)
elif oper == "-":
result = int(num1) - int(num2)
elif oper == "*":
result = int(num1) * int(num2)
elif oper == "/":
result = int(num1) / int(num2)
elif oper == "9":
loop = 0
print "The result of " + str(num1) + str(oper) + str(num2) + " is " + str(result)
input("\nPress 9 to exit.")
The issue seems to be that you haven't indented. Python cares about how much you indent, and thus only indented lines will be considered part of the while loop. Here only the first line (num1 = input...) is being considered part of the while loop. The simplest way to fix this would be to add four spaces before each line that is supposed to be in the loop (as well as an additional four spaces before each line in an if statement).
See http://www.diveintopython.net/getting_to_know_python/indenting_code.html for more help.
It's because you never do anything to break in the first place. Try changing your oper to include 9:
oper = raw_input("+, -, /, *, or 9 (to exit)": )
Then include an elif statement and change loop to 0 to exit the while loop:
elif oper == "9":
loop = 0
Also, deal with your indention:
loop = 1
while loop == 1:
num1 = input("Enter the first number: ")
print num1
oper = input("+, -, *, /,9: ")
print oper
num2 = input("Enter the second number: ")
print num2
if oper == "+":
result = int(num1) + int(num2)
elif oper == "-":
result = int(num1) - int(num2)
elif oper == "*":
result = int(num1) * int(num2)
elif oper == "/":
result = int(num1) / int(num2)
elif oper == "9":
loop = 0
print "The result of " + str(num1) + str(oper) + str(num2) + " is " + str(result)
You had problem with indentation and here's a better way to exit using break for the while loop:
loop = 1
oper = 0
while loop == 1:
x = input("Press 9 to exit otherwise anything to continue:")#much better way
if x == "9":
break
num1 = input("Enter the first number: ")
print (num1)
oper = input("+, -, *, /: ")
print (oper)
num2 = input("Enter the second number: ")
print (num2)
if oper == "+":
result = int(num1) + int(num2)
elif oper == "-":
result = int(num1) - int(num2)
elif oper == "*":
result = int(num1) * int(num2)
elif oper == "/":
result = int(num1) / int(num2):
else:
print("Invalid operator!") #if user inputs something else other than those
print ("The result of " + str(num1) + str(oper) + str(num2) + " is " + str(result))
I'm new here in this "world".
I tried to create a calculator with Python,here's the code.
When I try to run it,IDLE gives me errors,can you help me,please? :D
Header 1
print("Options")
print("Type 'add' to add two numbers")
print("Type'subtract' to subtract two numbers")
print("Type'multiply' to multiply two numbers")
print("Type'divide' to divide two numbers")
print("Type'quit' to exit")
user_input = input(": ")
if user_input == "quit":
break
elif user_input == "add" :
num1 = float(input("Insert a number: "))
num2 = float(input("Insert another number: "))
result = str(num1+num2)
print("The answer is " + result)
elif user_input == "subtract" :
num1 = float(input("Insert a number: "))
num2 = float(input("Insert another number: "))
result = str(num1+num2)
print("The answer is" + result)
elif user_input == "multiply" :
num1 = float(input("Insert a number: "))
num2 = float(input("Insert another number: "))
result = str(num1+num2)
print("The answer is " + result)
elif user_input == "divide" :
num1 = float(input("Insert a number: "))
num2 = float(input("Insert another number: "))
result = str(num1+num2)
print("The answer is " + result)
else:
print("Unknown command")
Try this (you don't have loop so use exit() rather than break, also use the right operators for add, divide, sub-struct, multiply.
from __future__ import division # to support division
print("Options")
print("Type 'add' to add two numbers")
print("Type'subtract' to subtract two numbers")
print("Type'multiply' to multiply two numbers")
print("Type'divide' to divide two numbers")
print("Type'quit' to exit")
user_input = raw_input(": ")
if user_input == "quit":
exit() #break is uesd in loops
elif user_input == "add" :
num1 = float(input("Insert a number: "))
num2 = float(input("Insert another number: "))
result = str(num1+num2)
print("The answer is " + result)
elif user_input == "subtract" :
num1 = float(input("Insert a number: "))
num2 = float(input("Insert another number: "))
result = str(num1-num2)
print("The answer is" + result)
elif user_input == "multiply" :
num1 = float(input("Insert a number: "))
num2 = float(input("Insert another number: "))
result = str(num1*num2)
print("The answer is " + result)
elif user_input == "divide" :
num1 = float(input("Insert a number: "))
num2 = float(input("Insert another number: "))
result = str(num1/num2)
print("The answer is " + result)
else:
print("Unknown command")
The problem is the break statement here:
if user_input == "quit":
break
You can only use a break statement within a while and/or for loop.
The solution is to replace the break statement with print() or exit() instead.
For your program to be more effective (and to be able to use the break statement), you could execute your if,elif and else statements within a while loop:
print("Options")
print("Type 'add' to add two numbers")
print("Type'subtract' to subtract two numbers")
print("Type'multiply' to multiply two numbers")
print("Type'divide' to divide two numbers")
print("Type'quit' to exit")
while True:
user_input = input(": ")
if user_input == "quit":
print('Program terminated')
break
elif user_input == "add" :
num1 = float(input("Insert a number: "))
num2 = float(input("Insert another number: "))
result = str(num1+num2)
print("The answer is " + result)
elif user_input == "subtract" :
num1 = float(input("Insert a number: "))
num2 = float(input("Insert another number: "))
result = str(num1+num2)
print("The answer is" + result)
elif user_input == "multiply" :
num1 = float(input("Insert a number: "))
num2 = float(input("Insert another number: "))
result = str(num1+num2)
print("The answer is " + result)
elif user_input == "divide" :
num1 = float(input("Insert a number: "))
num2 = float(input("Insert another number: "))
result = str(num1+num2)
print("The answer is " + result)
else:
print("Unknown command")
The problem is you are implementing a break that is not in a while loop.
Breaks must occur in a while loop. So a way of getting around this problem is by saying:
if user_input == "quit":
exit()
Example2:
import sys
if user_input == "quit":
sys.exit()
Hope this helped!
if user_input == 'quit':
#break
pass
I have an error with my code when I try to run it.
CODE
print("Operations: \n1. Addition \n2. Subtraction \n3: Multiplication \n4. Division")
print("^ Operation 'ID' please enter the id of your choice")
choice = input()
num1 = input("Enter your first number: ")
num2 = input("Enter your second number: ")
def addition(num1, num2):
num1
num2
ans = num1 + num2
print('Your answer is %s') %(ans)
def subtraction(num1, num2):
num1
num2
ans = num1 - num2
print('Your answer is %s') %(ans)
def multiply(num1, num2):
num1
num2
ans = num1 * num2
print('Your answer is %s') %(ans)
def division(num1, num2):
num1
num2
ans = num1 / num2
print('Your answer is %s') %(ans)
if choice == "1":
addition
elif choice == "2":
subtraction
elif choice == "3":
multiply
elif choice == "4":
division
else:
print("Invalid Input")
Everything works until python is called to print the answer.
I am aware of the possible duplicates but none of the code provided there works.
The issue is in the lines -
if choice == 1():
addition
elif choice == 2():
subtraction
elif choice == 3():
multiply
elif choice == 4():
division
I have no idea what you want 1() to do, seems like a typo. Also you should be calling the functions addition , etc , like - addition(num1, num2) .
And choice is string not int . And you should convert num1 and num2 to int.
More issues in your code -
Why are you doing - num1 and num2 in your functions , it does not do anything, you can remove the first two lines of each function.
Your print function in wrong, in Python 3.x , the %(ans) should be inside the function, not outside it.
Code -
num1 = int(input("Enter your first number: "))
num2 = int(input("Enter your second number: "))
def addition(num1, num2):
ans = num1 + num2
print('Your answer is %s' %(ans))
def subtraction(num1, num2):
ans = num1 - num2
print('Your answer is %s' %(ans))
def multiply(num1, num2):
ans = num1 * num2
print('Your answer is %s' %(ans))
def division(num1, num2):
ans = num1 / num2
print('Your answer is %s' %(ans))
if choice == '1':
addition(num1, num2)
elif choice == '2':
subtraction(num1, num2)
elif choice == '3':
multiply(num1, num2)
elif choice == '4':
division(num1, num2)
The problem is due to this
if choice == 1():
Where as it should be
if choice == 1:
And you have to convert num1 and num2 to integer types
And you should call the function just not declare them that is
if choice == 1:
addition(num1, num2)
And you have to do this for other things
And after doing all the changes your program would look like this
print("Operations: \n1. Addition \n2. Subtraction \n3: Multiplication \n4. Division")
print("^ Operation 'ID' please enter the id of your choice")
choice = int(input())
num1 = int(input("Enter your first number: "))
num2 = int(input("Enter your second number: "))
def addition(num1, num2):
ans = num1 + num2
print('Your answer is %s') %(ans)
def subtraction(num1, num2):
ans = num1 - num2
print('Your answer is %s') %(ans)
def multiply(num1, num2):
ans = num1 * num2
print('Your answer is %s') %(ans)
def division(num1, num2):
ans = num1 / num2
print('Your answer is %s') %(ans)
if choice == 1:
addition(num1,num2)
elif choice == 2:
subtraction(num1,num2)
elif choice == 3:
multiply(num1,num2)
elif choice == 4:
division(num1,num2)
else:
print("Invalid Input")
My changes would be:
choice = int(input("Operations: \n1. Addition \n2. Subtraction \n3: Multiplication \n4. Division\n^ Operation 'ID' please enter the id of your choice\n"))
num1 = int(input("Enter your first number: "))
num2 = int(input("Enter your second number: "))
ans=None
if choice == 1:
ans = num1 + num2
elif choice == 2:
ans = num1 - num2
elif choice == 3:
ans = num1 * num2
elif choice == 4:
ans = float(num1) / num2
if ans:
print('Your answer is %s') %(ans)
else:
print("Invalid Input")
The error with your current code is just with the brackets in your print statements. if instead of having %ans outside of the print statement, the code works when it is like this
print('Your answer is %s' %ans)
A neater version of the same code you just wrote looks like this
print("Operations: \n1. Addition \n2. Subtraction \n3: Multiplication \n4. Division")
print("^ Operation 'ID' please enter the id of your choice")
choice = int(input())
num1 = int(input("Enter your first number: "))
num2 = int(input("Enter your second number: "))
def addition(num1, num2):
return num1+num2
def subtraction(num1, num2):
return num1-num2
def multiply(num1, num2):
return num1*num2
def division(num1, num2):
return num1/num2
if choice == 1:
ans = addition(num1,num2)
elif choice == 2:
ans = subtraction(num1,num2)
elif choice == 3:
ans = multiply(num1,num2)
elif choice == 4:
ans = division(num1,num2)
else:
print("Invalid Input")
if choice in range(4):
print('Your answer is %s' %ans)
Ironically, this has ended up with me writing an answer to my own question with admittedly some help from # Vignesh Kalai, although hid code was a little off. So before anything else I will address the changes to my code.
Firstly, instead of defining every operation I have linked than with the choice of their "ID"'s.
Secondly, I am using "" + str(x) to print the answers out instead of the admittedly bad idea of using %s.
REVISED CODE
choice = int(input("Operations: \n1. Addition \n2. Subtraction \n3: Multiplication \n4. Division\n^ Operation 'ID' please enter the id of your choice\n"))
num1 = int(input("Enter your first number: "))
num2 = int(input("Enter your second number: "))
if choice == 1: #Addition
num1
num2
ans = num1 + num2
print("Your answer is " + str(ans))
elif choice == 2: #Subtraction
num1
num2
ans = num1 - num2
print("Your answer is " + str(ans))
elif choice == 3: #Miltiplication
num1
num2
ans = num1 * num2
print("Your answer is " + str(ans))
elif choice == 4: #Division
num1
num2
ans = float(num1) / float(num2)
print("Your answer is " + str(ans))