我是一个相对较新的程序员,我正在从事一个大学项目,我希望能够使用文件处理来存储我的一些代码之外的工作,但我不太确定如何,最好我想将我的用户定义函数存储在一个文件中,但我不太确定如何去做或者甚至可能。
我想在外部存储的代码如下:
#This is my combat module, I Call on this every time the user enters combat
def combat():
print("""To defeat the enemy you must hit a higher number than them, if the enemy hits higher you die""")
player_damage = random.randrange(7,15)
print("\nYou hit for: ", player_damage)
enemy_damage = random.randrange(1, 10)
print("The enemy hit for: ", enemy_damage)
if player_damage > enemy_damage:
print("\nYou successfully defeated the enemy")
elif enemy_damage > player_damage:
print("\nThe enemy strikes a fatal blow and you die almost instantly")
GameOver()
#Game Over function
def GameOver():
#Plays everytime the user dies
print("This is the end of your journey, thanks for playing.")
input("\nPress enter to exit")
#A simple fuction for when the user needs to choose yes or no
def YesNo():
while True:
answer = input("(y/n): ")
if answer in ('y', 'n'):
break
print("Invalid input.")
if answer == 'y':
return True
else:
return False
#Displays the logo
def welcome():
#Introduce the game to the player
print("Welcome to:")
print("""
_______ _ _____
|__ __| | / ____|
| | | |__ ___ | | __ ___ _____
| | | '_ \ / _ \ | | / _` \ \ / / _ \
| | | | | | __/ | |___| (_| |\ V / __/
|_| |_| |_|\___| \_____\__,_| \_/ \___|
/ _|
___ | |_
/ _ \| _|
| (_) | |
__ __ \___/|_| _
| \/ | | | (_)
| \ / |_ _ ___| |_ ___ _ __ _ __ _
| |\/| | | | / __| __/ _ \ '__| |/ _` |
| | | | |_| \__ \ || __/ | | | (_| |
|_| |_|\__, |___/\__\___|_| |_|\__,_|
__/ | (v1.0)
|___/ """)