why the def function does not calculate the same answer with my last print row?

which of the following enclose the input parameters or arguments of a function?
python function return multiple values
if return statement is not used inside the function the function will return
how to use return value in another function python
python function return list
python function return variable
how to print the result of a function in python
python function always returns a value true or false

Just want to know why the last two commands have different returns The return should come out with the value 600 rather than 100 for the exchange function.

unit ={"HKD":1, "USD":7.8, "CAN":6}
price = 100

for cur, rate in unit.items():
    print (cur, rate)

def exchange(price, cur):
  for (cur, rate) in unit.items():
    if cur=="HKD":
      exp = unit["HKD"]*price
    elif cur=="USD":
      exp = unit["USD"]*price
    else:
      exp = unit["CAN"]*price
    return print(exp)

exchange(100, "CAN")
print(unit["CAN"]*price)

just use the dictionary properly:

def exchange(price, cur):
   return unit[cur]*price

Why your code doesn't work? Beside the inefficient aspect, you're shadowing your cur parameter when loopîng on the dictionary keys.

Always ask yourself "why am I looping on a dictionary to perform a lookup when a dictionary is made for lookup without loops"

Some Tips, Tricks, and Common Errors — How to Think Like a , (Built-in functions are not privileged like keywords: we can define our own variable or Quiz: Is the function f(z) = 3z + 5 the same as function f above? So line 6 should not be there, and line 7 has to be outside the loop. because Python reached the last statement), the stack frame and its local variables are all destroyed. To print the spreadsheet down to the last row in column E which has any cell content. Except that I need code to determine which of columns D or E is the last one that has any content and then use that row as the last row to print down to. Note that this code does not print any columns which may be hidden which is what I want it to do.

Update your function, it should just return the exp rather then print

def exchange(price, cur):
    for (cur, rate) in unit.items():
        if cur == "HKD":
            exp = unit["HKD"] * price
        elif cur == "USD":
            exp = unit["USD"] * price
        else:
            exp = unit["CAN"] * price
        return exp

3. Functions — How to Think Like a Computer Scientist: Learning , def new_line(): print # a print statement with no arguments prints a new line Our first examples have an empty parameter list, so the function calls do not take any arguments. We could call the same function repeatedly: general, you will want to choose a name for your parameters that describes their use in the function. I just moved to Excel 2013 and am having the same problem but flipped. I have used Excel for years and am a power user. This worked last week when I was on Excel 2010. I am trying to auto fill a number sequence in a ROW and it does not work. The Auto Fill Option button that should show up at the lower right corner of the selection range is not

You're using the same name cur as a function parameter as well as the iterator. Hence the parameter passed into the function is no longer the value of the variable cur.

In the first iteration of the loop the value of cur becomes "HKD" which causes the first if statement to be correct. It will calculate *exp = unit["HKD"]price which is equal to 100.

Also, in your code the return statement is inside the for loop and hence in the first iteration itself the looping stops and the value of exp is 100 when it is printed.

This problem can be rectified as follows:

unit ={"HKD":1, "USD":7.8, "CAN":6}
price = 100

for cur, rate in unit.items():
    print (cur, rate)

def exchange(price, curr):
    for (cur, rate) in unit.items():
        if curr=="HKD":
            exp = unit["HKD"]*price
        elif curr=="USD":
            exp = unit["USD"]*price
        else:
            exp = unit["CAN"]*price
        return print(exp)

exchange(100, "CAN")
print(unit["CAN"]*price)

6.2. Functions that Return Values — How to Think like a Computer , Most functions require arguments, values that control how the function does its job. want to find the absolute value of a number, you have to indicate what the number is. In the last example, 503 is returned, since it is larger than 33, 125, and 1. The answer is that function definition is not the same as function execution. Create a function to calculate the different w.r.t. value the year before at the same day and month. This function would be invoked on each row of data frame. def calChange(row): change = 0 val_prev_yr = df_dateInx.loc[row.Date - 1]['min'] val_this_row = row['min'] # do anything with values and return change return change

You are defining "cur" variable again in for loop in your function. In another sense, you have defined cur="can" while starting function, when python enter the function you have over-write cur variable as defining it again in for loop.

I guess it will work!

Your Guide to the Python print() Function – Real Python, Print Is a Function in Python 3; print Was a Statement in Python 2 can use it to display formatted messages onto the screen and perhaps find line 1, in < module> 'My age is ' + 42 TypeError: can only concatenate str (not In the latter case, you want the user to type in the answer on the same line: Are def delta(a, b, c): . If I use this loop and calculate on a merged dataframe and it finds a Nan it works but only to the row with Nan. No errors are thrown, If I try a fillNa I get AttributeError: 'numpy.float64' object has no attribute 'fillna' Is there any way to skip the row with Nan or set values to zero? – ctrl-alt-delete Jan 18 '16 at 16:04

Deriving New Columns & Defining Python Functions, Make new columns from existing data and build custom functions. This lesson builds on the pandas DataFrame data type you learned about in a previous lesson. To get the feel for this, start by creating a new column that is not derived from Instead, you'll use functions to determine the value in each row of your new� 80. If a function does not have a return statement, is it valid? A function that doesn’t return anything returns a None object. Not necessarily does the return keyword mark the end of a function; it merely ends it when present in the function. Normally, a block of code marks a function, and where it ends, the function body ends.

Python: user defined function, Python: user defined function: In all programming and scripting language, In Python concept of function is same as in other languages. functions. Last update on February 28 2020 12:16:39 (UTC/GMT +8 hours) Line 2 : Calculate and return the value of (x + y)2 For second print statement [ Line no 4] My vlookup is only working for the top half of my spreadsheet and then stops working on any cell past row 270. My vlookup table is a named range, first column in order by dates, returning column is to the left of the reference column and formatting matches and is not text.

Defining Functions in Python 3, Because the function definition maintains the same order of print() statements, if we use keyword arguments, it does not matter which order we� In this step-by-step tutorial, you'll learn about the print() function in Python and discover some of its lesser-known features. Avoid common mistakes, take your "hello world" to the next level, and know when to use a better alternative.

Comments
  • you don't want to do return print(exp) you want to do return exp
  • that code is also wasting a dictionary… just access the dictionary. Dont loop on it. And it will work
  • In exchange you use cur for both the input parameter and for iterating over the keys of your dict in the for-loop. Change one of them to something else.