Hello coding community!
I am 9 years old and love programming in Python and this is my first time posting - so wish me luck!
I have just written a simple calculator - in Python - and I mainly struggle with concentration on writing the actual code (normally I spend ages printing long lines telling the user what to do - I should do that the end!). I am also trying to do this with as little help as possible to see if I can independently (on my own) code. But anyway, I keep on receiving this indentation error:Traceback (most recent call last):
File "python", line 9
z = input("Please input your next number:")
TabError: inconsistent use of tabs and spaces in indentation
Please help quickly otherwise, I can't test my program to see if it works!
I would be grateful for any help I can get.
This is my whole code:
#A simple calculator - I am doing this entirely without help and nonsanse such as printing, so here goes!
def calculator(x, y, z):
operation = input("a = Add, s = Subtract, m = Multiply, d = Divide")
if operation == "a":
x = input("First number:")
y = input("Second number:")
other = input("Any other numbers?")
elif other == "y":
z = input("Please input your next number:")
return x + y + z
else:
return x + y
if operation == "s":
x = input("First number:")
y = input("Second number:")
return x - y
if operation == "m":
x = input("First number:")
y = input("Second number:")
return x * y
if operation == "d":
x = input("First number:")
y = input("Second number:")
return x / y
print (calculator)
calculator(x, y)
What I have tried:
I have tried putting the elif statement inside the if, and same with the else statement.
解决方案The elif and else block is to check the input for "other", so it should be an if else block and indented like the "other" variable.
You could have use elif for other "operation" values as they belong to the same chain of flow.
One last point, it serves no purpose to use a function in your case, so replace all the return statements with print with do.
Try this
def calculator(x, y, z):
operation = input("a = Add, s = Subtract, m = Multiply, d = Divide")
if operation == "a":
x = input("First number:")
y = input("Second number:")
other = input("Any other numbers?")
if other == "y": #this is a new if block
z = input("Please input your next number:")
return x + y + z
else:
return x + y
elif operation == "s": #this continues the "if operation ..." above
x = input("First number:")
y = input("Second number:")
return x - y
elif operation == "m": #so does this
x = input("First number:")
y = input("Second number:")
return x * y
elif operation == "d": #so does this
x = input("First number:")
y = input("Second number:")
return x / y
print (calculator)
calculator(x, y)
You have also declared calculator as taking three parameters (x,y,z), but then you ask the user to type them in. Also your call at the bottom of the code only sends x and y.
However, I have to say, for a 9 year old that is some pretty good code, keep it up.
By the way all the solution above does not work. You can't declare arguments call for function that is going to get user input. Because to call that function you have to pass argument for it so it doesn't throw error.
Secondly, if you need a string input you call raw_input function. If you need integer number, you call the input function. This is the working code of that calculator and I wrote it as easiest to read as possible. Have fun and good luck If you need programming help, look for my articles. I write for people that just learning to the very expert one. It take me a week to go through one subject but if you read my article you can find out which programming language which suit you.
Here is the working program. With one loop of built in error check to show you how it is done.
def calculator():
operation = raw_input("a = Add, s = Subtract, m = Multiply, d = Divide:")
while (operation is not "a" and operation is not "s" and operation is not "m" and operation is not "d" and operation is not "q"):
print("Incorrect input, please try again. Type q to quit" )
operation = raw_input("a = Add, s = Subtract, m = Multiply, d = Divide:")
if operation is "q":
return;
if operation is "a":
x = input("First number:")
y = input("Second number:")
other = raw_input("Any other numbers?")
if other == "y":
z = input("Please input your next number:")
return x + y + z
else:
return x + y
if operation is "s":
x = input("First number:")
y = input("Second number:")
return x - y
if operation == "m":
x = input("First number:")
y = input("Second number:")
return x * y
if operation is "d":
x = input("First number:")
y = input("Second number:")
return x / y
a = calculator()
print(a)