概率问题,小白求解
本帖最后由 Ddaniel1177 于 2021-9-12 18:30 编辑买彩票,然后问至少要买多少张才能至少中三个白球。
这是我目前的代码
import random
print("Buy a ticket with 5 white balls and 1 red ball ")
mywhite = random.sample(range(1, 69), 5)
myred = random.randint(1, 26)
white =
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
and 1 red ball
"""
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 = } 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 =
myRed = 1
count = 0
while not game(myWhite, myRed):
count += 1
print(count) 你該不會是想要用代碼運算答案去買樂透吧?{:5_94:}兄弟,這和你當初題目設定不一樣哦,那些沒有中獎的不用打印?只算出次數?{:5_94:}{:5_94:} 傻眼貓咪 发表于 2021-9-12 19:22
你該不會是想要用代碼運算答案去買樂透吧?兄弟,這和你當初題目設定不一樣哦,那些沒有中獎的不用 ...
哈哈哈,这是一个题啊,只是附加的问题。
我如果想买彩票,我就会问全中该怎么算了{:10_250:} 傻眼貓咪 发表于 2021-9-12 19:22
你該不會是想要用代碼運算答案去買樂透吧?兄弟,這和你當初題目設定不一樣哦,那些沒有中獎的不用 ...
大佬,为啥我把你代码的最后一段放进我的里面,它就开始一直循环啊。难道是要出现三次中3个白球的情况才停下来吗?有什么方法可以直接得出结果吗? Ddaniel1177 发表于 2021-9-12 19:45
大佬,为啥我把你代码的最后一段放进我的里面,它就开始一直循环啊。难道是要出现三次中3个白球的情况才 ...
1. 用我的代碼就不會出現無限循環,你把我的後半段代碼放進別人的前半段代碼當然有問題咯,看來我的代碼並不是你想要的啊。{:5_99:}
2. 你用我的代碼你會發現,只求中獎三顆白球以上的機率不高啊,幸運可能 300 次以內,否則上千上萬次數,你確定全部結果要打印?
不好意思兄弟,我的 Python 技術不是很厲害,你的題目我無法解了,你看看有沒有其他大神可以幫助你 我运行了一下你的代码,但是为什么其它的都不显示,只显示一个次数啊 Ddaniel1177 发表于 2021-9-12 20:30
我运行了一下你的代码,但是为什么其它的都不显示,只显示一个次数啊
你上面要求中獎三顆白色球的次數啊,不是嗎?{:5_94:}
兄弟你先想清楚,你真正想要什麼,問題必須要有邏輯,有條有理,不然別人很難解決你的問題啊~
大家的代碼只是參考,你必須要了解其原理啊,然後試試自己動手寫吧
盲猜应该买 69*68*67 = 314364 张
页:
[1]