Hallo Ich habe Probleme, eine Variable aus einer Funktion ziehen und dann in einer mathematischen Gleichung verwenden. Ich kann die Funktion usdExRateRand kein Problem verwenden, aber wenn versuchen, den Wert usdExRate von der Funktion in einer verschachtelten if-Anweisung zu verwenden, verwendet es nur die ursprüngliche usdExRateRand nicht die neu aus der While-Schleife, wie kann ich das beheben?Ziehen einer Variablen aus einer Funktion in einer While-Schleife und verschachtelte if-Anweisung in Python
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()
hoffentlich habe ich meine Einbuchtung diesmal richtig, wenn über den Code zu kopieren Sie den Code in spyder funktioniert und ich bekomme diese Ausgabe, wie Sie nach sehen die usd ex-Rate „meine Betrag zu handeln“ ist immer 0,3 nicht die Wert oben.
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
was ich tun möchte ist.
def trade():
usdExRaterand()
sollte geben Sie mir eine neue usdExRate Variable kann ich dann in der folgenden if-Anweisung
ich versuchte usdExRateRand in dem eigentlichen if-Anweisung und hatte das gleiche Problem Aufruf –
Wissen Sie, wie Werte aus einer Funktion zurück mit der 'return' Aussage und auch, wie die Werte an Funktionen übergeben? Die 'usdExRateRand' -Funktion sollte die' usdExRate' zurückgeben und dann können Sie diesen Wert der 'usdExRate' -Variable in der while-Schleife zuweisen. --- Ihr Programm ist auf eine seltsame Art und Weise strukturiert und ich schlage vor, alle Funktionsdefinitionen zuerst aus der Whites-Funktion zu entfernen. Verschachtelungsfunktionen in anderen Funktionen können nützlich sein, aber Anfänger sollten das nicht tun. – skrx
[Hier ist] (http://programarcadegames.com/index.php?chapter=functions&lang=de#section_9) ein nettes Intro zu Funktionen. – skrx