exercise10.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Copyright (C) 2020, 2019 Girish M
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 3 of the License, or
  5. # (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program; if not, write to the Free Software
  14. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  15. # MA 02110-1301, USA.
  16. #
  17. '''
  18. Create a program that will play the “cows and bulls” game with the user. The game works like this:
  19. Randomly generate a 4-digit number. Ask the user to guess a 4-digit number.
  20. For every digit that the user guessed correctly in the correct place, they have a “cow”.
  21. For every digit the user guessed correctly in the wrong place is a “bull.”
  22. Every time the user makes a guess, tell them how many “cows” and “bulls” they have.
  23. Once the user guesses the correct number, the game is over.
  24. Keep track of the number of guesses the user makes throughout teh game and tell the user at the end.
  25. Say the number generated by the computer is 1038. An example interaction could look like this:
  26. Welcome to the Cows and Bulls Game!
  27. Enter a number:
  28. >>> 1234
  29. 2 cows, 0 bulls
  30. >>> 1256
  31. 1 cow, 1 bull
  32. ...
  33. '''
  34. # @author prince
  35. import random
  36. rn = random.choice(range(1000, 10000))
  37. rnlist = list(str(rn))
  38. choice = 'no'
  39. cows = 0
  40. bulls = 0
  41. score = 0
  42. while choice == 'no':
  43. n = input('[write 0000 to give up.] enter a 4-digit number: ')
  44. score += 1
  45. a = n[0::5]
  46. b = n[1::4]
  47. c = n[2::4]
  48. d = n[3::4]
  49. ra = int(rnlist[0])
  50. rb = int(rnlist[1])
  51. rc = int(rnlist[2])
  52. rd = int(rnlist[3])
  53. if int(n) == rn:
  54. print('You Won! You guessed the number in', score, 'guesses!')
  55. choice = 'yes'
  56. else:
  57. if int(a) == ra:
  58. cows += 1
  59. else:
  60. if a in rnlist:
  61. bulls += 1
  62. if int(b) == rb:
  63. cows += 1
  64. else:
  65. if b in rnlist:
  66. bulls += 1
  67. if int(c) == rc:
  68. cows += 1
  69. else:
  70. if c in rnlist:
  71. bulls += 1
  72. if int(d) == rd:
  73. cows += 1
  74. else:
  75. if d in rnlist:
  76. bulls += 1
  77. print('cows: ', cows)
  78. cows = 0
  79. if cows != 4:
  80. print('bulls: ', bulls)
  81. bulls = 0
  82. if n == '0000':
  83. print(rn, 'was the number!')
  84. break