Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python help thread?
#19
Can someone quickly double check this:

Assumptions: Only valid number inputs for the range. That is, no floating point values, letters and the like.

Result that comes out of this code should be exactly the same as this:

Code:
Do you want to play ('y' for yes)? y
Enter the low limit on the number: 200
Enter the high limit on the number: 500
Enter the maximum number of guesses: 4
I will never need more than 9 guesses.
I guess 350 am I low, correct, or high? high
I guess 274 am I low, correct, or high? high
I guess 236 am I low, correct, or high? low
I guess 255 am I low, correct, or high? high
You win, I loose!
Do you want to play ('y' for yes)? y
Enter the low limit on the number: 1
Enter the high limit on the number: 10
Enter the maximum number of guesses: 7
I will never need more than 4 guesses.
I guess 5 am I low, correct, or high? high
I guess 2 am I low, correct, or high? high
I guess 1 am I low, correct, or high? high
No way, you cheated!
Do you want to play ('y' for yes)? y
Enter the low limit on the number: 200
Enter the high limit on the number: 300
Enter the maximum number of guesses: 7
I will never need more than 7 guesses.
I guess 250 am I low, correct, or high? high
I guess 224 am I low, correct, or high? low
I guess 237 am I low, correct, or high? high
I guess 230 am I low, correct, or high? low
I guess 233 am I low, correct, or high? high
I guess 231 am I low, correct, or high? correct
I win!
Do you want to play ('y' for yes)? y
Enter the low limit on the number: 200
Enter the high limit on the number: 230
Enter the maximum number of guesses: 5
I will never need more than 5 guesses.
I guess 215 am I low, correct, or high? gihd
I don't understand gihd
I guess 215 am I low, correct, or high? high
I guess 207 am I low, correct, or high? loow
I don't understand loow
I guess 207 am I low, correct, or high? low
I guess 211 am I low, correct, or high? hihg
I don't understand hihg
I guess 211 am I low, correct, or high? high
I guess 209 am I low, correct, or high? low
I guess 210 am I low, correct, or high? correct
I win!
Do you want to play ('y' for yes)? n

Code:
import random

def high_low(low_range, high_range, NumberGuess):
    guess = 0
    flag = 0
    RandNum = random.randint(low_range, high_range)
    while flag == 0:
        while guess < NumberGuess and flag == 0:
            Answer = raw_input('I guess '+ str(RandNum)+ ' am I low, correct, or high? ')
            if Answer in ('c','C','correct','Correct'):
                print 'I win!'
                flag = 1
            elif Answer in ('h','H','High','high'):
                high_range = RandNum-1
                Cheat(low_range, high_range+1)
                RandNum = random.randint(low_range, high_range)
                guess = guess + 1
            elif Answer in ('l','L','Low','low'):
                low_range = RandNum+1
                Cheat(low_range-1, high_range)
                RandNum = random.randint(low_range, high_range)
                guess = guess + 1
            else:
                print 'I do not understand', Answer+'.'
        if guess == NumberGuess and flag == 0:
            print 'You win, I lose!'
            flag = 1
    Start_game()

def Start_game():
    Start = raw_input('Do you want to play ("y" for yes)? ')
    if Start in ('y','yes','Y','Yes'):
        low_range = int(raw_input('Enter the low limit on the number: '))
        high_range = int(raw_input('Enter the high limit on the number: '))
        while low_range > high_range:
            print 'How do you have a range of the low limit being higher than',
            print 'the high limit?'
            print 'Please give me a valid range.'
            low_range = int(raw_input('Enter the low limit on the number: '))
            high_range = int(raw_input('Enter the high limit on the number: '))
        while low_range == high_range:
            print 'Uh, that is not a range.  Please give a range so we can',
            print 'start playing!'
            low_range = int(raw_input('Enter the low limit on the number: '))
            high_range = int(raw_input('Enter the high limit on the number: '))
        NumberGuess = int(raw_input('Enter the maximum number of guesses: '))
        if NumberGuess < 0:
            print 'That was a negative number of guesses. I am sure you meant',
            print 'the opposite so that is', NumberGuess*(-1), 'guesses.'
            NumberGuess = NumberGuess*-1
        while NumberGuess == 0:
            print "I don't get any guesses? :("
            NumberGuess = int(raw_input('Enter the maximum number of guesses: '))
        if NumberGuess == 1:
            print 'I will never need more than', NumberGuess, 'guess.'
        else:
            print 'I will never need more than', NumberGuess, 'guesses.'
        high_low(low_range, high_range, NumberGuess)
    else:
        exit()

def Cheat(low_range, high_range):
    if low_range >= high_range:
        print 'No way! You cheated!'
        Start_game()
        
Start_game()

I did add some of my own stuff in there for additional dummy proofing. But it shouldn't change the output result should you enter in the same stuff as the required input.
Reply


Messages In This Thread
Python help thread? - by Alley - 2011-02-12, 10:05 PM
Python help thread? - by Derosis - 2011-02-12, 10:09 PM
Python help thread? - by Erebus - 2011-02-12, 10:18 PM
Python help thread? - by XTOTHEL - 2011-02-12, 10:23 PM
Python help thread? - by Alley - 2011-02-12, 10:25 PM
Python help thread? - by XTOTHEL - 2011-02-12, 10:28 PM
Python help thread? - by Alley - 2011-02-12, 10:30 PM
Python help thread? - by XTOTHEL - 2011-02-12, 10:46 PM
Python help thread? - by Alley - 2011-02-12, 11:04 PM
Python help thread? - by XTOTHEL - 2011-02-12, 11:19 PM
Python help thread? - by Eos - 2011-02-12, 11:45 PM
Python help thread? - by XTOTHEL - 2011-02-12, 11:52 PM
Python help thread? - by Eos - 2011-02-13, 12:21 AM
Python help thread? - by XTOTHEL - 2011-02-13, 12:27 AM
Python help thread? - by Alley - 2011-02-13, 01:34 AM
Python help thread? - by Eos - 2011-02-13, 09:13 AM
Python help thread? - by ThatWasMyKil - 2011-02-16, 12:16 AM
Python help thread? - by AngelSL - 2011-03-01, 10:49 AM
Python help thread? - by EarthAdept2 - 2011-03-04, 04:53 PM
Python help thread? - by JPTheMonkey - 2011-03-04, 06:05 PM
Python help thread? - by EarthAdept2 - 2011-03-04, 06:09 PM
Python help thread? - by Fiel - 2011-03-05, 08:22 AM
Python help thread? - by JPTheMonkey - 2011-03-05, 09:38 PM
Python help thread? - by Fiel - 2011-03-06, 03:35 AM
Python help thread? - by EarthAdept2 - 2011-03-14, 02:19 AM
Python help thread? - by XTOTHEL - 2011-03-14, 02:30 AM
Python help thread? - by EarthAdept2 - 2011-03-14, 03:30 AM
Python help thread? - by JPTheMonkey - 2011-03-30, 07:44 PM
Python help thread? - by Fiel - 2011-03-30, 07:47 PM
Python help thread? - by JPTheMonkey - 2011-03-30, 10:16 PM
Python help thread? - by Alley - 2011-03-30, 11:58 PM
Python help thread? - by Fiel - 2011-04-01, 11:45 PM
Python help thread? - by Nikkey - 2011-04-02, 12:51 PM
Python help thread? - by JPTheMonkey - 2011-04-06, 11:46 PM
Python help thread? - by Alley - 2011-04-12, 10:58 PM
Python help thread? - by XTOTHEL - 2011-04-12, 11:13 PM
Python help thread? - by JPTheMonkey - 2011-04-12, 11:18 PM
Python help thread? - by Fiel - 2011-04-12, 11:20 PM
Python help thread? - by Alley - 2011-04-13, 12:50 AM
Python help thread? - by Fiel - 2011-04-13, 01:08 AM
Python help thread? - by Alley - 2011-04-13, 01:25 AM
Python help thread? - by Fiel - 2011-04-13, 01:31 AM
Python help thread? - by Alley - 2011-04-13, 01:34 AM
Python help thread? - by Fiel - 2011-04-13, 01:37 AM
Python help thread? - by Alley - 2011-04-13, 01:44 AM
Python help thread? - by Fiel - 2011-04-13, 01:59 AM
Python help thread? - by Alley - 2011-04-13, 02:02 AM
Python help thread? - by Fiel - 2011-04-13, 02:05 AM
Python help thread? - by Alley - 2011-04-13, 02:07 AM
Python help thread? - by JPTheMonkey - 2011-04-13, 02:27 AM
Python help thread? - by Alley - 2011-04-13, 10:17 AM
Python help thread? - by JPTheMonkey - 2011-04-13, 04:17 PM
Python help thread? - by EarthAdept2 - 2011-04-18, 07:01 PM

Forum Jump:


Users browsing this thread: