質問

I know you can use

f = open("data1.dat", "w")
f.write()
f.close()

But i need to know how to do that for both text and numbers

print ("Create a character! you have points to assign to strength and skills (attack,defence,health")
name = input("What's your character's name? \n")
print ("Two dices are thrown (A 12 and 4 sided dice) and the 12 sided dice is divided by the score on the 4 sided dice \n")

attributes=("health", "strength", "attack", "defence")
health=10
strength=10
attack=10
defence=10

import random

dicenumber=random.randint(1,12)
dicenumber2=random.randint(1,4)
health = (dicenumber/dicenumber2)+ 10

dicenumber=random.randint(1,12)
dicenumber2=random.randint(1,4)
strength = (dicenumber/dicenumber2)+ 10

dicenumber=random.randint(1,12)
dicenumber2=random.randint(1,4)
attack = (dicenumber/dicenumber2)+ 10

dicenumber=random.randint(1,12)
dicenumber2=random.randint(1,4)
defence = (dicenumber/dicenumber2)+ 10

print ("PLAYER: ",name)
print ("Health Points :", round(health, 2))
print ("Strength Points :", round(strength, 2))
print ("Attack Points :", round(attack, 2))
print ("Defence Points :", round(defence, 2))

name2 = input("What's your second character's name? \n")

dicenumber=random.randint(1,12)
dicenumber2=random.randint(1,4)
health = (dicenumber/dicenumber2)+ 10

dicenumber=random.randint(1,12)
dicenumber2=random.randint(1,4)
strength = (dicenumber/dicenumber2)+ 10

dicenumber=random.randint(1,12)
dicenumber2=random.randint(1,4)
attack = (dicenumber/dicenumber2)+ 10

dicenumber=random.randint(1,12)
dicenumber2=random.randint(1,4)
defence = (dicenumber/dicenumber2)+ 10

print ("PLAYER: ",name2)
print ("Health Points :", round(health, 2))
print ("Strength Points :", round(strength, 2))
print ("Attack Points :", round(attack, 2))
print ("Defence Points :", round(defence, 2))

For example so the data file will be created...and it will be:

PLAYER: Name Health Points : 18.0 Strength Points : 11.0 Attack Points : 16.0 Defence Points : 12.0

^something like that for both players

Or is there a function that can hightlight/group/label my character coding:

(STATS CODING)

then f = open("data1.dat, "w")
f.write(STATS CODING)
f.close()

Many Thanks, If you need more information feel free to inform me!^^

SOLUTIONS:

with open("data1.txt", "w") as file:

    file.write("PLAYER: " + name)
    file.write("Health Points : " + str(round(health, 2)))
    file.write("Strength Points : " + str(round(strength, 2)))
    file.write("Attack Points : " + str(round(attack, 2)))
    file.write("Defence Points : " + str(round(defence, 2)))

file.close()

EASY but how do i create a new line each time??

役に立ちましたか?

解決

An easy way is to create a dict, and use repr()/eval() to convert to string and from string:

data = {
    "PLAYER":name,
    "Health points": health
    ...
}

with open("data1.txt", "w", encoding="utf-8") as file:
   file.write(repr(data))
file.close()

Then to load:

with open("data1.txt", "r", encoding="utf-8") as file:
   data_string = file.readline()
   data = eval(data_string)
   print data["PLAYER"]
file.close()
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top