|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Ddaniel1177 于 2021-9-12 18:30 编辑
买彩票,然后问至少要买多少张才能至少中三个白球。
这是我目前的代码import random
print("Buy a ticket with 5 white balls [not identical, between 1-69] and 1 red ball [between 1-26]")
mywhite = random.sample(range(1, 69), 5)
myred = random.randint(1, 26)
white = [32, 35, 40, 52, 54]
red = 1
print("Your selection for white balls are: ", *mywhite )
print("Your selection for red balls is: ", myred )
print("Winning numbers for white balls:", *white)
print("Winning number for red ball:", red)
matchwhite = 0
for i, j in zip(mywhite, white):
if i == j:
matchwhite += 1
matchred = 1 if myred == red else 0
if matchwhite == 0 and matchred ==0:
print ("No matching. Thanks for your contribution!")
elif matchred:
if matchwhite == 0:
print ("Matching only the red ball: $4")
elif matchwhite == 1:
print ("Matching the red ball and one white ball: $4")
elif matchwhite == 2:
print ("Matching the red ball and two white balls: $7")
elif matchwhite == 3:
print ("Matching the red ball and three white balls: $100")
elif matchwhite == 4:
print ("Matching the red ball and four white balls: $10,000")
else:
print ("Matching five white balls and the red ball: Jackpot")
else:
if matchwhite == 3:
print ("Matching three white balls: $7")
elif matchwhite == 4:
print ("Matching four white balls: $100")
else:
print ("Matching five white balls: $1,000,000")
求大佬能帮我解答一下,感激不尽!!
import random
def game(myWhite, myRed):
"""
Buy a ticket with 5 white balls [not identical, between 1-69]
and 1 red ball [between 1-26]
"""
white = set(random.sample(range(1, 70), 5))
red = random.choice(range(1, 27))
# string = {1: "first", 2: "second", 3: "third", 4: "forth", 5: "fiveth"}
# myWhite = [int(input(f"Your {string[i]} number for white balls\n")) for i in range(1, 6)]
# myRed = int(input("Your number for red ball\n"))
# print("Your selection for white balls are: ", *myWhite )
# print("Your selection for red balls is: ", myRed )
# print("Winning numbers for white balls:", *white)
# print("Winning number for red ball:", red)
myWhite = set(myWhite)
if myRed == red:
return len(myWhite & white) >= 3
# if not (myWhite & white):
# print("Matching only the red ball: $4")
# elif len(myWhite & white) == 1:
# print("Matching the red ball and one white ball: $4")
# elif len(myWhite & white) == 2:
# print("Matching the red ball and two white balls: $7")
# elif len(myWhite & white) == 3:
# print("Matching the red ball and three white balls: $100")
# elif len(myWhite & white) == 4:
# print("Matching the red ball and four white balls: $10,000")
# elif len(myWhite & white) == 5:
# print("Matching five white balls and the red ball: Jackpot")
elif myRed != red:
return False
# if not (myWhite & white):
# print("No matching. Thanks for your contribution!")
# elif len(myWhite & white) == 3:
# print("Matching three white balls: $7")
# elif len(myWhite & white) == 4:
# print("Matching four white balls: $100")
# elif len(myWhite & white) == 5:
# print("Matching five white balls: $1,000,000")
if __name__ == "__main__":
# print(game.__doc__)
myWhite = [32, 35, 40, 52, 54]
myRed = 1
count = 0
while not game(myWhite, myRed):
count += 1
print(count)
|
|