如何在python中正确使用函数及其语法?
Posted
技术标签:
【中文标题】如何在python中正确使用函数及其语法?【英文标题】:How to properly use a function and it's syntax in python? 【发布时间】:2022-01-15 10:02:25 【问题描述】:目前我正在开发一个基本的文本游戏,您可以选择与狼战斗的武器,从字典中驱动谁的健康,您可以选择的武器的统计数据也是如此。现在我想做的是设置它,这样我就不需要为整个伤害想法重复相同的代码代码,我想把它写在一个函数中,这样我就可以为每种武器类型回调函数并节省空间并减少代码。如果有人可以向我展示如何做到这一点,和/或对我的代码有任何建议,以及如何在不使用大量面向对象编程的情况下使其更短,那将意义重大。
感谢任何人提供帮助或只是阅读本文并抽出时间。
wolf = enemies_animals["animals"]["wolf"]["health"]
user = input("Write down your username: ")
time.sleep(0.5)
userInput = input("Welcome, for this test please choose either to attack, or to run: ")
time.sleep(0.5)
if userInput.lower() == "attack":
time.sleep(0.5)
weapon_type = input("Choose which type of weapon to use (melee/long_range/throwable): ")
if weapon_type.lower() == "melee":
time.sleep(0.5)
weapon_list = []
for key in weapons[weapon_type]:
weapon_list.append(key)
print(f'Choose from weapons: weapon_list')
time.sleep(0.5)
weapon = input("choose which weapon to use(seen from the list above): ")
critical_chance = weapons[weapon_type][weapon]["critical_chance"]
if random.randint(1, 100) == weapons[weapon_type][weapon]["critical_chance"]:
total_damage = ((weapons[weapon_type][weapon]["damage"] + weapons[weapon_type][weapon]["strength"]) / 2) * 3 * weapons[weapon_type][weapon]["critical_multiplier"]
attack = wolf - total_damage
if attack <= 0:
dead_wolf = wolf
if random.randint(1, 5) == 5:
time.sleep(0.5)
print(f"user killed a wolf with a critical hit and got it's meat! ")
else:
time.sleep(0.5)
print(f"user killed the wolf with a critical hit!")
else:
time.sleep(0.5)
print(f"The wolf has attack health! ")
else:
total_damage = ((weapons[weapon_type][weapon]["damage"] + weapons[weapon_type][weapon]["strength"]) / 2) * 3
attack = wolf - total_damage
if attack <= 0:
dead_wolf = wolf
if random.randint(1, 5) == 5:
time.sleep(0.5)
print(f"user killed a wolf with a critical hit and got it's meat! ")
else:
time.sleep(0.5)
print(f"user killed the wolf with a critical hit!")
else:
time.sleep(0.5)
print(f"The wolf has attack health! ")
elif weapon_type == "long_range":
time.sleep(0.5)
weapon_list = []
for key in weapons[weapon_type]:
weapon_list.append(key)
print(f'Choose from weapons: weapon_list')
time.sleep(0.5)
weapon = input("choose which weapon to use(seen from the list above): ")
critical_chance = weapons[weapon_type][weapon]["critical_chance"]
if random.randint(1, 100) == weapons[weapon_type][weapon]["critical_chance"]:
total_damage = ((weapons[weapon_type][weapon]["damage"] + weapons[weapon_type][weapon][
"strength"]) / 2) * 3 * weapons[weapon_type][weapon]["critical_multiplier"]
attack = wolf - total_damage
if attack <= 0:
dead_wolf = wolf
if random.randint(1, 5) == 5:
time.sleep(0.5)
print(f"user killed a wolf with a critical hit and got it's meat! ")
else:
time.sleep(0.5)
print(f"user killed the wolf with a critical hit!")
else:
time.sleep(0.5)
print(f"The wolf has attack health! ")
else:
total_damage = ((weapons[weapon_type][weapon]["damage"] + weapons[weapon_type][weapon]["strength"]) / 2) * 3
attack = wolf - total_damage
if attack <= 0:
dead_wolf = wolf
if random.randint(1, 5) == 5:
time.sleep(0.5)
print(f"user killed a wolf with a critical hit and got it's meat! ")
else:
time.sleep(0.5)
print(f"user killed the wolf with a critical hit!")
else:
time.sleep(0.5)
print(f"The wolf has attack health! ")
elif weapon_type == "throwable":
time.sleep(0.5)
weapon_list = []
for key in weapons[weapon_type]:
weapon_list.append(key)
print(f'Choose from weapons: weapon_list')
time.sleep(0.5)
weapon = input("choose which weapon to use(seen from the list above): ")
critical_chance = weapons[weapon_type][weapon]["critical_chance"]
if random.randint(1, 100) == weapons[weapon_type][weapon]["critical_chance"]:
total_damage = ((weapons[weapon_type][weapon]["damage"] + weapons[weapon_type][weapon][
"strength"]) / 2) * 3 * weapons[weapon_type][weapon]["critical_multiplier"]
attack = wolf - total_damage
if attack <= 0:
dead_wolf = wolf
if random.randint(1, 5) == 5:
time.sleep(0.5)
print(f"user killed a wolf with a critical hit and got it's meat! ")
else:
time.sleep(0.5)
print(f"user killed the wolf with a critical hit!")
else:
time.sleep(0.5)
print(f"The wolf has attack health! ")
else:
total_damage = ((weapons[weapon_type][weapon]["damage"] + weapons[weapon_type][weapon]["strength"]) / 2) * 3
attack = wolf - total_damage
if attack <= 0:
dead_wolf = wolf
if random.randint(1, 5) == 5:
time.sleep(0.5)
print(f"user killed a wolf with a critical hit and got it's meat! ")
else:
time.sleep(0.5)
print(f"user killed the wolf with a critical hit!")
else:
time.sleep(0.5)
print(f"The wolf has attack health! ")
else:
time.sleep(0.5)
print(f"user, choose to run away!")
【问题讨论】:
你的代码是 500 行。没有人会想要看透这一切。你确定不能发minimal reproducible example? 我把代码变小了,完全忘记了它有500行。 在我看来这更适合代码审查 SE (codereview.stackexchange.com)。如果您对函数有基本的疑问,网上有很多很好的资源。 好的,谢谢您的意见。这是我第一次写或使用类似的东西,所以我需要习惯它。 【参考方案1】:对于第一种方法,您可以使用类。 您可以使用此链接找到一个简单的教程 https://www.w3schools.com/python/python_classes.asp
【讨论】:
非常感谢,我会调查的。以上是关于如何在python中正确使用函数及其语法?的主要内容,如果未能解决你的问题,请参考以下文章
c语言中 如何单独运行一个子函数啊?或者说怎样验证一个子函数的正确性、以及其功能啊?求助···