Learn about Python, use Python to make everyday scissors, guess the price games

Source: Internet
Author: User

 The first contact with Python, it was attracted by its concise and beautiful language, is the so-called life is short, Python as a song. Python has become more hot in recent years, without some of its features:

1. Easy to learn: Python has a relatively small number of keywords, simple structure, and a well-defined syntax to learn more easily.
2. Easy to read: Python code definition is clearer.
3. Easy maintenance: The success of Python is that its source code is fairly easy to maintain.
4. An extensive standard library: one of the biggest advantages of Python is the rich library, cross-platform, compatible with Unix,windows and Macintosh very well.
5. Interactive mode: Interactive mode of support, you can enter the execution code from the terminal and obtain the results of the language, interactive testing and debugging code snippets.
6. Portable: Based on its open source features, Python has been ported (that is, making it work) to many platforms.
7. Extensible: If you need a critical code that runs fast, or if you want to write some algorithms that you don't like to open, you can use C or C + + to complete that part of the program and then call it from your Python program.
8. Database: Python provides the interface for all major business databases.
9.GUI Programming: Python support GUI can be created and ported to many system calls.
10. Embeddable: You can embed python in a C + + program to give your program's users the ability to "script".

Through Python, using the If branch, while loop we can make the basic judgment logic aspects of everyday seeing games, like scissors, guessing price, 99 multiplication table.

Think of the era that did not enter the full range of smartphones, many people's mobile phone, or the page must be scissors of the small game program, in fact, compiling this kind of small game program is very simple, through a simple if branch judgment, while loop can achieve the purpose.

Scissors game code is as follows:

Import Random
player = input (' Please input scissors [0] stone [1] cloth [2]: ')
computer = random.randint (0,2)
player = int (player)
if (player = = 0 and computer = = 2) or (player = = 1 and computer = = 0) or (player = = 2 and computer = = 1):
print (' Congratulations, you win ')
elif Player = = computer:
print (' Draw, showdown to Daylight ')
Else:
print (' Lost ')

The operation results are as follows

By referring to random arbitrary attributes, the If Judgment branch will be able to satisfy the execution of this small program.

Guess the price game code is as follows:

player = int (input (' Guess product Price: '))
Money =
i = 1
While I <=:
if player = = Money:
print (' Kong's right ')
Break
elif player > Money:
print (' more ')
player = int (input (' Guess product Price: '))
Else:
print (' less ')
player = int (input (' Guess product Price: '))
i + = 1
Else:
print (' End ')

The results of the operation are as follows:

By the while loop to limit the number of times to guess the price, more than a certain number of times can not guess, through if to see whether the price of each guess is more or less to remind users

Learn about Python, use Python to make everyday scissors, guess the price games

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.