Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The 21:00 showdown between Python:CPU and the man

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

# coding:utf-8# _ _ author__: immortal examination # _ author time__: 2017/8/8import randomprint "" requirements: 1, the initial CPU and the person each randomly own 2 cards; 2, each calculates the total number of the initial 2 cards; 3, when the number of cards is less than 16:00, automatically add a card; 4, the new cards into the total calculation 5. In human calculation: choose whether or not to add cards when it is greater than 16:00; when the added card is greater than or equal to 22:00, it will stop automatically; 6. In the calculation of CPU: when it is less than 16:00, it will automatically add new cards and calculate; when it is greater than 16:00, it will stop automatically; 7, the final values are compared. 8, comparison rules: [1] all greater than or equal to 22:00, then draw [2] when both are less than 22:00, if the two numbers are equal, the game is drawn; [3] those with large points win and those with small points lose. " Print "* start the game *" # initialize four random integers 1-10 number1 = random.randint (1,10) number2 = random.randint (1,10) number3 = random.randint (1,10) number4 = random.randint (1,10) # initialize CPU and random list of people man_numbers = [number1, number2] cpu_numbers = [number3 Number4] # Total number of initialization CPU and people man_count = 0cpu_count = "initialization cycle ID flag_cpu = 1flag_man = define human behavior # * * * # calculate the total number of cards for i in man_numbers: man_count = man_count + iprint "your cards are% s"% man_numbersprint "your total cards are% s"% man_countif man_count

< 16: # 小于16则继续自动添加牌到16点以上 print "拥有的牌数小于16点,自动添加新牌到16点以上..." while flag_man == 1: man_random1 = random.randint(1, 10) # 获得随机值,保证每次取到的都是一个新的随机值 man_numbers.append(man_random1) # 把获得的随机值添加到列表中 man_count = man_count + man_numbers[-1] # 始终加上列表的最后一个值 if man_count >

= 16 and man_count = 22: flag_man = 0 if man_count > = 22: flag_man = 0 print "" * * "print" your current total number of cards is% s "% man_count print" the current number of cards you have is% S "% man_numberselse: while flag_man = = 1: print" the number of cards you have is greater than 16:00 The total is s, do you want to continue to add new cards? " % man_count print "any key means to continue to ask for cards, enter uppercase Q or lowercase Q to indicate exit!" Get_key = raw_input ("Please enter:\ n") if get_key = ='Q' or get_key = = 'qvalues: flag_man = 0 else: man_random3 = random.randint (1,10) # get random values Make sure that each time you get a new random value man_numbers.append (man_random3) # add the obtained random value to the list man_count = man_count + man_numbers [- 1] # always add the last value of the list if man_count > = 22: flag_man = 0 print " * * "print" the total number of cards you currently have is% s "% man_count print". The card you currently have is% s "% man_numbers# * * # # define CPU behavior # * # calculate the total number of initial CPU for j in cpu_numbers: cpu_count = cpu_count + jif cpu_count

< 16: while flag_cpu == 1: # CPU获取随机值 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 is the sum of% s"% cpu_count print "the card currently owned by CPU is% s"% cpu_numberselse: print "* * the sum of the "" print "CPU is% s"% cpu_count print "the current CPU has the card% s"% cpu_numbers# * # # compare CPU and human values # * * print "" * "if man_count > = 22 and cpu_count"

< 22: print "CPU赢了..." print "你的总数大于22,你输了..."elif man_count < 22 and cpu_count >

= 22: print "you win." The total number of print "CPU is greater than 22 CPU lost..." elif man_count

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report