Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8593087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:54:27+00:00 2026-06-11T23:54:27+00:00

I am just learning python (first language ever) and am implementing what I find

  • 0

I am just learning python (first language ever) and am implementing what I find in ways that I find fun. I built a pseudo slot machine odds calculator. However, it stops at one Grand prize win. Is there a way to make it run over and over to give an average number of attempts for n amount of games to get the grand prize?

Here’s my code

#!/usr/bin/env python
import random

a = 1

while a >0 :
    l1 = random.randrange(36)
    l2 = random.randrange(36)
    l3 = random.randrange(36)

    print l1, l2, l3
    if l1 == l2 == l3 == 7:
        print 'grand prize winner!!!'
        break
    elif l1 == l2 == l3:
        print 'you won! congratulations'
        print 'it took', a, 'attempts to win'
    else:
        a += 1
        print 'sorry...  try again'
        print 'attempt', a

Also, is there a way to tell me how many normal wins there were during the course of winning that grand prize

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:54:28+00:00Added an answer on June 11, 2026 at 11:54 pm

    The break statement in the grand prize if block exits the outer while loop. If you want it to keep going, remove the break. Also, as a style point, while True: or while 1: is a bit clearer of a way to create an infinite loop. As far as the second part of your question, you have the a counter, but you may want to capture more data, like so:

    import random
    
    def play(till_jackpot_count):
        game_data_per_jackpot = [{'plays' : 0, 'wins' : 0}]
        wheel_values = xrange(36)
        wheels = [0, 0, 0]
        while till_jackpot_count >= len(game_data_per_jackpot):
            wheels = [random.choice(wheel_values) for wheel in wheels]
            game_data_per_jackpot[-1]['plays'] += 1
            print '%d plays since last jackpot' % game_data_per_jackpot[-1]['plays']
            print '%d wins since last jackpot' % game_data_per_jackpot[-1]['wins']
            print '%d total plays' % sum([data['plays'] for data in game_data_per_jackpot])
            print '%d total wins' % sum([data['wins'] for data in game_data_per_jackpot])
            print '%d total jackpots' % (len(game_data_per_jackpot) - 1)
            print 'this play: {} {} {}'.format(*wheels)
            if len(set(wheels)) == 1:
                if wheels[0] == 7:
                    print 'jackpot!'
                    game_data_per_jackpot.append({'plays' : 0, 'wins' : 0})
                else:
                    print 'win!'
                    game_data_per_jackpot[-1]['wins'] += 1
        return game_data_per_jackpot[:-1]
    
    play(10)
    

    I also snuck a control till_jackpot_count in at the top that will make the loop end after that number of jackpots. The function also returns the test results if you want to analyze them further outside of the function itself, but the result just gets dropped here because it is not assigned to anything.

    For your own study, this code uses lists ([]), dicts ({}), tuples (()), old style string formatting ('%d' % var), new style string formatting ('{} {} {}'.format(*iterable)), list comprehensions ([a for a in b]), slicing (list[:]), and a few builtins (sum, len) in addition to the random library and while loop you’re already familiar with. I also swapped out your random.randrange() for a somewhat simpler, probably more efficient, random.sample() of a pre-built xrange().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started learning Python a couple of days ago as my first language
I just started learning Python (as my first language, so I know next to
I just started learning my first real programming language, Python. I'd like to know
I'm just learning about python. I'm fairly new. I have the following code that
I am just learning python on my ubuntu 8.04 machine which comes with python
I am learning Python and I just read in a book that Python 3
I am just learning programming in Python for fun. I was writing a palindrome
I just start learning Python + Tornado for my web servers. Every time I
I have just started learning python version 3 and trying to create a file
So ive just started learning python on WAMP, ive got the results of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.