0

I am new to Python. I have a program that asks the user for an input, and then the program tries to randomly guess what the user inputed, but narrows its search range by asking if it was 'too high' or 'too low'.

How do I make it so that after the user inputs 'too high' or 'too low' it reruns the guessing function?

Also please look at my code for suggestions on improvement. Thanks!

import random

while True:
    inp = int(input("type in a number (between 0 and 100) \n> "))

    if inp > 0 and inp < 101:
        break
    else:
        print ('sorry try again')

def guessmachine(x):
    guesstries = 0
    guess = random.randint(0, 100)
    print ("my guess was %s" %guess)
    if guess != x:

        while True:

            guesstries += 1

            response = input('was my guess too high or too low? Tell the truth \n>')       
            if response == 'too high':
                guess = random.randint(0, guess)   #assigning new guess range to narrow down possibilities
                return guess
                break
                guessmachine(inp)  #how do I make it rerun guessmachine and guess all over again?

            elif response == 'too low':
                guess = random.randint(guess, 100) #assigning new guess range to narrow down possibilities
                return guess
                break
                guessmachine(inp) #how do I make it rerun guessmachine and guess all over again?

            else:
                print ('sorry didnt get that')

    else:
        print ("Found it! Your number was %s and it took me %s tries" %guess, guesstries)


guessmachine(inp)

updated:

import random

while True:
    inp = int(input("type in a number (between 0 and 100) \n> "))
    if inp > 0 and inp < 101:
        break
    else:
        print ('sorry try again')

def guessmachine(x, guess, guesstries):
    print ("my guess was %s" %guess)
    if guess != x:
        while True:
            response = input('was my guess too high or too low? Tell the truth \n>')       
            if response == 'too high':
                guess = random.randint(0, guess)
                guesstries += 1
                newguess = guess
                guessmachine(inp, newguess,guesstries)


            elif response == 'too low':
                guess = random.randint(guess, 101)
                guesstries += 1
                newguess = guess
                guessmachine(inp, newguess,guesstries)

            else:
                print ('sorry didnt get that')

    else:
        print ("Found it! Your number was %s and it took me %s tries" %guess, guesstries)


guessmachine(inp, random.randint(0, 100), 1)
R_C
  • 333
  • 2
  • 6
  • 17

2 Answers2

0

A simple way to do it is to make your guess machine to return whether the number is higher or lower (or equal) and put it in a while loop. In a few simple changes it would look like:

import random

MAX = 10

while True:
    inp = int(input("type in a number (between 0 and {}) \n> ".format(MAX)))

    if inp >= 0 and inp <= MAX:
        break
    else:
        print ('sorry try again')

def guessmachine(x, guess, guesstries = 0):

    print ("my guess was %s" %guess)
    if guess != x:
        while True:
            response = input('was my guess too high or too low? Tell the truth \n>')       
            if response in ('too high', 'too low'):
                return response
            else:
                print ('sorry didnt get that')

    else:
        print ("Found it! Your number was %s and it took me %s tries" %(guess, guesstries))
        return 'found'

guesses = 0
guess = random.randint(0,MAX)
while True:
    guesses += 1
    was = guessmachine(inp,guess,guesses)
    if was == 'too high':
        guess = random.randint(0,guess)
    elif was == 'too low':
        guess = random.randint(guess,MAX)
    elif was == 'found':
        break
DSLima90
  • 2,680
  • 1
  • 15
  • 23
  • Thank you for the code. I get wonky results. It repeats the same guess over and over again. Also, see my comment on Mr Geek's reply to see what I'm trying to solve. Thanks! – R_C May 24 '17 at 03:18
  • I just answered your question about how to recall the function in a loop dind't change your logic. Look at my answer in this code for a better code: https://stackoverflow.com/questions/43618861/bash-syntax-error-near-unexpected-token-newline-in-python-number-game/43619109#43619109 – DSLima90 May 24 '17 at 10:48
  • The code is exactly what you want, except for minor changes. – DSLima90 May 24 '17 at 10:52
0

With some improvement and corrections, here's the final work :

EDIT : I'am now using a max and a min, so that with each guess, I narrow down the random interval :

import random

while True:
    inp = int(input("type in a number (between 0 and 100) \n> "))
    if inp > 0 and inp < 101:
        break
    else:
        print ('sorry try again')

def guessmachine(x, guess, guesstries=1, mn=0, mx=100):
    print("my guess was %s" %guess)
    print('{',mn,mx,'}')
    if guess != x:
        while True:
            response = input('was my guess too high or too low? Tell the truth \n>')
            if response in ['too high','too low']:
                guesstries += 1
                if response == 'too high':
                    if guess<mx: mx=guess-1
                elif response == 'too low':
                    if guess>mn: mn=guess+1
                guess = random.randint(mn, mx)
                guessmachine(x, guess, guesstries, mn ,mx)
                break
            else:
                print ('sorry didnt get that')
    else:
        print ("Found it! Your number was %s and it took me %s tries" %(guess, guesstries))

guessmachine(inp, random.randint(0, 100))

Test Case I (random, 5 tries) :

type in a number (between 0 and 100)
> 47
my guess was 75
{ 0 100 } # first search in 0,100
was my guess too high or too low? Tell the truth
>too high
my guess was 13
{ 0 74 } # search in 0,74 since 75 is high, any number more than it is too
was my guess too high or too low? Tell the truth
>too low
my guess was 37
{ 14 74 } # search in 14,74 since 13 is low, any number less than it is too
was my guess too high or too low? Tell the truth
>too low
my guess was 55
{ 38 74 } # you got the idea ...
was my guess too high or too low? Tell the truth
>too high
my guess was 47
{ 38 54 }
Found it! Your number was 47 and it took me 5 tries

Test Case II (random, 6 tries) :

type in a number (between 0 and 100)
> 55
my guess was 74
{ 0 100 }
was my guess too high or too low? Tell the truth
>too high
my guess was 42
{ 0 74 }
was my guess too high or too low? Tell the truth
>too low
my guess was 72
{ 42 74 }
was my guess too high or too low? Tell the truth
>too high
my guess was 46
{ 42 72 }
was my guess too high or too low? Tell the truth
>too low
my guess was 56
{ 46 72 }
was my guess too high or too low? Tell the truth
>too high
my guess was 55
{ 46 56 }
Found it! Your number was 55 and it took me 6 tries
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
  • okay so I want to know how do I make it save the results of the guess it learned from. Example, if it guesses 10 and I say "it is too high" it may proceed to guess 2 the next time, which I will say 'too low', then it will guess 50 the time after that. How do I make it so that it stores that memory of 10 being too high and that it should not go above that? – R_C May 24 '17 at 02:45
  • Actually if it guesses 10 and say 'too high', it mustn't guess any number above 10. See the edit above. – DjaouadNM May 24 '17 at 09:14