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 8824305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:34:46+00:00 2026-06-14T06:34:46+00:00

Just a quick run-down of what this does. Generates a secret string of numbers

  • 0

Just a quick run-down of what this does. Generates a secret string of numbers and 2 symbols (+ and *) which the user is to guess. Hangman I guess. I’ve solved all the other issues I’ve had and I’m down to one and a half. I’m a newbie.

Firstly, one problem is that sometimes running it (using F5 in IDLE) will show nothing. Just ” >> “, and I have to close the shell and re-run it.

Secondly, after winning and declining the offer to play again, it still asks to enter a guess. I suppose the while loop would still be running, so how could I stop it?

There may be other issues I’ve overlooked, but I’ve run through it a bit and everything seems to work. If there are any suggestions to make it better (but nothing too advanced), please feel free to point it out!

So here’s what I have:

from random import randint, choice
maxguesses = 11                                     
wins = 0                                            
losses = 0                                          
points = 0                                          
games = 0                                           
numguesses = 0                                      
allowed = ('0','1','2','3','4','5','6','7','8','9','0','+','*')

guesses = ''                                        
def genform():                                      
    global secret                                   
    global guesses
    global maxguesses
    sym = choice(['++','**','+*','*+'])             
    num = ''                                        
    max = randint(3,9)                             
    for i in range(max):                           
        num = num + str(randint(0,9))               
    secret = sym + num                              
    guesses = ''
    maxguesses = len(secret)+2                      
    return secret                                   

def evaluate(b):
    global evaluated
    evaluated = ''
    s1 = secret[0]
    s2 = secret[1]
    for i in range(2,len(secret)-1):
        evaluated = evaluated + secret[i]
        if (i%2 == 0):
            evaluated = evaluated + s1
        else:
            evaluated = evaluated + s2
    evaluated = evaluated + secret[-1]
    return evaluated

letters = set()
partial = ''
def takeguess(ch):
global numguesses, points, games, wins, partial, guesses
numguesses = numguesses + 1                              
print 'Remaining Guesses: ' + str(maxguesses-numguesses)
if (ch in partial) or (ch in guesses):                      
    print "\nYou've already guessed '%s'. Try again: " % ch
elif ch not in secret:
    guesses = guesses + ch
    print 'Wrong, guess again.'
elif ch in secret:                                         
    letters.add(ch)                                        
    print "\nGood guess, it's in the secret formula!"
    partial = ''.join([l if l in letters else '-' for l in secret])
    print 'Formula so far: ' + partial
    if partial == secret:                                   
        print 'You win!'
        wins = wins + 1
        points = points + 2
        games = games + 1
        bonus = raw_input("Evaluate the formula for 10 bonus points: ") 
        if bonus == str(eval(evaluate(secret))):                        
            points = points + 10
            print "That's correct! +10 points."
            print eval(evaluate(secret)) #REMOVE AFTER
            play_again()
        else:                                              
            print "That's incorrect. The right answer is", eval(evaluate(secret))
            play_again()

def play_again():
global numguesses, guesses, partial, letters
letters = set()
    if points < 2:
        print "\nYou don't have enough points to play again.\nGames Played: " + str(games) + '\nPoints: ' + str(points)
    else:        
        ans = raw_input('Would you like to play again? [y/n] ')
        if ans in ('yY'):
            numguesses = 0
            guesses = ''
            partial = ''
            play()
        elif ans in ('nN'):
            print '\nOkay, goodbye.\nWins: ' + str(wins) + '\nLosses: ' + str(losses) + '\nPoints: ' + str(points)
        else:
            print 'Invalid input.'
            play_again()

def play():
    global numguesses, guesses, partial, points, games, losses
    genform()
    print 'Unsolved formula: ' + ('-'*len(secret))
    print 'You have ' + str(maxguesses) + ' guesses.'
    print secret #REMOVE AFTER
    while (numguesses < maxguesses):
        guess = raw_input('\nEnter a guess: ')    # Receive guess
        if guess not in allowed:                # Check if valid
            print '\nInvalid guess, please enter a single digit, *, or +. Try again: '  
        elif partial != secret:
            takeguess(guess)
            print guesses # REMOVE AFTER
        if numguesses == maxguesses:
            points = points - 2
            games = games + 1
            losses = losses + 1
            print 'Sorry, you lose. The answer is: ' + secret
            play_again()
            return

print 'Welcome to hangman!\n'
play()
  • 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-14T06:34:49+00:00Added an answer on June 14, 2026 at 6:34 am

    Your biggest problem with this code is that you are using global variables everywhere; this would be much better if you stored values in python objects and initialized your object every time you start a new game.

    To fix what you are using now, you need to initialize letters in the play_again() function…

    def play_again():
        global numguesses, guesses, partial
        global letters
        letters=set()   # Reset letters to be empty
        if points < 2:
            print "\nYou don't have enough points to play again.\nGames Played: " + str(games) + '\nPoints: ' + str(points)
        else:
            ans = raw_input('Would you like to play again? [y/n] ')
            if ans in ('yY'):
                numguesses = 0
                guesses = ''
                partial = ''
                play()
            elif ans in ('nN'):
                print '\nOkay, goodbye.\nWins: ' + str(wins) + '\nLosses: ' + str(losses) + '\nPoints: ' + str(points)
            else:
                print 'Invalid input.'
                play_again()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

just a quick question. I've got this table in a Word template which have
I'm absolutely bewildered by this issue. Let me give a quick run down of
just a quick question, was trying different ways to accomplish this, but ended with
Just a quick question, does anyone know if there are restrictions on creating Performance
Can someone just give me a quick run through of what SAP is. I
Just a quick question my textbox1.text seems to get focus when I run the
Its just a quick question: Is it possible to run a falsh based voice
I'm trying to run a very quick update query to record that a user's
Just a quick on this morning. Is it possible to create a file and
Just a quick one about installing a PHP website, are there any tools out

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.