# coding:utf-8# __author__: Fairy also exam # __author time__: 2017/8/8import randomprint "" "Demand: 1, the initial CPU and people randomly own 2 cards, 2, each calculated the total number of initial 2 cards; 3, when possession of the number of cards is less than 16 automatically added a card, 4, new cards entered the calculation sum; 5, Man's calculation: when the greater than the 16-point time to choose whether to add cards, when the addition of the card is greater than or equal to 22 points, the automatic stop; 6, CPU calculation: When less than 16 points are automatically added to the card and calculate, when more than 16 points automatically stop; 7, the final value is compared 8, the comparison rule: "1" is greater than or equal to 22 points, then the draw; "2" are less than 22 points, if the two number is equal, then the draw; "3" points large to win, the number of small people for the loss. "" "print " *************** start Game *************** "# initialize 4 random 1-10 integers number1 = Random.randint (1, 10) number2 = random.randint (1, 10) number3 = random.randint (1, &NBSP;10) Number4 = random.randint (1, 10) # initializes the random list of CPUs and people man_numbers = [number1, number2]cpu_numbers = [number3, number4]# Initialize the total number of CPUs and people man_count = 0cpu_count = 0# initialize the cyclic identity flag_cpu = 1flag_man = 1# **************************************# ############# Defining Human Behavior ############## * # calculates the total number of initial people obtained for i in man_numbers: man_count = man_count + iprint "The card you have is %s " % man_ numbersprint "Your total number of cards is %s" % man_countif man_count < 16: # Less than 16 continue to automatically add cards to more than 16 points print "has a number of cards less than 16 points, automatically add new cards to more than 16 points ..." while flag_man == 1: man_random1 = random.randint (1, 10) # get random values to ensure that each fetch is a new random value man_numbers.append (MAN_RANDOM1) # Add the obtained random values to the list man_count = man_count + man_numbers[-1] # always adds the last value of the list if man_count >= 16 and man_count <22: while flag_man == 1: print "You have a number of cards more than 16 points, the total is %s, do you need to continue to add new cards?" " % man_count print "Any key to continue to the card, enter the uppercase Q or lowercase q, indicating exit!" " get_key = raw_input ("Please enter: \ n") if get_key == ' q ' or get_key == ' Q ': flag_man = 0 else: man_random2 = random.randint (1, 10) # get random values to ensure that each fetch is a new random value man_numbers.append ( MAN_RANDOM2) # Add the obtained random values to the list man_count = man_count + man_ numbers[-1] # always add the last value of the list if man_count >= 22: &nbSp; flag_man = 0 if man_count >= 22: flag_man = 0 print "" "******************************" "" print "your current total number of cards is %s" % man_count print "The card you currently have is %s" % man_numberselse: while flag_man == 1: print "You have more than 16 points, the total is %s, do you need to continue to add new cards?" " % man_count print " any key means to continue to the card, enter the uppercase Q or lowercase q, indicating exit! " get_key = raw_input (" Please enter: \ n ") if get_key == ' q ' or get_key == ' Q ': flag_man = 0 else: man_random3 = random.randint (1, 10) # get random values to ensure that each fetch is a new random value man_numbers.append (MAN_RANDOM3) # Add the obtained random values to the list man_count = man_count + man_numbers[-1] # always adds the last value of the list if man_count >= 22: flag_man = 0 print "" "******************************" "" print "the current total number of your cards is %s" % man_count print "The card you have now is %s" % man_numbers# **************************************# ############# Define CPU Behavior ############# **************************************# Calculates the total number of initial CPUs obtained for j in cpu_numbers: cpu_count = cpu_count + jif cpu_count < 16: while flag_cpu == 1: &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;#&NBSP;CPU get random values cpu_ Random1 = random.randint (1, 10) cpu_ Numbers.append (CPU_RANDOM1) cpu_count = cpu_count + cpu_numbers[-1] if cpu_count >= 16: flag_cpu = 0 print "" "************"" " print " CPU sum is %s " % cpu_count print "Current CPU-owned cards are %s" % cpu_numberselse: print "" " "" " print " CPU sum is %s " % cpu_count print "Current CPU-owned card is %s" % cpu_numbers# ************************ # ######### comparison of CPU and human values ############ ************************************** print "" "******************************" "" If man_count >= 22 and cpu_count < 22: print "CPU wins ..." print "your total is greater than 22, You lost ... "elif man_count < 22 and cpu_count >= 22: print "You win ..." print "total number of CPUs is greater than 22,cpu lost ..." elif man_count < = 21 and cpu_count <= 21: if man_count > cpu_count: print "You won ..." print "Your total is:%s" % man_count print "CPU lost ..." print "Total number of CPUs:%s" % cpu_count elif man_count < cpu_count: print "CPU wins ..." print "total number of CPUs:%s" % cpu_count print "You lost ..." print "Your total is:%s" % man_count else: print "Draw ..." print "you and the total number of CPUs:%s" % man_countelse: print "Draw ..." print "you and the total number of CPUs are greater than ..." print "* * * * * * * * * Game Over *************** "
This article is from the "as1024" blog, make sure to keep this source http://as1024.blog.51cto.com/12241298/1954497
Python:cpu and man's 21-point duel