Jump to content

Python code help

JaynineReturns
Go to solution Solved by msevilgenius,

print(mul(var1, var2)  <- there is a ')' missing

I'm trying to run this code right now in my cmd window.

#Returns the sum of num1 and num2
def add(num1, num2):
	return num1 + num2

#Returns num1 minus num2
def sub(num1, num2):
	return num1 - num2

#Returns the product of num1 and num2	
def mul(num1, num2):
	return num1 * num2

#Returns num1 divided by num2
def div(num1, num2):
	return num1 / num2

#Main function
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(sub(var1, var2))
        elif(operation == '*'):
            print(mul(var1, var2)
        else:
            print(div(var1, var2))
main()

_________________

The last else statement is the problem apparently. There is a syntax error? I just this summer started learning python and I'm still learning and I'm just kind of stuck here. I tried changing it to elif(operation == '/') but that gives me syntax error as well. Any help would be appreciated!

I'm using notepad++ to write the code and then the cmd to execute.

Link to comment
Share on other sites

Link to post
Share on other sites

@msevilgenius spotted the problem, but I'll add a quick stylistic note.  In Python, if/elif/else are statements, not functions, so you don't need parentheses around what follows them.  You can do if(expression) if you want, but it doesn't usually look very good to Python people, especially not with a single expression like if(x < 5).  Instead, your main() function would look a bit more Pythonic if it were written like this:

 

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(sub(var1, var2))
        elif operation == '*' :
            print(mul(var1, var2)
        else:
            print(div(var1, var2))

You could also make the if statement that makes sure they have a valid input a bit more compact by writing it as if operation not in ['+', '-', '*', '/'].  Again, this doesn't affect how your code runs, it just makes it look a bit nicer.

Link to comment
Share on other sites

Link to post
Share on other sites

On 7/9/2016 at 4:37 PM, GreezyJeezy said:

use code tags fam

??? Like on here? I tagged this post python. Or are you talking about something else?

On 7/9/2016 at 4:55 PM, msevilgenius said:

print(mul(var1, var2)  <- there is a ')' missing

Oh wow. I done goofed haha. Thanks I totally missed that!

On 7/10/2016 at 2:05 AM, Azgoth 2 said:

@msevilgenius spotted the problem, but I'll add a quick stylistic note.  In Python, if/elif/else are statements, not functions, so you don't need parentheses around what follows them.  You can do if(expression) if you want, but it doesn't usually look very good to Python people, especially not with a single expression like if(x < 5).  Instead, your main() function would look a bit more Pythonic if it were written like this:

 


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(sub(var1, var2))
        elif operation == '*' :
            print(mul(var1, var2)
        else:
            print(div(var1, var2))

You could also make the if statement that makes sure they have a valid input a bit more compact by writing it as if operation not in ['+', '-', '*', '/'].  Again, this doesn't affect how your code runs, it just makes it look a bit nicer.

Thank you for the tip! I didn't know that. Thank you thank you. :)

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, JaynineReturns said:

??? Like on here? I tagged this post python. Or are you talking about something else?

Oh wow. I done goofed haha. Thanks I totally missed that!

Thank you for the tip! I didn't know that. Thank you thank you. :)

Code tags in your formatting.. Click code then paste the code 

 

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, GreezyJeezy said:

Code tags in your formatting.. Click code then paste the code 

Oh I see now! Sorry I didn't notice that before. Thanks for letting me know! I'll fix it. Thank you for the tip. :)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×