BushellsMaster - 1 year ago 160
Python Question

# Storing a score in python

``````User_Points = '0'

print("Welcome to Area Trainer")
global user_name
user_name = str(input())
save = open(user_name + '.txt', 'w')
save.write(str(User_Points))

elif not any(i.isdigit() for i in user_password):
elif not any(i.isupper() for i in user_password):
elif not any(i.islower() for i in user_password):
else:

global User_Points
User_Points = ''
print('''Here is a triangle with a height of 12cm and a width of 29cm
/\     |                *Not to scale.
/  \    |
/    \   | 12cm
/      \  |
<------->
29cm
You must find out the area and select the correct answer from these options''')
print('''A) 175
B) 174
C) 2000
D) 199

''')
user_input = input().upper()

if user_input == "A":
print("I'm sorry this is incorrect but you still have a chance to get 1 point!")

elif user_input == "C":
print("I'm sorry this is incorrect but you still have a chance to get 1 point!")

elif user_input == "D":
print("I'm sorry this is incorrect but you still have a chance to get 1 point!")

elif user_input == "B":
print("Congratulations! You got it right, someone's a smart cookie. Here have two points!")
score = score + '2'
save = open(user_name + '.txt', 'a')
save.write(str(score))

print("Welcome to the mathematical area game!")
print("In this game you will be required to calculate the area of multiple shapes.")
print("To do this you must know how to calculate the area of different shapes with increasing difficulty")
print('''Please select a shape you want to play,
A) Triangle
B) Square
C) Circle''')
user_input = input().upper()

if user_input == "A":
print("You have chosen to calculate the area of a triangle!")

elif user_input == "B":
print("You have chosen to calculate the area of a square!")

elif user_input == "C":
print("You have chosen the calculate the area of a circle!")

else:
print("Oops! I didn't understand that >:")

``````

Hello, so far I have made a short game that you have to guess the area of the shape in question (Only the triangle works so far and the correct answer is B for testing) I am trying to score the users progress by putting it into a text file but because I have to keep calling the function it overwrites the file so the score is removed! How can I get around it and if not is there another way of doing this because I want the user to be able to go from level to level! Thanks in advance!

It's not saving because you never close the file. This is why most people agree `with open(filename, 'w+')` is best practice.

Try using the below format for `LoginScreen()`

``````def LoginScreen():
print("Welcome to Area Trainer")
global user_name
user_name = str(input())
with open(user_name + '.txt', 'w') as f:
f.write(str(User_Points))

# Remaining code below here...
``````

I also noticed at the end of `MenuTriangle()` you try to add strings together rather than adding integers. You'll want to convert the string you read from file to an integer before increasing the score. You also don't provide which mode of opening the file you need. It does default to `'r'` but it's better to be explicit.

``````def MenuTriangle():

# if: ...
# elif: ...
# elif: ...

elif user_input == "B":
print("Congratulations! You got it right, someone's a smart cookie. Here have two points!")

with open(user_name + '.txt', 'r') as f: