2

Hi I am having trouble pulling a variable out of a function and then using it in a mathematical equation. I can use the function usdExRateRand no problem but when try to use the value usdExRate from the function in a nested if statement it only uses the initial usdExRateRand not the newly defined one from the while loop how do i fix this?

import random

def whiles():
    cur1 = "NZD"
    cur2 = "USD"
    usdExRate = round(random.random() * (0.909 - 0.101) + 0.101, 3)
    print ("usdExRate Initial = $", usdExRate)
    nzdExRate = round(random.random() * (1.909 - 1.101) + 1.101, 3)
    print ("nzdExRate Initital = $", nzdExRate)
    amt = 1000
    amt2 = 0
    amtPrev = 0
    amt2Prev = 0

    def usdExRateRand():
        usdExRate = round(random.random() * (0.909 - 0.750) + 0.750, 3)
        print ("USD Ex Rate = $", usdExRate,"\n")
    usdExRateRand()

    def nzdExRateRand():
        nzdExRate = round(random.random() * (1.505 - 1.101) + 1.101, 3)
        print ("NZD Ex Rate = $", nzdExRate, "\n")
    nzdExRateRand()

    while amt > 0:
        def trade1():
    #       usdExRate = round(random.random() * (0.909 - 0.101) + 0.101, 3)
            print ("TRADE 1")
            usdExRateRand()
            if usdExRate > 0:
    #       def trade1():
    #       usdExRate = round(random.random() * (0.909 - 0.101) + 0.101, 3)  
    #       print (usdExRate)
                if (usdExRate * amt > amt2Prev):
                    print ("My Amount to trade =", cur1," $", amt)
                    print ("my ",cur2,"rate = $", usdExRate)
                    trd1 = amt * usdExRate
                    print ("trade1", cur1,"$", amt," * ",cur2,"$",usdExRate," = $", trd1, "\n")
                    amt2 = trd1
                    amtPrev = amt

                else:
                    print ("error reluts in a loss\n")
                    trade1()
        trade1()

        def trade2():
            print ("TRADE 2")
            nzdExRateRand()
            if nzdExRate > 0:
                if (nzdExRate * amt2 > amtPrev):
                    print ("My Amount to trade =", cur2," $",amt2,"\n")
                    print ("my", cur1, "rate = $", nzdExRate)
                    trd2 = amt2 * nzdExRate
                    print ("trade2", cur2,"$", amt2," * ",cur1,"$",nzdExRate," = $", trd2, "\n")
                    amt1 = trd2
                    amt2Prev = amt2
                else:
                    print ("error results in a loss\n")

        trade2()

#            amtPrev = amt
#            def usdExRateom2():
#                usdExRate2 = round(random.random() * (1.909 - 1.101) + 1.101, 3)
#                print ("my nzd rate", usdExRate2)
#            
#            if (trd1 * usdExRate2 < amtPrev):
#                
#                usdExRate2 = round(random.random() * (1.909 - 1.101) + 1.101, 3)
#                
#            else:
#                trd2 = trd1 * usdExRate2
#                print ("trade2 =", trd2)
#            usdExRateom2()  




whiles()

hopefully i got my indentation right this time when copying over the code the code works in spyder and i get this output as you can see after "my amount to trade" the usd ex rate is always 0.3 not the value above.

TRADE 1
USD Ex Rate = $ 0.761 

My Amount to trade = NZD  $ 1000
my  USD rate = $ 0.3
trade1 NZD $ 1000  *  USD $ 0.3  = $ 300.0 

TRADE 2
NZD Ex Rate = $ 1.178 

error results in a loss

TRADE 1
USD Ex Rate = $ 0.772 

My Amount to trade = NZD  $ 1000
my  USD rate = $ 0.3
trade1 NZD $ 1000  *  USD $ 0.3  = $ 300.0 

TRADE 2
NZD Ex Rate = $ 1.296 

what i would like to do is.

def trade():
    usdExRaterand()

this should give me a new usdExRate variable I can then use in the following if statement

5
  • i tried calling usdExRateRand in the actual if statement and had the same problem Commented Jul 25, 2017 at 3:33
  • Do you know how to return values from a function with the return statement and also how to pass values to functions? The usdExRateRand function should return the usdExRate and then you can assign this value to the usdExRate variable in the while loop. --- Your program is structured in a strange way and I suggest to move all the function definitions out of the whiles function first. Nesting functions in other functions can be useful, but beginners shouldn't do that. Commented Jul 25, 2017 at 13:55
  • Here's a nice intro to functions. Commented Jul 25, 2017 at 14:51
  • I wanted to write an example for you, but I'm still trying to figure out what your code is supposed to do. ;) Commented Jul 25, 2017 at 20:56
  • it is supposed to provide an ever increasing value in either nzd or usd based upon the random exchange rates, where if i have say 1000 nzd and trade to usd and get 800 based upon an ex rate of 0.800 it then store the previous nzd rate of 1000 and attempts to trade the 800 back to nzd but only does so if the new nzd amount is more than 1000. Then it attempts to trade the new nzd rate say 1015 back to usd and it must be more than 800 and so on and so on i would like it to do so infinitely taking me from thousand ever increasing upwards you do this code you free to use it Commented Jul 25, 2017 at 23:11

1 Answer 1

1

Here's a very vague example that should only demonstrate how you can pass values to your functions and how to return values. I'm not sure what you want to achieve, so please provide more information and I'll update this post.

import random


def usdExRateRand():
    return round(random.random() * (0.909 - 0.750) + 0.750, 3)


def nzdExRateRand():
    return round(random.random() * (1.505 - 1.101) + 1.101, 3)


def trade1(usdExRate, amtPrev):
    if usdExRate * amt > amtPrev:
        # Do your calculations.
    # Then return the updated amt and previous amt. (If you need them.)
    return amt, amtPrev


def trade2(nzdExRate, amtPrev):
    if nzdExRate * amt > amtPrev:
        # Do your calculations.
    # Then return the updated amt and previous amt.
    return amt, amtPrev


def main():
    amt = 1000
    amtPrev = amt

    while amt > 0:
        usdExRate = usdExRateRand()
        nzdExRate = nzdExRateRand()
        print("USD Ex Rate = $", usdExRate)
        print("NZD Ex Rate = $", nzdExRate)
        amt, amtPrev = trade(usdExRate, amtPrev)
        amt, amtPrev = trade2(nzdExRate, amtPrev)


main()
Sign up to request clarification or add additional context in comments.

6 Comments

thankx wil try i have been doing tuts but they normally provide code which is too complicated for me to then try with this thank you again skrx money here we c me
tried your code with this in the trade1() unedr the if statement trd1 = float(amt * usdExRate) print (trd1) amtPrev = amt amt = trd1trd1 = float(amt * usdExRate) print (trd1) amtPrev = amt amt = trd1
still didnt workkocal variable amt referenced before assignment so itreid assignin it at the top also i dont understand what the last twolines are doing amt, amtPrev = trade(usdExRate, amtPrev) amt, amtPrev = trade2(nzdExRate, amtPrev)
it all looks great but i guess im just not smart/ experienced enough to understand fully how to get to work? thanks for your attempts at help
i understand most of it and how you are implementing the random functionality and then calling it but am a little unclear on it also like i dont know how to use it confidently
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.