Python老虎机的问题 随机导入 余额=50 def生成器(): 插槽=0 计数=0 gen=random.random() 当计数小于3时: 计数=计数+1 如果gen

Python老虎机的问题 随机导入 余额=50 def生成器(): 插槽=0 计数=0 gen=random.random() 当计数小于3时: 计数=计数+1 如果gen,python,python-2.7,Python,Python 2.7,使用范围函数选择3次并将其存储在列表中 import random balance = 50 def generator(): slot = 0 count = 0 gen = random.random() while count < 3: count = count + 1 if

使用范围函数选择3次并将其存储在列表中

        import random
        balance = 50

        def generator():
            slot = 0
            count = 0
            gen = random.random()
            while count < 3:
                count = count + 1
                if gen <= 0.01:
                        slot = 'Cherry'
                elif gen <= 0.06:
                        slot = 'Diamond'
                elif gen <= 0.16:
                        slot = 'Heart'
                elif gen <= 0.36:
                        slot = 'Spade'
                elif gen <= 0.66:
                        slot = 'Club'
                elif gen <= 1:
                        slot = 'Monkey'
                else:
                    break

            return slot


        def win(generator):
            if generator() == 'Monkey' and generator() == 'Monkey' and generator() == 'Monkey':
                balance = balance + 2122

        print "Welcome to the International Slot Machine"
        print ""
        print "Balance: $",balance
        print ''
        spinyn = (raw_input("Would you like to spin? $5 per spin. Enter y or n:\n"))
        while True:
            if spinyn == "y":
                break
            elif spinyn == "n":
                print "Final Balance: $",balance
                print "Thank you for using the International Slot Machine"
                raise SystemExit
            else:
                spinyn = raw_input('\033[31mPlease enter only y or n.\033[0m\n')

        spin = (raw_input("Press enter to spin for $5:\n"))
        while True:
            if spin == '':
                balance = balance - 5
                if balance <= 0:
                    print ""
                    print "Final Balance: $",balance
                    print "You have run out of money, the game has now ended."
                    raise SystemExit
                print ""
                print "\033[34mResult:\033[0m"
                print "\033[34m-------\033[0m"
                print generator()
                print generator()
                print generator()
                print ""
                print "New balance:$",balance
                print ""
                spinagain = (raw_input("Would you like to spin again? Press enter to spin again, type anything to exit.\n"))
                while True:
                    if spinagain == "":
                        break
                    else:
                        print "Final Balance: $",balance
                        print "Thank you for using the International Slot Machine"
                        raise SystemExit
            else:
                spin = (raw_input("Please press enter to spin.\n"))

使用范围功能选择3次并将其存储在列表中

        import random
        balance = 50

        def generator():
            slot = 0
            count = 0
            gen = random.random()
            while count < 3:
                count = count + 1
                if gen <= 0.01:
                        slot = 'Cherry'
                elif gen <= 0.06:
                        slot = 'Diamond'
                elif gen <= 0.16:
                        slot = 'Heart'
                elif gen <= 0.36:
                        slot = 'Spade'
                elif gen <= 0.66:
                        slot = 'Club'
                elif gen <= 1:
                        slot = 'Monkey'
                else:
                    break

            return slot


        def win(generator):
            if generator() == 'Monkey' and generator() == 'Monkey' and generator() == 'Monkey':
                balance = balance + 2122

        print "Welcome to the International Slot Machine"
        print ""
        print "Balance: $",balance
        print ''
        spinyn = (raw_input("Would you like to spin? $5 per spin. Enter y or n:\n"))
        while True:
            if spinyn == "y":
                break
            elif spinyn == "n":
                print "Final Balance: $",balance
                print "Thank you for using the International Slot Machine"
                raise SystemExit
            else:
                spinyn = raw_input('\033[31mPlease enter only y or n.\033[0m\n')

        spin = (raw_input("Press enter to spin for $5:\n"))
        while True:
            if spin == '':
                balance = balance - 5
                if balance <= 0:
                    print ""
                    print "Final Balance: $",balance
                    print "You have run out of money, the game has now ended."
                    raise SystemExit
                print ""
                print "\033[34mResult:\033[0m"
                print "\033[34m-------\033[0m"
                print generator()
                print generator()
                print generator()
                print ""
                print "New balance:$",balance
                print ""
                spinagain = (raw_input("Would you like to spin again? Press enter to spin again, type anything to exit.\n"))
                while True:
                    if spinagain == "":
                        break
                    else:
                        print "Final Balance: $",balance
                        print "Thank you for using the International Slot Machine"
                        raise SystemExit
            else:
                spin = (raw_input("Please press enter to spin.\n"))

使用范围功能选择3次并将其存储在列表中

        import random
        balance = 50

        def generator():
            slot = 0
            count = 0
            gen = random.random()
            while count < 3:
                count = count + 1
                if gen <= 0.01:
                        slot = 'Cherry'
                elif gen <= 0.06:
                        slot = 'Diamond'
                elif gen <= 0.16:
                        slot = 'Heart'
                elif gen <= 0.36:
                        slot = 'Spade'
                elif gen <= 0.66:
                        slot = 'Club'
                elif gen <= 1:
                        slot = 'Monkey'
                else:
                    break

            return slot


        def win(generator):
            if generator() == 'Monkey' and generator() == 'Monkey' and generator() == 'Monkey':
                balance = balance + 2122

        print "Welcome to the International Slot Machine"
        print ""
        print "Balance: $",balance
        print ''
        spinyn = (raw_input("Would you like to spin? $5 per spin. Enter y or n:\n"))
        while True:
            if spinyn == "y":
                break
            elif spinyn == "n":
                print "Final Balance: $",balance
                print "Thank you for using the International Slot Machine"
                raise SystemExit
            else:
                spinyn = raw_input('\033[31mPlease enter only y or n.\033[0m\n')

        spin = (raw_input("Press enter to spin for $5:\n"))
        while True:
            if spin == '':
                balance = balance - 5
                if balance <= 0:
                    print ""
                    print "Final Balance: $",balance
                    print "You have run out of money, the game has now ended."
                    raise SystemExit
                print ""
                print "\033[34mResult:\033[0m"
                print "\033[34m-------\033[0m"
                print generator()
                print generator()
                print generator()
                print ""
                print "New balance:$",balance
                print ""
                spinagain = (raw_input("Would you like to spin again? Press enter to spin again, type anything to exit.\n"))
                while True:
                    if spinagain == "":
                        break
                    else:
                        print "Final Balance: $",balance
                        print "Thank you for using the International Slot Machine"
                        raise SystemExit
            else:
                spin = (raw_input("Please press enter to spin.\n"))

使用范围功能选择3次并将其存储在列表中

        import random
        balance = 50

        def generator():
            slot = 0
            count = 0
            gen = random.random()
            while count < 3:
                count = count + 1
                if gen <= 0.01:
                        slot = 'Cherry'
                elif gen <= 0.06:
                        slot = 'Diamond'
                elif gen <= 0.16:
                        slot = 'Heart'
                elif gen <= 0.36:
                        slot = 'Spade'
                elif gen <= 0.66:
                        slot = 'Club'
                elif gen <= 1:
                        slot = 'Monkey'
                else:
                    break

            return slot


        def win(generator):
            if generator() == 'Monkey' and generator() == 'Monkey' and generator() == 'Monkey':
                balance = balance + 2122

        print "Welcome to the International Slot Machine"
        print ""
        print "Balance: $",balance
        print ''
        spinyn = (raw_input("Would you like to spin? $5 per spin. Enter y or n:\n"))
        while True:
            if spinyn == "y":
                break
            elif spinyn == "n":
                print "Final Balance: $",balance
                print "Thank you for using the International Slot Machine"
                raise SystemExit
            else:
                spinyn = raw_input('\033[31mPlease enter only y or n.\033[0m\n')

        spin = (raw_input("Press enter to spin for $5:\n"))
        while True:
            if spin == '':
                balance = balance - 5
                if balance <= 0:
                    print ""
                    print "Final Balance: $",balance
                    print "You have run out of money, the game has now ended."
                    raise SystemExit
                print ""
                print "\033[34mResult:\033[0m"
                print "\033[34m-------\033[0m"
                print generator()
                print generator()
                print generator()
                print ""
                print "New balance:$",balance
                print ""
                spinagain = (raw_input("Would you like to spin again? Press enter to spin again, type anything to exit.\n"))
                while True:
                    if spinagain == "":
                        break
                    else:
                        print "Final Balance: $",balance
                        print "Thank you for using the International Slot Machine"
                        raise SystemExit
            else:
                spin = (raw_input("Please press enter to spin.\n"))

生成三个值后,让生成器返回一个包含三个值的列表或元组,使用
random.choice()
比使用
random.random()
更容易
random.choice()
为值列表随机选择一个元素/iterable,所有元素的概率相等。范例-

import random

choices_list=[]
for ctr in range(3):
    gen = random.choice(['Cherry', 'Diamond', 'Heart',
                     'Spade', 'Club', 'Monkey'])
    choices_list.append(gen)

print choices_list
def generator():
    ret = []
    for _ in range(3):
        ret.append(random.choice(['Cherry','Diamond','Heart','Spade','Club','Monkey']))
    return tuple(ret)
如果希望不同元素的概率不同,可以保留当前方法,只需在该方法上循环三次,然后像上面那样附加到
ret
,然后从中返回
ret

然后,在win函数中,保留一个字典,以便键是组合的元组,值是用户赢得的金额,然后您只需使用默认值为0的
.get()
,即可获得用户赢得的金额。不要将
生成器
作为参数传入。范例-

import random

choices_list=[]
for ctr in range(3):
    gen = random.choice(['Cherry', 'Diamond', 'Heart',
                     'Spade', 'Club', 'Monkey'])
    choices_list.append(gen)

print choices_list
def generator():
    ret = []
    for _ in range(3):
        ret.append(random.choice(['Cherry','Diamond','Heart','Spade','Club','Monkey']))
    return tuple(ret)

然后在主循环中,调用此
win()
函数滚动并查看用户赢了多少。

使生成器在生成三个值后返回一个包含三个值的列表或元组,而且使用
random.choice()
比使用
random.random()
更容易
random.choice()
为值列表随机选择一个元素/iterable,所有元素的概率相等。范例-

import random

choices_list=[]
for ctr in range(3):
    gen = random.choice(['Cherry', 'Diamond', 'Heart',
                     'Spade', 'Club', 'Monkey'])
    choices_list.append(gen)

print choices_list
def generator():
    ret = []
    for _ in range(3):
        ret.append(random.choice(['Cherry','Diamond','Heart','Spade','Club','Monkey']))
    return tuple(ret)
如果希望不同元素的概率不同,可以保留当前方法,只需在该方法上循环三次,然后像上面那样附加到
ret
,然后从中返回
ret

然后,在win函数中,保留一个字典,以便键是组合的元组,值是用户赢得的金额,然后您只需使用默认值为0的
.get()
,即可获得用户赢得的金额。不要将
生成器
作为参数传入。范例-

import random

choices_list=[]
for ctr in range(3):
    gen = random.choice(['Cherry', 'Diamond', 'Heart',
                     'Spade', 'Club', 'Monkey'])
    choices_list.append(gen)

print choices_list
def generator():
    ret = []
    for _ in range(3):
        ret.append(random.choice(['Cherry','Diamond','Heart','Spade','Club','Monkey']))
    return tuple(ret)

然后在主循环中,调用此
win()
函数滚动并查看用户赢了多少。

使生成器在生成三个值后返回一个包含三个值的列表或元组,而且使用
random.choice()
比使用
random.random()
更容易
random.choice()
为值列表随机选择一个元素/iterable,所有元素的概率相等。范例-

import random

choices_list=[]
for ctr in range(3):
    gen = random.choice(['Cherry', 'Diamond', 'Heart',
                     'Spade', 'Club', 'Monkey'])
    choices_list.append(gen)

print choices_list
def generator():
    ret = []
    for _ in range(3):
        ret.append(random.choice(['Cherry','Diamond','Heart','Spade','Club','Monkey']))
    return tuple(ret)
如果希望不同元素的概率不同,可以保留当前方法,只需在该方法上循环三次,然后像上面那样附加到
ret
,然后从中返回
ret

然后,在win函数中,保留一个字典,以便键是组合的元组,值是用户赢得的金额,然后您只需使用默认值为0的
.get()
,即可获得用户赢得的金额。不要将
生成器
作为参数传入。范例-

import random

choices_list=[]
for ctr in range(3):
    gen = random.choice(['Cherry', 'Diamond', 'Heart',
                     'Spade', 'Club', 'Monkey'])
    choices_list.append(gen)

print choices_list
def generator():
    ret = []
    for _ in range(3):
        ret.append(random.choice(['Cherry','Diamond','Heart','Spade','Club','Monkey']))
    return tuple(ret)

然后在主循环中,调用此
win()
函数滚动并查看用户赢了多少。

使生成器在生成三个值后返回一个包含三个值的列表或元组,而且使用
random.choice()
比使用
random.random()
更容易
random.choice()
为值列表随机选择一个元素/iterable,所有元素的概率相等。范例-

import random

choices_list=[]
for ctr in range(3):
    gen = random.choice(['Cherry', 'Diamond', 'Heart',
                     'Spade', 'Club', 'Monkey'])
    choices_list.append(gen)

print choices_list
def generator():
    ret = []
    for _ in range(3):
        ret.append(random.choice(['Cherry','Diamond','Heart','Spade','Club','Monkey']))
    return tuple(ret)
如果希望不同元素的概率不同,可以保留当前方法,只需在该方法上循环三次,然后像上面那样附加到
ret
,然后从中返回
ret

然后,在win函数中,保留一个字典,以便键是组合的元组,值是用户赢得的金额,然后您只需使用默认值为0的
.get()
,即可获得用户赢得的金额。不要将
生成器
作为参数传入。范例-

import random

choices_list=[]
for ctr in range(3):
    gen = random.choice(['Cherry', 'Diamond', 'Heart',
                     'Spade', 'Club', 'Monkey'])
    choices_list.append(gen)

print choices_list
def generator():
    ret = []
    for _ in range(3):
        ret.append(random.choice(['Cherry','Diamond','Heart','Spade','Club','Monkey']))
    return tuple(ret)


然后在主循环中,调用这个
win()
函数滚动,看看用户赢了多少钱。

非常感谢,但是我不理解def win()函数?你不理解其中的什么?是字典
d
?@ScottMiller请记住
random.choice
将以相同的概率从数组中进行选择,这与生成器函数具有不同的probabilties@AnandSKumar我不明白它在做什么。你把这些组合和它们对应的赢值存储在字典里。然后调用generator()获取当前滚动。然后您使用
dictionary.get()
从字典中获取win值,您还指定了默认值
0
(作为第二个参数),因此如果字典中不存在滚动,我们将返回默认值0,这意味着没有赢得任何东西,然后我们从函数中返回该值。非常感谢,但是,我不了解def win()函数?您不了解其中的哪些内容?是字典
d
?@ScottMiller请记住
random.choice
将以相同的概率从数组中进行选择,这与生成器函数具有不同的probabilties@AnandSKumar我不明白它在做什么。你把这些组合和它们对应的赢值存储在字典里。然后调用generator()获取当前滚动。然后使用
dictionary.get()
从字典中获取win值,还指定了默认值
0
(作为第二个参数),因此如果字典中不存在滚动,我们将返回默认值0