我的函数没有在其中打印修改后的变量



代码正在打印函数中的原始起始余额 5000 而不是修改后的余额 4900。

def Building(mon, house, office):
  print("A house costs 100 dollars and an office does too")
  building_type = input("House or office?: ")
  if building_type == "House":
    print("You have built your first building, a house!")
    mon -= 100
    house += 1
  elif building_type == "Office":
    print("You have constructed an office in your city!")
    mon -= 100
    office += 1
  else:
    print("Neither house or office was entered, enter again.")
    Building(mon, house, office)
Building(money, houses, offices)
print(money)

你应该返回变量mon。

if building_type=="House":
    # do calculations
    return (mon, house, office)
elif building_type=="Office":
    # do calculations
    return (mon, house, office)

然后

results = Building(money, house, office)
print(results[0]) # for money
print(results[1]) # for house
print(results[2]) # for office

最新更新