lundi 24 juillet 2017

pulling a variable out of a function in a while loop and nested if statement in python

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 

Aucun commentaire:

Enregistrer un commentaire