''' 需求: 已知1颗六级石头的市场售价为750金,请问是自己合成石头划算还是直接购买划算 ''' # 购买1级石头 def buy_l1(l1_number): gold = l1_number * 0.75 diamond = l1_number * 8 return gold, diamond # 合成3级石头 def l1_to_l3(l3_number): gold, diamond = buy_l1(l3_number * 12) gold += l3_number * 0.39 vit = l3_number * 10 return gold, diamond, vit # 合成4级石头 def l3_to_l4(l4_number): # 需要先有相应的3级石头 gold, diamond, vit = l1_to_l3(l4_number) # 按照成功的前提计算出实际消耗的1级石头 count = l4_number * 16 wastage = count // 0.4878 # 购买实际消耗的1级石头 gold_l4, diamond_l4 = buy_l1(wastage) gold += gold_l4 diamond += diamond_l4 # 计算实际消耗的体力值 vit = (wastage//16) * l4_number gold += l4_number * 0.897 return gold, diamond, vit # 合成6级石头 def l4_to_l6(l6_number): # 需要先有相应的4级石头 gold, diamond, vit = l3_to_l4(l6_number * 12) gold += l6_number * 19.75 vit += l6_number * 10 return gold, diamond, vit # 根据需要合成的数量与石头的级别,得到合成所需要消耗的金 def synthesis(ln_name, number): if ln_name == 'L3': gold, diamond, vit = l1_to_l3(number) elif ln_name == 'L4': gold, diamond, vit = l3_to_l4(number) elif ln_name == 'L6': gold, diamond, vit = l4_to_l6(number) else: print("输入有误!") gold += vit gold += diamond*0.05 return gold # 处理用户的输入 def user_input(): print("1.计算合成五行石所耗费的金", "\t", "2.计算购买1级五行石所耗费的金") input_str = input("请输入:") if input_str == "1": print("\n1.合成3级五行石", "\t", "2.合成4级五行石", "\t", "3.合成6级五行石") input_str = input("请输入:") num = int(input("请输入需要合成的数量:")) if input_str == "1": gold = synthesis('L3',num) print('\n需要消耗:', gold, '金') elif input_str == "2": gold = synthesis('L4',num) print('\n需要消耗:', gold, '金') elif input_str == "3": gold = synthesis('L6',num) print('\n需要消耗:', gold, '金') else: print("输入错误!") elif input_str == "2": num = int(input("请输入购买数量:")) gold, diamond = buy_l1(num) print("需要消耗", gold, '金,', diamond, '个钻石。') else: print("输入错误!") user_input()
运行结果:
PS E:\PythonProject> python '.\test_function\homework.py' 1.计算合成五行石所耗费的金 2.计算购买1级五行石所耗费的金 请输入:1 1.合成3级五行石 2.合成4级五行石 3.合成6级五行石 请输入:3 请输入需要合成的数量:1 需要消耗: 950.7439999999999 金 PS E:\PythonProject>