I'm having a problem trying to create a game.
the point of the game is to guess a random number.
i'm learning python and i tought it would be a good exercise.
everything went fine (if you dont count syntax errors :))
until i tried to save the program.
i can't get it to create a list without putting another pair of [] around the scores.
and the part where i use c ==x doesn't work wel.
can somebody help me with this?
the program is in dutch
def readScore(lol):
import os
if os.path.exists(file):
store = open(file,"r")
for line in store:
naam = line.rstrip()
entry = store.next().rstrip()
score = [entry]#after 2 times of opening the score is surrounded by a lot of []
topscore[naam]=score
store.close()
import os
import sys
topscore ={}
name = raw_input("wat is je naam?""\n")
score =[]
def addScore(lol):
if topscore.has_key(name):
score = topscore[name]
score.append(n)
topscore[name]=score
else:
score = []
score.append(n)
topscore[name]=score
file = "score.dat"
def saveScore(lol):
store = open(file,"w")
for naam,lijst in topscore.items():
store.write(naam+"\n")
store.write(str(lijst)+"\n")
store.close()
c = 1
import random
menu = """
1)nog eens spelen
2) stoppen
3) print scores
"""
a = random.randint(0,1000)
while a !=0 :
print " nieuw spel begint"
readScore(topscore)
n = 0
b=int(raw_input("kies een nummer tussen 0 en 1000" '\n' ))
while b !=0 :
n +=1
if b>a :
print " te veel"
elif b<a :
print " te weinig"
elif b==a:
addScore(topscore)
saveScore(topscore)
print " gelukt"
print " je deed het in %d beeurten" %n
print"-------------------------------"
c = int(raw_input(menu))
if c ==2 :
exit()
else :
if c ==3 :
print topscore
c = int(raw_input(menu))
else :
if c == 1 :#if you print the scores before you use this you stil get the same number
n=0
print " nieuwe spel begint nu"
a = random.randint(0,1000)
else :
print " verkeerde input"
c = int(raw_input(menu))
else :
print " verkeerd cijfer"
b= int(raw_input("kies een nummer tussen 0 en 1000" '\n' ))
thanks