我在python上写了一个货币转换器,它说';货币';是时未定义。这发生在最后的乘法运算中
变量我在python上写了一个货币转换器,它说';货币';是时未定义。这发生在最后的乘法运算中,python,arrays,Python,Arrays,变量money实际上没有在xe1中定义,因为它的作用域仅限于函数。在更好的实现中,您可以将money作为两个函数的参数传递,如下所示: usd = 2 yen = 1000 gbp = 1.7 eur = 0.75 def again(): choice = False while choice == False: money = input("please enter an amount ") try: int(money) e
money
实际上没有在xe1
中定义,因为它的作用域仅限于函数。在更好的实现中,您可以将money作为两个函数的参数传递,如下所示:
usd = 2
yen = 1000
gbp = 1.7
eur = 0.75
def again():
choice = False
while choice == False:
money = input("please enter an amount ")
try:
int(money)
except ValueError:
print("inavlid: numbers only")
choice == False
else:
money = int(money)
if money >= 1000:
confirm = input("are you sure y / n").lower()
if confirm == "y":
transfer()
elif confirm == "n":
again()
elif confirm != "y" or choice != "n":
print("invalid options")
yn()
else:
transfer()
def xe1():
xe = input("do you want currency rates set on this program(yes) or\ndo you want to make your own currency amounts(no)\n")
if xe == 'yes':
print ("you have got from ", (money), c, "to ", ((money)/c1*ac1),ac)
quit()
elif xe == 'no':
er = (float(input("what is the exchange rate ")))
print ("you have got from ", (money), c, "to ", ((money)*er), ac)
quit()
else:
xe1()
没错,money
没有在xe1
中定义,也没有作为参数传递给它。我建议您阅读并遵循Python教程。有一个很好的答案应该会有所帮助。
usd = 2
yen = 1000
gbp = 1.7
eur = 0.75
def again(money):
choice = False
while choice == False:
money = input("please enter an amount ")
try:
int(money)
except ValueError:
print("inavlid: numbers only")
choice == False
else:
money = int(money)
if money >= 1000:
confirm = input("are you sure y / n").lower()
if confirm == "y":
transfer()
elif confirm == "n":
again(money)
elif confirm != "y" or choice != "n":
print("invalid options")
yn()
else:
transfer()
def xe1(money):
xe = input("do you want currency rates set on this program(yes) or\ndo you want to make your own currency amounts(no)\n")
if xe == 'yes':
print ("you have got from ", (money), c, "to ", ((money)/c1*ac1),ac)
quit()
elif xe == 'no':
er = (float(input("what is the exchange rate ")))
print ("you have got from ", (money), c, "to ", ((money)*er), ac)
quit()
else:
xe1(money)