variables - About num1 and num2 in python calculator -
why works if isn't there variable says value of num1 , num2 variables? mean, shouldn't necessary add input num1 , num2? quite confused , new @ coding
{
#returns sum of num1 , num2 def add(num1, num2): return num1 + num2 #returns subtracting of num1 , num2 def sub(num1, num2): return num1 - num2 #returns multiplying of num1 , num2 def mul(num1, num2): return num1 * num2 #returns dividing of num1 , num2 def div(num1, num2): return num1 / num2 def main(): operation = input("what want (+,-,*,/): ") if(operation != "+" , operation != "-" , operation != "*" , operation != "/"): #invalid operation print("you must enter valid operation") else: var1 = int(input("enter num1: ")) var2 = int(input("enter num2: ")) if(operation == "+"): print(add(var1, var2)) elif(operation == "/"): print(div(var1, var2)) elif(operation == "-"): print(sub(var1, var2)) else: print(mul(var1, var2)) main()}
you have input in code
the 2 lines are:
var1 = int(input("enter num1: ")) var2 = int(input("enter num2: "))
read 2 documentation blocks on python built-ins
- input() : https://docs.python.org/2/library/functions.html#input
- int() : https://docs.python.org/2/library/functions.html#int
then need understand how function calls work
the names assign parameters of function valid inside declaration, , not need same on code.
def my_function(fn_variable_name): return fn_variable_name + 1 # calling directly print my_function(1) # outputs 2 # calling variable another_variable_name = 45 print my_function(another_variable_name) # outputs 46 # storing result in variable yet_another_variable_name = my_function(32) print yet_another_variable_name # outputs 33 # combining 2 var_1 = 23 var_2 = my_function(var_1) print var_1 # outputs 23 print var_2 # outputs 24
Comments
Post a Comment