Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import time
- import sys
- yorn = True #Boolean for restarting game, loops until it reaches a 'False' and 'Break'
- while yorn == True:
- fourno = [0,1, 2, 3, 4, 5, 6, 7, 8, 9 ] #List used for number combination generation (stores as integers)
- random.shuffle(fourno) #Shuffle list
- fourno2 = fourno[::3] #Picks out the first 4 numbers in the list
- print (fourno2) #Outputs number for test to show it works
- name = input("\nPlease enter your name: ")
- print ("Hello {0}!".format(name))
- #Bull + Cow counter
- bull = 0
- cow = 0
- while bull < 4: #Continue loop if the Bull counter is lower than 4
- bull = 0
- cow = 0
- #User input
- ui = input("\nDo not enter duplicate numbers and only enter numbers in between 0-9.\n\nPlease type 'exit' if you would like to exit the game and reveal the numbers.\n\nPlease enter 4 digits: ").upper()
- if ui == "EXIT":
- print (fourno2)
- print ('\nExiting the script...\n')
- yorn == False
- break
- #Divides the numbers into individual lists for comparison & Validation "Try & Except"
- try:
- a = int(ui[0]) #Converted and then stored into integers
- b = int(ui[1])
- c =int(ui[2])
- d = int(ui[3])
- except ValueError:
- print ("{0}\n Invalid input... ".format(ui))
- continue
- print ("{0} {1} {2} {3}".format(a, b, c, d)) #Displays the numbers the user entered
- #Comparing the user input and the computer
- #Comparing Bulls with Bulls
- #Adds value to Bull if a number is placed in the same position as the generated list
- if a == fourno2[0]:
- bull = bull + 1
- if b == fourno2[1]:
- bull = bull + 1
- if c == fourno2[2]:
- bull = bull + 1
- if d == fourno2[3]:
- bull = bull + 1
- #Comparing Cows with Cows, 1st set
- #Adds value to Cow if there is a correct number but is not placed in the same position/is placed in the same position
- if a == fourno2[0]:
- cow = cow + 1
- if a == fourno2[1]:
- cow = cow + 1
- if a == fourno2[2]:
- cow = cow + 1
- if a == fourno2[3]:
- cow = cow + 1
- #Comparing Cows with Cows, 2nd set
- if b == fourno2[0]:
- cow = cow + 1
- if b == fourno2[1]:
- cow = cow + 1
- if b == fourno2[2]:
- cow = cow + 1
- if b == fourno2[3]:
- cow = cow + 1
- #Comparing Cows with Cows, 3rd set
- if c == fourno2[0]:
- cow = cow + 1
- if c == fourno2[1]:
- cow = cow + 1
- if c == fourno2[2]:
- cow = cow + 1
- if c == fourno2[3]:
- cow = cow + 1
- #Comparing Cows with Cows, 4th set
- if d == fourno2[0]:
- cow = cow + 1
- if d == fourno2[1]:
- cow = cow + 1
- if d == fourno2[2]:
- cow = cow + 1
- if d == fourno2[3]:
- cow = cow + 1
- #Displays results
- print ("Bull = {0}".format (bull))
- print ("Cow = {0} ".format (cow))
- #Play again?
- restart = input("Would you like to play again?\nYes or No? (Can type 'Y' or 'N'): ").upper() #Avoiding long line of code with differently written answers
- while not (restart == "Y" or restart == "N" or restart == "YES" or restart == "NO"): #Validating answer for restarting game
- print ("Not a valid answer\n")
- restart = input("Would you like to play again? ").upper()
- if restart == "Y"or restart == "YES":
- print ("\nRestarting\n--------------------------------------------------------------------\n")
- yorn = True
- continue
- elif restart == "N" or restart == "NO" :
- msg = ("\nThank you for playing!\nEnding program...\n..........\n..........\n..........\n\n\n\n")
- for i in msg:
- sys.stdout.write(i)
- sys.stdout.flush()
- time.sleep (0.05)
- yorn = False
- break
- if ui == "exit":
- print (fourno2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement