http://www.megaupload.com/?d=GZJD9V51

Above is the program spec.

and below is what I did so far.

numStr = raw_input("'p' to be prompted for the secret number or 'g' to have it generated: ")
p = numStr
print
print

true = p
while true:
    secret = raw_input("Enter the 'secret' number (five digits, no duplicates):")
    if secret.isdigit() == True:
        if len(secret) != 5:
            print 'Secret number must contain 5 digits. Try again.'
            print
            continue
        else:
            check = 1
            for dig in secret :
                if dig in secret[check:5]:
                    print 'Your secret number cannot have duplicate number. Try again.'
                    continue
                check += 1
                print
                print

            print 'Secret number was successfully entered'
            n = 10
            print 'The user will be permitted', n, 'guesses'
            print
            while true:
                numStr1 = raw_input("Enter a 5-digit guess (or 'q' to quit the game):")
                gues = numStr1
                x = gues
                if gues.isdigit() == x:
                    if len(gues) != 5:
                        print 'The  guess must be a 5-digit number. Guess again.'
                        

                    elif gues == secret:
                        print 'Congratulations, you correctly guessed the secret number'

        
print 'Thanks for playing. Try again later!'

I know some parts are like weird and I just don't get it.
As a python newbie, it's really hard figuring out stuff with no prior programming background or experience, but I am really interested in programming. I would really appreciate you guys' help.

Recommended Answers

All 9 Replies

Failed to download the program. What do you try to accomplish?

Successfully downloaded the spec. It is a mastermind game.

My advises:
Write down the workflow of the actions. Take notice of the tries and so on.. On paper it is enough. Example
Write a function that decides if the secret number is good. No printing, just return True or False...
Write a function that reports the number of good guesses on any place and the good guesses on godd place.
Write tests for the function, or just test them separately by hand...
Glue the things together....

The code should look like:

import things

def good_number(inp): 
    "decides if it a good number returns True/False"
    pass

def report_result(inp, secret):
    "Match inp to secret and return the result: (good guess wrong place, good guess good place)"

maxguess=10

print("gimme the secret")
phase="secret" # ("secret","guessing")
secret=None
tries=0
while True: # mainloop
    key=raw_input()
    if #key is the quit key, then print out the results
    if phase=="secret":
        ret=good_number(key)
        if ret:
            print("\n"*45)
            secret=key
            phase="guessing"
        else:
            print("bad number")
            print("gimme the secret")
    else:
        ret=report_result(key,secret)
        if # reti is not good
            print(ret)
            print("Left %s tries of %s " % (tries,maxguess))
        else:
            print("you won") 
            #statistics
            break

^hm... I didn't learn how to use functions. So I'm lost T_T

interesting...how professor told us not to use functions yet on this project....or else we lose points.
We're supposed to use while, strings, and if statements.

Ah well im sure he will come to teach you them, everything you need can be done without functions without a fuss though so dont worry.

Maybe the professor wants to show how messy a relatively little code can become without functions...
Some errors in the code:

check = 1
for dig in secret :
    if dig in secret[check:5]:
        print 'Your secret number cannot have duplicate number. Try again.'
        continue
    check += 1
    print
    print

The continue statement continues the loop in wich the statement resides. That can be a problem.
As a simple but not too effective approach would be:

if len(secret)>len(set(secret)): 
    print "it is wrong"
    continue # the outer loop

Constructing a set removes the duplicates.

Your code misses the result checking:

good_guess,good_place=0,0
for i,c in enumerate(guess):
    if c in secret:
        good_guess+=1
    if secret[i]==c:
        good_place+=1
if good_place=len(secret): print "you won"

You don't handle the case when the secret is not a digit.
You don't handle quitting.

I hope this helps.

thanks so much T_T

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.