error in guessing game

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2007
Posts: 39
Reputation: mathijs is an unknown quantity at this point 
Solved Threads: 2
mathijs's Avatar
mathijs mathijs is offline Offline
Light Poster

error in guessing game

 
0
  #1
Oct 20th, 2007
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

  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,028
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 289
woooee woooee is offline Offline
Veteran Poster

Re: error in guessing game

 
0
  #2
Oct 21st, 2007
Look through the projects for beginner thread. This or something similiar is sure to be there.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: error in guessing game

 
0
  #3
Oct 21st, 2007
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

  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. >>>
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 39
Reputation: mathijs is an unknown quantity at this point 
Solved Threads: 2
mathijs's Avatar
mathijs mathijs is offline Offline
Light Poster

Re: error in guessing game

 
0
  #4
Oct 22nd, 2007
thanks for the advice
it's very helpful
i think i should be able to finish it now
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 39
Reputation: mathijs is an unknown quantity at this point 
Solved Threads: 2
mathijs's Avatar
mathijs mathijs is offline Offline
Light Poster

Re: error in guessing game

 
0
  #5
Oct 22nd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: error in guessing game

 
0
  #6
Oct 22nd, 2007
Start here:
  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 39
Reputation: mathijs is an unknown quantity at this point 
Solved Threads: 2
mathijs's Avatar
mathijs mathijs is offline Offline
Light Poster

Re: error in guessing game

 
0
  #7
Oct 22nd, 2007
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
  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
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,028
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 289
woooee woooee is offline Offline
Veteran Poster

Re: error in guessing game

 
0
  #8
Oct 22nd, 2007
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?
  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"
Originally Posted by mathijs View Post
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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 39
Reputation: mathijs is an unknown quantity at this point 
Solved Threads: 2
mathijs's Avatar
mathijs mathijs is offline Offline
Light Poster

Re: error in guessing game

 
0
  #9
Oct 23rd, 2007
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, 9 views)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC