1 Replies - 1727 Views - Last Post: 04 May 2011 - 02:38 AM Rate Topic: -----

#1 successfulangel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-May 11

Can't stop looping in my number guessing game

Posted 03 May 2011 - 08:22 AM

I can't find the error in my program.In the part of Play() function,i can't guess my number and the program run again and again.So if you find some error in my program,Pls inform me.Here is my coding.
import random
def main():
    print"Welcome to the Greater CP1200 Guessing Game!\nWritten by successful,April 2011\nSee if you get a high score"
    name=raw_input("What is your name")
    while name=="":
        name2=raw_input("Come on,tell me your name")
        if name2!="":
            name=name2
            break
    menu()
def menu():
    print"Menu:\n(V)iew High Scores\n(P)lay Game\n(S)et Game Limits\n(Q)uit"
    input()
def input():
    response=raw_input()
    if response=="V":
        f=open("score.txt",'r')
        for line in f:
            print line,
            menu()
    elif response=="P":
        play()
    elif response=="S":
        setgame()
    elif response=="Q":
        print"Thanks for playing"
    
    else:
        print "Invalid Option"
        menu()
def play():
    guessnumber=random.randint(1,42)
    count=0
    while True:
        count=count+1
        askno=int(raw_input(("Please Enter your guess no,between 1 and 42:"))
        if askno<0 or askno>42:
            print "Invalid guess"
            play()
        elif askno=="":
            print "Invalid guess"
            play()
        elif askno>guessnumber:
            print"My number is smaller"
            play()
        elif askno<guessnumber:
            print"My number is larger"
            play()
        else:
            print"You got it"
            print"Well done",name,".","You guess it in",count,"guesses."
            save=raw_input("Do you want to save your score or not\n(Y)es or(N)o")
            if save=="Y":
                    f=open("score.txt",'a')
                    f.write("\n"+name+" "+str(count))
                    f.close()
                    break
            elif request=="N":
                break
            menu()
main()


This post has been edited by macosxnerd101: 03 May 2011 - 08:24 AM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Can't stop looping in my number guessing game

#2 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Re: Can't stop looping in my number guessing game

Posted 04 May 2011 - 02:38 AM

Every time a you make a guess in your game, you call the play() function again, restarting your game instead of continuing it. What you need to do to continue it and not restart it is take out the calls to play() unless you want to restart the game. Then after each if/else statement is met, the while loop will start again, and you'll be at guess two instead of restarting the game.
Hope I could help!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1