Case: Python play 21 points

Source: Internet
Author: User
Tags abs

  • Two players, start typing the first name of the game
  • Save each player's information in a dictionary: name, number of wins
  • The computer randomly produces 2 numbers, each player takes turns guessing 1 numbers, and the computer randomly two number sums, the closest to 21 wins
  • Show player info at end of each round
  • Press Q to exit the game
  • Import randomuser1 = input (' user1name: ') user2 = input (' user2name: ') User_info = {user1: {' win ': 0}, U Ser2: {' win ': 0}}while true:computer1_num = Random.randint (1,10) print (f ' computer randomly generates a number: {computer 1_num} ') Computer2_num = Random.randint (1,10) print (f ' computer randomly generates a number: {computer2_num} ') User1_num = input (f ' {user1} please guess a        Number (at q end): ') User2_num = input (f ' {user2} Please guess a number (at q end): ') if user1_num = = ' Q ' or user1_num = = ' Q ': print (' End ') Break else:user1_sum = Int (computer1_num) + int (computer2_num) + int (user1_num) user2_sum = Int (com Puter1_num) + int (computer2_num) + int (user2_num) if ABS (USER1_SUM-21) > Abs (USER2_SUM-21): Print (f ' {User1} points: {User1_sum},{user2} points: {user2_sum}, Result: {user2} wins ') user_info[user2][' win ']+=1 print (user_inf O) else:print (f ' {user1} points: {User1_sum},{user2} points: {user2_sum}, Result: {user1} wins ') user_info[user1][ ' Win ']+=1 priNT (User_info) 
    user1name:周杰伦user2name:高圆圆电脑随机产生一个数:1电脑随机产生一个数:7周杰伦请猜一个数(按q结束):5高圆圆请猜一个数(按q结束):6周杰伦点数:13,高圆圆点数:14,结果:高圆圆获胜{‘周杰伦‘: {‘win‘: 0}, ‘高圆圆‘: {‘win‘: 1}}电脑随机产生一个数:6电脑随机产生一个数:2周杰伦请猜一个数(按q结束):1高圆圆请猜一个数(按q结束):2周杰伦点数:9,高圆圆点数:10,结果:高圆圆获胜{‘周杰伦‘: {‘win‘: 0}, ‘高圆圆‘: {‘win‘: 2}}电脑随机产生一个数:7电脑随机产生一个数:1周杰伦请猜一个数(按q结束):10高圆圆请猜一个数(按q结束):1周杰伦点数:18,高圆圆点数:9,结果:周杰伦获胜{‘周杰伦‘: {‘win‘: 1}, ‘高圆圆‘: {‘win‘: 2}}电脑随机产生一个数:5电脑随机产生一个数:7周杰伦请猜一个数(按q结束):q高圆圆请猜一个数(按q结束):qend

    Case: Python play 21 points

    Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.