Not breaking correctly

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 970 Views - Last Post: 06 December 2012 - 06:45 PM Rate Topic: -----

#1 flebber  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 05-December 12

Not breaking correctly

Posted 05 December 2012 - 06:04 AM

I am reading the wikibook the non programmers guide to python.

In chapter 5 I have created the guessing game. simple game 3 guesses or its over and it prints a message.

Even if my game resolves that they win the failed message still shows. Why is my code not exiting correctly?


guess = 0
number = 78
count = 0

while count < 3:
    count += 1
    guess = input("Time for a guess: ")
    if guess > number:
        print "Oops to high"
    elif guess < number:
        print "too low try again !"
    else: 
        print "Just Right"
                
print "that must have been too hard"


Is This A Good Question/Topic? 0
  • +

Replies To: Not breaking correctly

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: Not breaking correctly

Posted 05 December 2012 - 09:28 AM

You should check out break statements.

guess = 0
number = 78
count = 0

success = False
while count < 3:
    count += 1
    guess = input("Time for a guess: ")
    if guess > number:
        print "Oops to high"
    elif guess < number:
        print "too low try again !"
    else: 
        print "Just Right"
        #the guess was correct so there is no point in keeping guessing
        success = True
        break
    
if not success:
    print "that must have been too hard"
else:
    print "well done"



Was This Post Helpful? 1
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7301
  • View blog
  • Posts: 12,158
  • Joined: 19-March 11

Re: Not breaking correctly

Posted 05 December 2012 - 10:04 AM

Here's a tidy little recursive approach. Notice how much easier this is to read.

>>> def get_guess(target, guesses):
...     if guesses >=3:
...             return "Too  many tries"
...     g = int(raw_input("Make a guess"))
...     if g == target:
...             return("Correct!")
...     return get_guess(target, guesses+1)


>>> def play_game(target):
...     return get_guess(target, 0)




Was This Post Helpful? 2
  • +
  • -

#4 Hqtitan  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 51
  • Joined: 28-August 10

Re: Not breaking correctly

Posted 05 December 2012 - 12:54 PM

Rather than using a break statement in your loop, have the loop check two conditions. You can check if the counter is less than three so that it only runs three times, while also checking if they guessed right. Set a variable that will equal false when they guess correctly, and it will end the while loop. And then only run that last print statement if they didn't guess correctly.

It could look something like this:

guess = 0
number = 78
count = 0

incorrect = True
 
while count < 3 and incorrect:
    count += 1
    guess = input("Time for a guess: ")

    if guess > number:
        print "Oops to high"
    elif guess < number:
        print "too low try again !"
    else:
        print "Just Right"
        incorrect = False

if incorrect:
    print "that must have been too hard"


Was This Post Helpful? 0
  • +
  • -

#5 flebber  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 05-December 12

Re: Not breaking correctly

Posted 05 December 2012 - 12:57 PM

@darek9576

I did try break, but they resulted the same for me as I did not include a separate success option as you had. Break put me from the inside loop to the outer.

@jon.kiparsky

I understand the flow of your function but not experienced enough yet to get how it is recursive.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7301
  • View blog
  • Posts: 12,158
  • Joined: 19-March 11

Re: Not breaking correctly

Posted 05 December 2012 - 01:07 PM

View Postflebber, on 05 December 2012 - 02:57 PM, said:

@jon.kiparsky

I understand the flow of your function but not experienced enough yet to get how it is recursive.


That's how it loops.

 return get_guess(target, guesses+1)


If the guess was no good, call this again and return the result.
This is actually a nice use of recursion to solve a frequent problem. This sort of problem - and the similar "Get a good answer or try again" - is usually an annoying functionality to implement in standard loops, but doing it as a recursion communicates the intent very nicely. (at least, I think it's nice)

This post has been edited by jon.kiparsky: 05 December 2012 - 01:10 PM

Was This Post Helpful? 2
  • +
  • -

#7 Nallo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 162
  • View blog
  • Posts: 248
  • Joined: 19-July 09

Re: Not breaking correctly

Posted 05 December 2012 - 03:23 PM

View Postflebber, on 05 December 2012 - 07:57 PM, said:

@darek9576

I did try break, but they resulted the same for me as I did not include a separate success option as you had. Break put me from the inside loop to the outer.


Well, darek did not provide the whole syntax of the while - break - else construct. After the while block one can put an optional else block that only gets executed when the loop hasn't been left by a break:

guess = 0
number = 78
count = 0

while count < 3:
    count += 1
    guess = int(raw_input("Time for a guess: "))
    if guess > number:
        print "Oops to high"
    elif guess < number:
        print "too low try again !"
    else: 
        print "Just Right"
        break
#else block will only be executed if loop is not exited via a break statement
else:
    print "that must have been too hard"



So no need for a success flag.
Was This Post Helpful? 2
  • +
  • -

#8 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: Not breaking correctly

Posted 06 December 2012 - 02:45 AM

Never knew this kind of stuff would work. Java background in ma way i guess.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7301
  • View blog
  • Posts: 12,158
  • Joined: 19-March 11

Re: Not breaking correctly

Posted 06 December 2012 - 06:23 AM

View PostNallo, on 05 December 2012 - 05:23 PM, said:

Well, darek did not provide the whole syntax of the while - break - else construct. After the while block one can put an optional else block that only gets executed when the loop hasn't been left by a break:



darek9576 said:

Never knew this kind of stuff would work. Java background in ma way i guess.


I don't know about anyone else, but I never think of the else clause on the while because "else" is pretty much completely the wrong word there. "else" simply doesn't mean "unless the loop has exited normally" to me, so it never comes to mind there.

However, it might also be because I generally don't use while loops in python, since there always seems to be a better construction. (like the recursive one in this case)
Was This Post Helpful? 1
  • +
  • -

#10 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: Not breaking correctly

Posted 06 December 2012 - 06:31 AM

I agree, but it just shows that every language has some nice subtle differences.
Was This Post Helpful? 0
  • +
  • -

#11 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 07-October 12

Re: Not breaking correctly

Posted 06 December 2012 - 08:07 AM

The question seems quite confusing, because you don't really need a break function to stop running the program. Once the count reaches 4, it will return False therefore the while loop will break automatically. Unless if his statement return always True, than it would be necessary to add a break function to stop the program.

Oh yes, you had a mistake in Line: 9, where user input a number, but it will return as a string. You need to make sure to convert it back to integer or also you will get a TypeError, basically means that you cannot compare a string to integer.

guess, number, count = 0, 78, 0

while (True):
    guess = int(input("Time for a guess: "))
    count += 1
    if (count != 3):
        if (guess > number):
            print ("Oops to high")

        elif (guess < number):
            print ("too low try again !")
            continue

        else:
            print ("Just Right")
            break

    elif (count == 3):
        print ("that must have been too hard")
        break



In addition, you don't really need a break function unless if you have an infinite loop as I did in this example. I will take an example from your code:

guess, number, count = 0, 78, 0
while (count < 4):
    count += 1
    if (count != 4):
        guess = int(input("Time for a guess: "))
        if (guess > number):
            print ("Oops to high")
        elif (guess < number):
            print ("too low try again !")
        else:
            print ("Just Right")
    elif (count == 4):
        print ("that must have been too hard")



Your count variable, already started the loop if the initial value count = 1, as you can see I didn't needed to use a break function, since when the counter reaches 4, than it will automatically return as False, since 3 is not greater than 4. Hopefully this did help you a lot.
Was This Post Helpful? 1
  • +
  • -

#12 flebber  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 05-December 12

Re: Not breaking correctly

Posted 06 December 2012 - 12:20 PM

@chan_06

I thought there were 2 input functions

Input - which takes numbers
raw_input - which takes any char type.

But you are converting an int to an int with the below statement aren't you?

Int(input())
Was This Post Helpful? 0
  • +
  • -

#13 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: Not breaking correctly

Posted 06 December 2012 - 12:30 PM

python 3 vs python 2.x
Was This Post Helpful? 1
  • +
  • -

#14 Nallo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 162
  • View blog
  • Posts: 248
  • Joined: 19-July 09

Re: Not breaking correctly

Posted 06 December 2012 - 12:41 PM

View Postdarek9576, on 06 December 2012 - 07:30 PM, said:

python 3 vs python 2.x


Exactly. Flebbers original code clearly is Python 2.x. So int(raw_input()) is approriate.

input in Python 2.x is equivalent to eval(raw_input()) and you don't want to use that. It opens a security hole.
Was This Post Helpful? 1
  • +
  • -

#15 flebber  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 05-December 12

Re: Not breaking correctly

Posted 06 December 2012 - 12:43 PM

View Postdarek9576, on 06 December 2012 - 12:30 PM, said:

python 3 vs python 2.x


Ah yes

CS py2 py3 diferences

http://docs.python.o...atsnew/3.0.html
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2