Vi write a game of scissors and stone

Source: Internet
Author: User

Vi write a game of scissors and stone

Code:

Import randomprint ("if you don't want to play a game, press q to exit! ") While True: player = input (" Please input 0 scissors 1 stone 2 cloth: ") computer = random. randint (0, 2) if player = "q": print ("Bye! ") Break else: if player. isdigit (): player = int (player) if player <= 2 and player> = 0: #2. judge the user input, and then display the corresponding result if (player = 0 and computer = 2) or (player = 1 and computer = 0) or (player = 2 and computer = 1): print ("win! You can buy milk powder! ") Elif player = computer: print! Go home and get the money. Come back! ") Else: print (" the input number is out of the range! ") Continue else: print (" input error! ") Continue

Thoughts:

In a simple stone scissors game, the input error has not been determined at the beginning, for example, if the user does not enter an integer at the beginning, and the range is determined when the input is an integer. This mini-game is improved by adding the if judgment statement to exclude user input errors. If you are interested, you can take it and relax.★

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.