How does a Python game solve the inconvenience caused by game operations?

Source: Internet
Author: User

If you want to "smooth" the game, you can start with a Python game and learn the practical application technology of the Python game. If you are interested in it, you can browse the following articles. I hope you will gain some benefits. The following is a detailed introduction to this article.

This is a sunny Saturday afternoon. You can browse the Internet leisurely. suddenly you see a small game on the message board. it is very simple, and the problem is: Put the five numbers 56789 into {[] [] [] * [] [] to maximize the result ..

You should first say to yourself: "What's the problem? Just put the largest number in the maximum number of digits. "Maybe it's wrong if you forget it. each result depends on the number of rows placed in other locations. you are interested in it. You are learning a fun programming language. Why not practice it?

As a result, you opened your beloved Python game and started to think: "In fact, what I want is a program. I gave it a combination of various numbers, and then it automatically helped me find the biggest one. if I input 111, 111, 1, 2, it will know that the calculation is * 11 and * 12, the larger result is 111*12, and the combination and product are output. this program is not difficult."

 
 
  1. 1 # calc.py  
  2. 2 def calc(seq):  
  3. 3 maximum = 0 
  4. 4 max_item = []  
  5. 5 for i in seq:  
  6. 6 product = (i[0]*100 + i[1]*10 + i[2]) * (i[3]*10 + i[4])  
  7. 7 if product > maximum:  
  8. 8 maximum = product 
  9. 9 max_item = i  
  10. 10 elif product == maximum:  
  11. 11 max_item += ','+i  
  12. 12 return max_item, maximum  
  13. 13   
  14. 14 seq = [ [5,6,7,8,9], [5,6,7,9,8] ]  
  15. 15 max_item, maximum = calc(seq)  
  16. 16 print "Maximum at", max_item, ",product", maximum  

You tried,

 
 
  1. $python calc.py   
  2. Maximum at [5, 6, 7, 9, 8] ,product 90160   
  3.  

No problem. now you only need to give all the arrangement. after you have made a few attempts, I think it is too hard to use [100, 9], and I [0] * + I [1] * 10... the method seems too ugly, so you need to make a change. okay. Use a string. "56789", this input is more convenient, and int ("567") * int ("89") is much better, it should be faster. in addition, you can also shorten the program, which looks like written by experienced people. the above article introduces Python 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.