943,722 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1996
  • Python RSS
Oct 20th, 2007
0

error in guessing game

Expand Post »
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

python Syntax (Toggle Plain Text)
  1. def readScore(lol):
  2. import os
  3. if os.path.exists(file):
  4. store = open(file,"r")
  5. for line in store:
  6. naam = line.rstrip()
  7. entry = store.next().rstrip()
  8. score = [entry]#after 2 times of opening the score is surrounded by a lot of []
  9. topscore[naam]=score
  10. store.close()
  11.  
  12.  
  13. import os
  14. import sys
  15. topscore ={}
  16. name = raw_input("wat is je naam?""\n")
  17. score =[]
  18.  
  19. def addScore(lol):
  20. if topscore.has_key(name):
  21. score = topscore[name]
  22. score.append(n)
  23. topscore[name]=score
  24. else:
  25. score = []
  26. score.append(n)
  27. topscore[name]=score
  28.  
  29.  
  30.  
  31. file = "score.dat"
  32.  
  33. def saveScore(lol):
  34. store = open(file,"w")
  35. for naam,lijst in topscore.items():
  36. store.write(naam+"\n")
  37. store.write(str(lijst)+"\n")
  38. store.close()
  39.  
  40.  
  41.  
  42. c = 1
  43. import random
  44. menu = """
  45. 1)nog eens spelen
  46. 2) stoppen
  47. 3) print scores
  48. """
  49. a = random.randint(0,1000)
  50. while a !=0 :
  51. print " nieuw spel begint"
  52. readScore(topscore)
  53. n = 0
  54. b=int(raw_input("kies een nummer tussen 0 en 1000" '\n' ))
  55. while b !=0 :
  56. n +=1
  57. if b>a :
  58. print " te veel"
  59. elif b<a :
  60. print " te weinig"
  61. elif b==a:
  62. addScore(topscore)
  63. saveScore(topscore)
  64. print " gelukt"
  65. print " je deed het in %d beeurten" %n
  66. print"-------------------------------"
  67. c = int(raw_input(menu))
  68. if c ==2 :
  69. exit()
  70. else :
  71. if c ==3 :
  72. print topscore
  73. c = int(raw_input(menu))
  74. else :
  75. if c == 1 :#if you print the scores before you use this you stil get the same number
  76. n=0
  77. print " nieuwe spel begint nu"
  78. a = random.randint(0,1000)
  79. else :
  80. print " verkeerde input"
  81. c = int(raw_input(menu))
  82. else :
  83. print " verkeerd cijfer"
  84. b= int(raw_input("kies een nummer tussen 0 en 1000" '\n' ))

thanks
Similar Threads
Reputation Points: 10
Solved Threads: 2
Light Poster
mathijs is offline Offline
41 posts
since Oct 2007
Oct 21st, 2007
0

Re: error in guessing game

Look through the projects for beginner thread. This or something similiar is sure to be there.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006
Oct 21st, 2007
0

Re: error in guessing game

It's funny to me that, armed with English and German, I can read printed Dutch pretty well...but spoken Dutch might as well be Arabic to me!

Several things strike me about the code.

(1) All import statements should occur at the top of the main code, before functions are defined (there are rare exceptions to this practice, but that's the rule). So: move lines 13, 14, and 43 to the top, and eliminate line 2.

(2) Generally, I try to get the skeleton framework for the code working before I get the whole thing working. In your case, you want to (a) have the user guess a number, (b) keep track of all of the user scores in a round, and (c) store the user scores in a file.

Create your program in that order. Get a basic guess-the-number program working. Then create a program that gets the user's name and keeps track of his scores within the round. Then finally, add the file functionality.

(3) Your topscore dictionary appears to be like this: {"name1": score1, "name2": score2, ...}. I would recommend reversing that: {score1: "name1", score2: "name2", score3: "name3", ...}.

By doing so, you can sort the keys and take, say, the top ten, eliminating the rest.

Hope it helps,
Jeff

Python Syntax (Toggle Plain Text)
  1. import random
  2.  
  3. secret = random.randint(1,100)
  4. guesses = 0
  5.  
  6. while True:
  7. guess = raw_input("I'm thinking of an integer between 1 and 100. What is it? ")
  8. if guess == "quit":
  9. break
  10. try:
  11. guess = int(guess)
  12. except:
  13. print "Please enter an integer!"
  14. continue
  15. guesses += 1
  16. if guess > secret:
  17. print "Too high!"
  18. elif guess < secret:
  19. print "Too low!"
  20. else:
  21. break
  22. if guess == secret:
  23. print "You got it! It took you %d tries." % guesses
  24. else:
  25. print "Sorry! Better luck next time!"
  26.  
  27. >>>
  28. I'm thinking of an integer between 1 and 100. What is it? two
  29. Please enter an integer!
  30. I'm thinking of an integer between 1 and 100. What is it? 50
  31. Too low!
  32. I'm thinking of an integer between 1 and 100. What is it? 75
  33. Too high!
  34. I'm thinking of an integer between 1 and 100. What is it? 65
  35. Too high!
  36. I'm thinking of an integer between 1 and 100. What is it? 60
  37. Too low!
  38. I'm thinking of an integer between 1 and 100. What is it? 63
  39. Too high!
  40. I'm thinking of an integer between 1 and 100. What is it? 62
  41. Too high!
  42. I'm thinking of an integer between 1 and 100. What is it? 61
  43. You got it! It took you 7 tries.
  44. >>>
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Oct 22nd, 2007
0

Re: error in guessing game

thanks for the advice
it's very helpful
i think i should be able to finish it now
Reputation Points: 10
Solved Threads: 2
Light Poster
mathijs is offline Offline
41 posts
since Oct 2007
Oct 22nd, 2007
0

Re: error in guessing game

It works great when you use the score as the key in the dictionary thanks
now all i need to do is try to sort the results
Reputation Points: 10
Solved Threads: 2
Light Poster
mathijs is offline Offline
41 posts
since Oct 2007
Oct 22nd, 2007
0

Re: error in guessing game

Start here:
Python Syntax (Toggle Plain Text)
  1. keys = mydict.keys() # List of keys
  2. keys.sort() # Sorts keys[] in-place
  3.  
  4. for k in keys:
  5. print k, mydict[k]
Last edited by BearofNH; Oct 22nd, 2007 at 4:52 pm. Reason: Fumble-fingered <TAB>P in example, suddenly posted
Reputation Points: 94
Solved Threads: 48
Posting Whiz
BearofNH is offline Offline
321 posts
since May 2007
Oct 22nd, 2007
0

Re: error in guessing game

found it myself!
and used it but the 10,11 appear before the 6,9
like this:
10 : mathijs
11 : mathijs
13 : mathijs
6 : mathijs
8 : mathijs
9 : mathijs
and i can't seem to get the c-loop working without errors
tought i did make some improvements
python Syntax (Toggle Plain Text)
  1. c = int(raw_input(menu))
  2. while c !="":
  3. if c ==2 :
  4. exit()
  5. elif c ==3 :
  6. printScore()
  7. c = int(raw_input(menu))
  8. elif c == 1 :
  9. n=0
  10. print " nieuwe spel begint nu"
  11. a = random.randint(0,1000)
  12. break
  13. else :
  14. print " verkeerde input"
  15. c = int(raw_input(menu))

and the sort+print code
python Syntax (Toggle Plain Text)
  1. def printScore():
  2. keylist = topscore.keys()
  3. keylist.sort()
  4. for key in keylist:
  5. print " %s : %s " %(key,topscore[key]

I want to thank everyone for the support and help!!
Last edited by mathijs; Oct 22nd, 2007 at 4:59 pm.
Reputation Points: 10
Solved Threads: 2
Light Poster
mathijs is offline Offline
41 posts
since Oct 2007
Oct 22nd, 2007
0

Re: error in guessing game

You will not exit the while() loop because c is never "", you want to use
while c > 2: as 1 is a break (exits the while loop), and 2 is what executes exit()-and do you want sys.exit(0) instead to exit the program?
python Syntax (Toggle Plain Text)
  1. c=3
  2. while c > 2:
  3. c = int(raw_input(menu))
  4. if c ==2 :
  5. exit()
  6. elif c ==3 :
  7. printScore()
  8. elif c == 1 :
  9. n=0
  10. print " nieuwe spel begint nu"
  11. a = random.randint(0,1000)
  12. break
  13. else :
  14. print " verkeerde input"
Click to Expand / Collapse  Quote originally posted by mathijs ...
but the 10,11 appear before the 6,9
like this:
10 : mathijs
11 : mathijs
13 : mathijs
6 : mathijs
8 : mathijs
9 : mathijs
and the sort+print code
A string like you are using in key list sorts left to right so the "1" in "10" is less than the 9. Convert to an int. This would not be necessary if you can use an int for the dictionary key to begin with.
python Syntax (Toggle Plain Text)
  1. def printScore():
  2. keylist = [int(key) for key in topscore.keys()]
  3. keylist.sort()
  4. for key in keylist:
  5. print " %d : %s " %(key,topscore[str(key)]
Last edited by woooee; Oct 22nd, 2007 at 7:54 pm.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006
Oct 23rd, 2007
0

Re: error in guessing game

Now it work's !!!!
thanks everyone!!
added the finished version translated to english
and marked the post as solved
many thanks

mathijs
Attached Files
File Type: txt game.txt (1.9 KB, 25 views)
Reputation Points: 10
Solved Threads: 2
Light Poster
mathijs is offline Offline
41 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Adding 1 with 1 in binary
Next Thread in Python Forum Timeline: a class containg a set of data members





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC