2016-08-23 87 views
-1

到目前为止,我已经做了一个短小的游戏,你必须猜测有问题的形状区域。只有三角形到目前为止工作正确答案是B进行测试。在Python中存储分数

我试图通过将其放入文本文件来存储用户的进度,但是当我调用更新分数的函数时,它会覆盖文件以便删除分数!我怎么能解决这个问题,如果我不能有另一种方式来做到这一点,因为我希望用户能够从一个级别到另一个级别?

我的代码:

User_Points = '0' 

def LoginScreen(): 
    print("Welcome to Area Trainer") 
    print("Please enter the username for your account") 
    global user_name 
    user_name = str(input()) 
    save = open(user_name + '.txt', 'w') 
    save.write(str(User_Points)) 


    PasswordCheck= True 
    while PasswordCheck: 
     user_password = input("Type in your password: ") 
     if len(user_password) < 8: 
      print("Your password must be 8 characters long") 
     elif not any(i.isdigit() for i in user_password): 
      print("You need a number in your password") 
     elif not any(i.isupper() for i in user_password): 
      print("You need a capital letter in your password") 
     elif not any(i.islower() for i in user_password): 
      print("You need a lowercase letter in your password") 
     else: 
      PasswordCheck = False 


def MenuTriangle(): 
    global User_Points 
    User_Points = '' 
    print('''Here is a triangle with a height of 12 cm and a width of 29 cm 
    /\  |    *Not to scale. 
/\ | 
/ \ | 12 cm 
/ \ | 
<-------> 
    29 cm 
    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!") 
      MenuTriangle2() 

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

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

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

def MenuStart(): 

    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!") 
     MenuTriangle() 

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

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

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


LoginScreen() 
MenuStart() 
+3

为什么你发布你的整个游戏?我们只需要看看将分数写入文件的部分。请发布**最小**但完整的示例:https://stackoverflow.com/help/mcve –

+0

我不清楚你遇到的问题是什么?您似乎正在打开,阅读并正确追加到文件。顺便说一句,您可能会发现'with'语句对打开文件很有帮助 - http://effbot.org/zone/python-with-statement.htm。 –

+0

为什么你不检查答案是否正确('B')否则你的打印和函数调用,这会缩短你的代码 – depperm

回答

1

它不节能,因为你永远不会关闭该文件。这就是为什么大多数人同意with open(filename, 'w+')是最佳做法。

尝试使用以下格式LoginScreen()

def LoginScreen(): 
    print("Welcome to Area Trainer") 
    print("Please enter the username for your account") 
    global user_name 
    user_name = str(input()) 
    with open(user_name + '.txt', 'w') as f: 
     f.write(str(User_Points)) 

    # Remaining code below here... 

我也是在MenuTriangle()您尝试添加字符串连接在一起,而不是添加整数结束注意。在增加分数之前,您需要将您从文件中读取的字符串转换为整数。您也不提供您需要打开文件的模式。它默认为'r',但最好是明确的。

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: 
      score = f.read() 
      new_score = int(score) + 2 
      print("Your score is {}".format(new_score)) 

     with open(user_name + '.txt', 'w+') as w: # Wouldn't you want to overwrite this rather than continue appending numbers? 
      w.write(str(new_score)) 

     MenuStart() # Call this outside of your with statements so that the file closes 
+0

非常感谢!这工作完美!我明白你现在做了什么! :d – BushellsMaster

2

加入,你在这里做

score = reading.read() 
score = score + '2' 

type str所以你不断收到值一样024,该文件中的值更改为int第一。

score = int(score) + '2' 

w+模式

save = open(user_name + '.txt', 'w+') 

而且打开文件,或者使用with,因为它是被推荐他人。