This is a game from a tutorial I'm following.
There is a menu where you can choose from 1 to 4 to perform an action. If you press 1 you can write your name and the stores it in the "load.text" file.
When I run the code from the folder "Open With" >> "Python" it crushes when I write my name.
I'm very new to Python, I would appreciate any help.
The code:
import os
run = True
menu = True
play = False
rules = False
HP = 50
ATK = 3
def clear():
os.system("cls")
def save():
list = [
name,
str(HP),
str(ATK)
]
f = open("load.txt", "w")
for item in list:
f.write(item + "\n")
f.close()
while run:
while menu:
clear()
print("1, NEW GAME")
print("2, LOAD GAME")
print("3, RULES")
print("4, QUIT GAME")
if rules:
print("I'm the creator of this game and these are the rules")
rules = False
choice = ""
input("> ")
else:
choice = input("# ")
if choice == "1":
clear()
name = input("# What's your name, hero? ")
play = True
menu = False
elif choice == "2":
f = open("load.txt", "r")
load_list = f.readlines()
name = load_list[0][:-1]
HP = load_list[1][:-1]
ATK = load_list[2][:-1]
clear()
print("Welcome back, " + name + "!")
input("> ")
menu = False
play = True
elif choice == "3":
rules = True
elif choice == "4":
quit()
while play:
save() # autosave
dest = input("# ")
if dest == "0":
play = False
menu = True
save()
Edit: through debugging I noticed that the problem is with the line
f = open("load.txt", "w")
It's like when I run it with Python, the code can't create another files?
Why is this happening?
Edit2: Using
from pathlib import Path
and putting " print(Path.cwd())" in the menu like this:
while menu:
clear()
print("1, NEW GAME")
print("2, LOAD GAME")
print("3, RULES")
print("4, QUIT GAME")
print(Path.cwd())
The result printed is:
1, NEW GAME
2, LOAD GAME
3, RULES
4, QUIT GAME
C:\WINDOWS\system32
It doesnt' appear the real path of the .py file. So it crushes because when creating a file it confuses the paths (?
How can I fix this? I want to also know why the path directs to system32