Use Python to complete SHUANGSE Qiu programs __python

Source: Internet
Author: User

Using Python to complete a shuangse Qiu program

The program can implement this function, but the algorithm should not be the best, if there is a better algorithm, please comment


Import random balls = [] #创建一个空列表
RedBall = List (range (1,34)) #创建红色球的列表
BlueBall = List (range (1,17)) #创建蓝色球的列表
For I in range (6):
x = Random.choice (redball) #每次从红色球里随机取出一个
If x not in balls: #判断取出的球在列表ball是否存在, if it does not exist, add it to the list
Balls.append (x)
If Len (balls)!= 6: #随机数可能存在取出来重复的数字, 6 times after the cycle to determine whether the list length is 6
y = Random.choice (RedBall) #如果不等于6, and then continue from the red ball list to remove a
If Y not in balls: #判断新取出来的是否在balls列表里
Balls.append (y) #不在里面就加进去
Balls.sort () #前面取出来的6个数字是无序的, General Shuangse Qiu are from small to large out

Balls.append (Random.choice (blueball)) #最后将蓝色球随机取出来一个放到列表里

Print (balls)


[3, 9, 14, 23, 26, 32, 8] would it not be possible to come out of this, accidentally in the 5 million


Note: The program will still appear when the red ball randomly taken, appeared several times to take out the same ball, I only made a judgment is equal to 6, then can only add one to the balls, this time will appear to take a total of less than 7 balls


Improved procedures:

Import Random
Balls = []
RedBall = List (range (1,34))
BlueBall = List (range (1,17))
For I in range (6):
x = Random.choice (RedBall)
If x not in balls:
Balls.append (x)
For j in Range (5):
If Len (balls)!= 6:
y = Random.choice (RedBall)
If Y not in balls:
Balls.append (y)
Else
Break
Balls.sort ()
Balls.append (Random.choice (BlueBall))
Print (balls)


[5, 11, 13, 27, 31, 32, 4]

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.