'none' prints for no apparent reason - python

print("Entre nummber 1: ")
num1 = float(input('> '))
print("Entre opperation: ")
op = input('> ')
print("Entre nummber 2: ")
num2 = float(input('> '))
result = print("Your Result is:")
if op == "+":
print(num1 + num2)
print(result)
print("Done")
elif op == '-':
print(num1 - num2)
print(result)
print("Done")
elif op == '/':
print(num1 / num2)
print(result)
print("Done")
elif op == '*':
print(num1 * num2)
print(result)
print("Done")
elif op == '**':
print(num1 ** num2)
print(result)
print("Done")
else:
print("Entre a valid opperation")
I tried to make a calculator. It works fine but when at the end a 'none' pops up for no apparent reason .
I don't know why. Any help is appreciated.
This is the problem:

result = print("Your Result is:")
print("Your Result is:") prints this string "Your Result is:" and returns None and now result is equal None. then print(result) prints None

result = print("Your Result is:")
print return nothing None
value of result is None
print(result) #this is none
You should store it in a variable like
result = num1 + num2
print(result) #with calculated value

# remove result = print("Your result is :")
# Add this result = num1 'operation +/-/*/** etc' num2 after your if condittions.
# for example :
if op == "+":
result = num1 + num2
print("Your result is :",result)
if op == "-" :
result = num1 - num2
print("Your result is :",result)
# It will work fine.

Related

I build a calculator and I want to give error if they didn't type any number

Code works pretty well if you enter an actual number and operator and it actually gives u an error code if you enter an invalid operator but I want to give same error code with numbers but I dont know how I use python 3.9 help plz
num1 = float(input('Enter First Number:'))
op = input('Enter operator:')
num2 = float(input('Enter Second Number:'))
if op == '+':
print(num1 + num2)
elif op == '-':
print(num1 - num2)
elif op == '*':
print(num1 * num2)
elif op == '/':
print(num1 / num2)
else:
print('enter an operator plz')
You want to use try/catch statements and/or while loops around each step.
flag1,flag2,operator =True,True,True
while flag1:
try:
num1 = float(input('Enter first Number:'))
flag1 = False
except ValueError:
print("Enter a valid first number please")
while operator:
op = input('Enter operator:')
if not (op=='+' or op =='-' or op =='*' or op=='/'):
print('Enter a valid operator please')
else:
operator=False
while flag2:
try:
num2 = float(input('Enter second Number:'))
flag2 = False
except ValueError:
print("Enter a valid second number please")
if op == '+':
print(num1 + num2)
elif op == '-':
print(num1 - num2)
elif op == '*':
print(num1 * num2)
elif op == '/':
print(num1 / num2)
Maybe try something along the lines of this:
invalid = True
while invalid:
try:
num1 = float(input('Enter First Number:'))
op = input('Enter operator:')
num2 = float(input('Enter Second Number:'))
invalid = False
except ValueError:
print("Please enter a valid number")
if op == '+':
print(num1 + num2)
elif op == '-':
print(num1 - num2)
elif op == '*':
print(num1 * num2)
elif op == '/':
print(num1 / num2)
else:
print('enter an operator plz')

it is keep on printing [duplicate]

This question already has answers here:
What does "while True" mean in Python?
(18 answers)
Closed 2 years ago.
when i run this code it keeps on giving me the answer
here is my code
num1 = float(raw_input("enter a number: ")) # type: float
operation = str(raw_input("enter a operation: "))
num2 = float(raw_input("enter a number: ")) # type: float
while True:
if operation == "+":
print num1 + num2
elif operation == "-":
print num1 - num2
elif operation == "*":
print num1 * num2
elif operation == "/":
print (num1 / num2)
else:
print("Error Error")
What you might want is to put the input taking code into the while loop:
while True:
num1 = float(raw_input("enter a number: ")) # type: float
operation = str(raw_input("enter a operation: "))
num2 = float(raw_input("enter a number: ")) # type: float
if operation == "+":
print (num1 + num2)
elif operation == "-":
print (num1 - num2)
elif operation == "*":
print (num1 * num2)
elif operation == "/":
print (num1 / num2)
else:
print("Error Error")
`while True:` means Infinite Loop.
You can take input inside while loop or you can change the condition of while loop.
remove the while True: and it will only print out the answer once. while loops continue running as long as the argument is true and True is always true :P
What you probably want is for the application to keep calculating input from the user.
Try this
def calculate():
num1 = float(raw_input("enter a number: ")) # type: float
operation = str(raw_input("enter a operation: "))
num2 = float(raw_input("enter a number: ")) # type: float
if operation == "+":
print (num1 + num2)
elif operation == "-":
print (num1 - num2)
elif operation == "*":
print (num1 * num2)
elif operation == "/":
print (num1 / num2)
else:
print("Error Error")
while True:
calculate()

How do i write the users input in a file in python? [duplicate]

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}")

How can i make this part of my new code loop?

i want to make it so when you get your answer it automatically asks for another operator and another number until you type stop
like real calculators
from math import*
info = input("Would you like information? : ")
if info == "yes":`enter code here`
print("+ = add")
print("- = subtract")
print("* = multiplication")
print("/ = division")
print("pow/power/p = raise to power")
num1 = float(input("Enter your first number: "))
op = input("Enter your operator: ")
num2 = float(input("Enter your second number: "))
if op == "+":
print(num1 + num2)
elif op == "-":
print(num1 - num2)
elif op == "*":
print(num1 * num2)
elif op == "/":
print(num1 / num2)
elif op == "pow" or "power" or "p":
print(pow(num1, num2))
else:
print("Something went wrong please try again")
from math import *
info = input("Would you like information? : ")
if info == "yes":
print("+ = add")
print("- = subtract")
print("* = multiplication")
print("/ = division")
print("pow/power/p = raise to power")
while True:
num1 = input("Enter your first number: ")
if num1 == "stop":
break
num1 = float(num1)
op = input("Enter your operator: ")
num2 = float(input("Enter your second number: "))
if op == "+":
print(num1 + num2)
elif op == "-":
print(num1 - num2)
elif op == "*":
print(num1 * num2)
elif op == "/":
print(num1 / num2)
elif op == "pow" or "power" or "p":
print(pow(num1, num2))
else:
print("Something went wrong please try again")
print("Code finished")
It obviously does not take edge cases into concideration (like division by zero, making sure that the stuff that you pass to float() is actually a number) but this should get you going :)
cheers.

Python Calculator Code

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))

Categories

Resources