Click here to Skip to main content
14,773,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 11-Jan-21 7:40am

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.
   
v7
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.
   
Comments
Member 12971412 28-Jan-17 7:02am
   
Hey, thanks. That's just made my day a whole lot better!
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)
   
Quote:
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

Message is rather explicit, it tell you that line 9 start with a mix of tabs and spaces. PHP can only deal with 1 kind of them at a given time.
Look at your code, the second line should start with 2 tabs:
<tab>elif other == "y":
<space><space><space><space><tab>z = input("Please input your next number:")


Professional text editors can make visible those chars.
Otherwise, put the cursor on left and use right arrow, if cursor moves 1 position, it is a space, if cursor moves to next column which is multiple of 4, it is a tab.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900