Maker Pro
Maker Pro

Exchanging data variables?

I had never programmed python before but wanted to see an implementation done with nested loops, just because of belief that I/O and calculations should be kept separated, although you seem to have combined them with finesse. Anyway, consider this an example of an alternative approach. It works OK with Python 2.6.6 on CentOS 6.5 Linux.

Code:
msg1 = 'Please think of a number between 0 and 100!'
msg2 = 'Is your secret number'
msg3 = 'Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed correctly. '
msg4 = 'Sorry, I did not understand your input.'
msg5 = 'Game over. Your secret number was:'
mini = 0
maxi = 100

print msg1
while (1) :
    guess = (mini+maxi)/2
    while (1) :
        print msg2,"%d?" % guess
         choice = raw_input(msg3)
        if choice == 'l' or choice == 'h' or choice == 'c' :
            break
        else:
            print msg4
    if choice == 'l' :
        mini = guess
    elif choice == 'h' :
        maxi = guess
    else :
        print msg5, "%d" % guess
        break
 
I had never programmed python before but wanted to see an implementation done with nested loops, just because of belief that I/O and calculations should be kept separated, although you seem to have combined them with finesse. Anyway, consider this an example of an alternative approach. It works OK with Python 2.6.6 on CentOS 6.5 Linux.

Code:
msg1 = 'Please think of a number between 0 and 100!'
msg2 = 'Is your secret number'
msg3 = 'Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" to indicate I guessed correctly. '
msg4 = 'Sorry, I did not understand your input.'
msg5 = 'Game over. Your secret number was:'
mini = 0
maxi = 100

print msg1
while (1) :
    guess = (mini+maxi)/2
    while (1) :
        print msg2,"%d?" % guess
         choice = raw_input(msg3)
        if choice == 'l' or choice == 'h' or choice == 'c' :
            break
        else:
            print msg4
    if choice == 'l' :
        mini = guess
    elif choice == 'h' :
        maxi = guess
    else :
        print msg5, "%d" % guess
        break
I see what you've done.
It's an additional nested loop so that you don't have to recalculate what guess is if the user enters an invalid entry.
I certainly agree to your reason for this. If the calculation was much more complex, then recalculating when nothing has technically changed is pointless and wastes cycles and energy.
I particularly like how you store the strings as variables. It could easily allow you alter the program for a different locale by changing a few predetermined strings.
 
although you seem to have combined them with finess

Beginners luck more likely and lots of help, but thanks none the less!

I really like how you separated your statements from the logic processing - I can see what you meant in an earlier post. I just couldn't do it! I am still not comfortable with loops or nested loops - they seem simple at first, but I think its a matter of practice and familiarity.

Thanks for posting that code :)
 
Top