简体   繁体   English

我如何将分数 1 和 2 写入 python 中的文本文件?

[英]how would i write scores 1&2 to a text file in python?

I'm making a game where two players roll 2 dice each and receive points depending on the number they roll.我正在制作一个游戏,其中两名玩家每人掷 2 个骰子,并根据他们掷出的数字获得分数。 There will be 5 rounds in a game.一场比赛将进行 5 轮比赛。 Whoever has the highest total of points by the end wins.最后总分最高者获胜。

At the end I would like it to write the winner's name and score to a text file.最后,我希望将获胜者的姓名和分数写入文本文件。

How would I go about doing that?我该怎么做?

Many thanks, Roshan非常感谢, Roshan

#Import required functions
import time
import random

#Created required variables
goes=0
score1=0
score2=0
password1="2w$&Kk"
password2="Zc7g/m"

#Authorisation
passcheck1=input("PLayer 1, please enter the set password for your player: ")
passcheck2=input("PLayer 2, please enter the set password for your player: ")
if password1 == passcheck1:
    player1=input("Player 1, please enter your name: ")
else:
    exit()
if password2 == passcheck2:
    player2=input("Player 2, please enter your name: ")
else:
    exit()

#Game
while goes <10:
    print(player1, "rolls 2 dice")
    time.sleep(2)
    dice1=random.randint(0,6)
    dice2=random.randint(0,6)
    dice3=random.randint(0,6)
    total=dice1+dice2

    if dice1 == dice2:
        print(player1, "rolled a double! They are now allowed to roll a third die. The number on the third die is the amount of points they earn")
        score1=score1+dice3

    elif (total % 2) == 0:
        score1=score1+10
        goes=goes+1

    else:
        score1=score1-5
        goes=goes+1

    print (score1)
    if score1 < 0:
        print(player1,"'s score has reached below 0, therefore has lost.")
        goes=10


    time.sleep(2)


    print(player2, "rolls 2 dice")
    time.sleep(2)
    dice1=random.randint(0,6)
    dice2=random.randint(0,6)
    dice3=random.randint(0,6)
    total=dice1+dice2

    if dice1 == dice2:
        print(player2, "rolled a double! They are now allowed to roll a third die. The number on the third die is the amount of points they earn")
        score2=score2+dice3

    elif (total % 2) == 0:
        score2=score2+10
        goes=goes+1

    else:
        score2=score2-5
        goes=goes+1

    print(score2)
    if score2 < 0:
        print(player2,"'s score has reached below 0, therefore has lost.")
        goes=10

#Deciding who wins
if goes==10:
    if score1 == score2:
        time.sleep(2)
        print("Both players scores are the same and must roll another five times to see who wins")
        goes=0
    elif score1 < score2:
        print("Player 2 wins")
        f = open("scores.txt", "a")
        f.write(player2)
        f.write(score2)
        f.close
    else:
        print("Player 1 wins")
        f = open("scores.txt", "a")
        f.write(player1)
        f.write(score1)
        f.close

You could do it like this:你可以这样做:

with open('my_file.txt', 'a') as f:
    f.write(f"{player2},{score2}\n")

or if you're not using python 3.6 or later或者如果您没有使用 python 3.6 或更高版本

    f.write( "{},{}\n".format(player2,score2) )

The way it works is that you open a file f , with argument a (append) to indicate you wish to add it to the existing lines.它的工作方式是打开一个文件f ,并使用参数a (追加)表示您希望将其添加到现有行中。 Because you use a with statement to open the file, it automatically closes the file and you won't have to worry about the file not being closed and eating up memory.因为您使用with语句打开文件,它会自动关闭文件,您不必担心文件未关闭而占用内存。

If you want to overwrite the file, you should use w instead of a .如果你想覆盖文件,你应该使用w而不是a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM