-1

Im trying to make a codebreaker game but for some reason it doesnt work please help

done = 0
guess = 0
import random
a=random.randrange(0,9)
str(a)
b=random.randrange(0,9)
str(b)
c=random.randrange(0,9)
str(c)
d=random.randrange(0,9)
str(d)

print a,b,c,d  #Its on so I can guess the correct number

while done == 0:

   ag=raw_input("Input a single digit between 9 and 0  :  ")
   bg=raw_input("Input a single digit between 9 and 0  :  ")
   cg=raw_input("Input a single digit between 9 and 0  :  ")
   dg=raw_input("Input a single digit between 9 and 0  :  ")
   print ag,bg,cg,dg
   if ag == a and bg == b and cg == c and dg == d:
      print "Well Done! You Got it correct in " ,guess, " times!" 
      done = 1
   else:
      print "Incorrect"
      if ag == a :
         print "You succsessfully guessed the first number!"
      if bg == b :
         print "You succsessfully guessed the second number!"
      if cg == c :
         print "You succsessfully guessed the third number!"
      if dg == d :
         print "You succsessfully guessed the forth number!"

Im thinking this is supposed to work as when I get some right numbers it will print that you got "n" number correct but it always comes out with this :

 >> 5 8 3 3
 >> Input a single digit between 9 and 0  :  5
 >> Input a single digit between 9 and 0  :  8
 >> Input a single digit between 9 and 0  :  3
 >> Input a single digit between 9 and 0  :  3
 >> 5 8 3 3
 >> Incorrect
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • You most likely need to covert your `String` values from from STDIN to `Integer` values - [this](http://stackoverflow.com/a/642169/2071828). Further, consider making `done` a `Boolean`. – Boris the Spider Apr 20 '15 at 07:40
  • @TrialandMostlyError: If your query was resolved, don't forget to [mark the answer below as accepted](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) by ticking it :) – Anshul Goyal Apr 20 '15 at 09:26

2 Answers2

2

When you do str(a), it doesn't convert a to a string. To do that, do a = str(a).

Since a is currently an integer, when you later compare a with ag and so on, the checks fail.

You will need to do the same for b, c, d in your current code.

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • 2
    @BoristheSpider Since OP uses str(a), so I thought this was the better way to point out.. of course the other way works as well. – Anshul Goyal Apr 20 '15 at 07:43
-1
done = 0
guess = 0
import random
a=random.randrange(0,9)
b=random.randrange(0,9)
c=random.randrange(0,9)
d=random.randrange(0,9)
print a,b,c,d  #Its on so I can guess the correct number

while done == 0:

   ag=int(raw_input("Input a single digit between 9 and 0  :  "))
   bg=int(raw_input("Input a single digit between 9 and 0  :  "))
   cg=int(raw_input("Input a single digit between 9 and 0  :  "))
   dg=int(raw_input("Input a single digit between 9 and 0  :  "))
   print ag,bg,cg,dg
   if ag == a and bg == b and cg == c and dg == d:
      print "Well Done! You Got it correct in " ,guess, " times!" 
      done = 1
   else:
      print "Incorrect"
      if ag == a :
         print "You succsessfully guessed the first number!"
      if bg == b :
         print "You succsessfully guessed the second number!"
      if cg == c :
         print "You succsessfully guessed the third number!"
      if dg == d :
         print "You succsessfully guessed the forth number!"

Now i think you got the correct idea how to solve this problem.

bhansa
  • 7,282
  • 3
  • 30
  • 55