返回值不为';我不能在Python3中工作
似乎我无法将值从一个函数传递到另一个函数,即使我在第一个函数中放入了返回值不为';我不能在Python3中工作,python,return,Python,Return,似乎我无法将值从一个函数传递到另一个函数,即使我在第一个函数中放入了return语句 def SumPrice(): price = 0 TotalPrice = 0 if cup_cone=="cup": price=(price+(mass/10)*0.59)*TotalSet else: if cone_size=="small": price=(price+2)*TotalSet el
return
语句
def SumPrice():
price = 0
TotalPrice = 0
if cup_cone=="cup":
price=(price+(mass/10)*0.59)*TotalSet
else:
if cone_size=="small":
price=(price+2)*TotalSet
else:
if cone_size=="medium":
price=(price+3)*TotalSet
else:
price=(price+4)*TotalSet
if Member_Ans=="yes":
TotalPrice=TotalPrice+price*0.90
return price, TotalPrice
def PrintDetails():
price, TotalPrice = SumPrice()
if Member_Ans=="yes":
print("Member ID: ", loginID, " (" , Username, ")")
for element in range (len(UserFlavor)):
print (UserFlavor[element], "--- ", UserFlavorPercentage[element], "%")
print ("Total set = ", TotalSet)
print ("Total price = RM %.2f" % (price))
if Member_Ans=="yes":
print ("Price after 10%% discount = RM %.2f" % (TotalPrice))
while True:
Payment=int(input("Please enter your payment: "))
if Payment<TotalPrice:
print("Not enough payment.")
if Payment >= TotalPrice:
break
Balance=Balance+(Payment-TotalPrice)
print(Balance)
PrintDetails()
这是我的代码:
price=0
TotalPrice=0
def SumPrice(price,TotalPrice):
if cup_cone=="cup":
price=(price+(mass/10)*0.59)*TotalSet
else:
if cone_size=="small":
price=(price+2)*TotalSet
else:
if cone_size=="medium":
price=(price+3)*TotalSet
else:
price=(price+4)*TotalSet
if Member_Ans=="yes":
TotalPrice=TotalPrice+price*0.90
print(price,TotalPrice)
return (price)
return (TotalPrice)
def PrintDetails(price,TotalPrice,Balance):
SumPrice(price,TotalPrice)
if Member_Ans=="yes":
print("Member ID: ", loginID, " (" , Username, ")")
for element in range (len(UserFlavor)):
print (UserFlavor[element], "--- ", UserFlavorPercentage[element], "%")
print ("Total set = ", TotalSet)
print ("Total price = RM %.2f" % (price))
if Member_Ans=="yes":
print ("Price after 10% discount = RM %.2f" % (TotalPrice))
while True:
Payment=int(input("Please enter your payment: "))
if Payment<TotalPrice:
print("Not enough payment.")
if Payment >= TotalPrice:
break
Balance=Balance+(Payment-TotalPrice)
print(Balance)
PrintDetails(price,TotalPrice,Balance)
price=0
总价=0
def总价(价格、总价):
如果杯形圆锥=“杯形”:
价格=(价格+(质量/10)*0.59)*全套
其他:
如果圆锥体尺寸=“小”:
价格=(价格+2)*总集
其他:
如果圆锥体尺寸=“中等”:
价格=(价格+3)*全套
其他:
价格=(价格+4)*总集
如果成员为“是”:
总价=总价+价格*0.90
打印(价格、总价)
退货(价格)
退货(总价)
def打印详细信息(价格、总价、余额):
总价(价格、总价)
如果成员为“是”:
打印(“成员ID:,登录号,”(“,用户名,”))
对于范围(len(UserFlavor))中的元素:
打印(UserFlavor[元素],“--”,UserFlavorPercentage[元素],“%”)
打印(“Total set=”,TotalSet)
打印(“总价=RM%.2f”%(价格))
如果成员为“是”:
打印(“10%折扣后的价格=RM%.2f”%(总价))
尽管如此:
Payment=int(输入(“请输入您的付款:”)
如果付款=总价:
打破
余额=余额+(付款总价)
打印(余额)
打印详细信息(价格、总价、余额)
当我尝试打印
price
和TotalPrice
时,它会打印0
,为什么?您尝试使用return
两次,这是不允许的(您的函数将在到达第一条return
语句时立即结束,使另一条语句无效)。但是,您可以在一条语句中返回这两个值:
return (price, TotalPrice)
然后将该值指定给元组或任何您想要的内容:
my_tuple = SumPrice(a, b)
或
无法访问第一个函数的第二个
return
语句!顺便说一句,不要在代码中使用全局变量,而是访问第一个函数的返回值
def SumPrice():
price = 0
TotalPrice = 0
if cup_cone=="cup":
price=(price+(mass/10)*0.59)*TotalSet
else:
if cone_size=="small":
price=(price+2)*TotalSet
else:
if cone_size=="medium":
price=(price+3)*TotalSet
else:
price=(price+4)*TotalSet
if Member_Ans=="yes":
TotalPrice=TotalPrice+price*0.90
return price, TotalPrice
def PrintDetails():
price, TotalPrice = SumPrice()
if Member_Ans=="yes":
print("Member ID: ", loginID, " (" , Username, ")")
for element in range (len(UserFlavor)):
print (UserFlavor[element], "--- ", UserFlavorPercentage[element], "%")
print ("Total set = ", TotalSet)
print ("Total price = RM %.2f" % (price))
if Member_Ans=="yes":
print ("Price after 10%% discount = RM %.2f" % (TotalPrice))
while True:
Payment=int(input("Please enter your payment: "))
if Payment<TotalPrice:
print("Not enough payment.")
if Payment >= TotalPrice:
break
Balance=Balance+(Payment-TotalPrice)
print(Balance)
PrintDetails()
def SumPrice():
价格=0
总价=0
如果杯形圆锥=“杯形”:
价格=(价格+(质量/10)*0.59)*全套
其他:
如果圆锥体尺寸=“小”:
价格=(价格+2)*总集
其他:
如果圆锥体尺寸=“中等”:
价格=(价格+3)*全套
其他:
价格=(价格+4)*总集
如果成员为“是”:
总价=总价+价格*0.90
退货价格
def PrintDetails():
价格,总价格=总价格()
如果成员为“是”:
打印(“成员ID:,登录号,”(“,用户名,”))
对于范围(len(UserFlavor))中的元素:
打印(UserFlavor[元素],“--”,UserFlavorPercentage[元素],“%”)
打印(“Total set=”,TotalSet)
打印(“总价=RM%.2f”%(价格))
如果成员为“是”:
打印(“10%%折扣后的价格=RM%.2f”%(总价))
尽管如此:
Payment=int(输入(“请输入您的付款:”)
如果付款=总价:
打破
余额=余额+(付款总价)
打印(余额)
打印详细信息()
您不能返回两次。您的函数以到达的第一个返回语句结束。我已更改为仅返回“price”,但它仍然打印“0”。如何解决它?@Jess您必须准确描述您遇到的问题,以及我们能够帮助您的输入和预期输出很棒,使用我的答案并对我的帖子投了反对票?如果你暗示我复制了你的答案,那么你一定是在开玩笑,因为我在2分钟前回答了。我删除了全局变量,但结果是错误的(NameError:name'price'未定义)@Jess此行的缩进PrintDetails(price,TotalPrice,Balance)
不正确,现在试试这个代码。我在你提醒后加上了,但它仍然会弹出相同的窗口error@Jess我更新了代码,您不需要将变量传递给PrintDetails
函数,因为它从SumPrice
函数获取price
和TotalPrice
值。另外,我假设您在代码中的某个地方定义了cup\u cone
、Member\u Ans
和cone\u size
。我遵循您为我更改的所有代码,现在又发生了一个错误。UnboundLocalError:分配前引用的局部变量“price”