Hi,

I'm pretty new to python, just started 2 weeks ago xD, and new to forum too, I tried to search to see if there were any other question that was similar to this but couldn't find one. Therefore, I apologize if there's already another thread with the similar problem.

Anyway, my problem is basically I got the script to kind of work. It will ask me to input a starting number and ending number, but I just can't seem to eliminate the task or rather append to the starting or ending number if the guessed number should go higher or lower.

def guessing():
    import random
    tries = 1
    start = int(input("Input starting number"))
    end = int(input("Input ending number"))
    init_guess = random.randint(start, end)
    print(init_guess)
    answ = input("Is this the right number?")
    while answ != "yes":
        right = input("Is it higher or lower?")
        if right == 'higher':
            guess = random.randint(init_guess, end)
            print(guess)
            guess = init_guess
            answ = input("Is this the right number?")
            tries += 1
        elif right == 'lower':
            guess = random.randint(start, init_guess)
            print(guess)
            answ = input("Is this the right number?")
            guess = end_guess
            tries += 1
        else:
            print("Invalid input, please only use higher or lower as answer")
    print("I got it right, thank you for playing")
    return guessing

The thing is I don't think I should be using random.randint, does anyone have any suggestion? I tried to create a variable to append to the guessed number but I keep failing. It kind of work but the result would be if I put higher it would indeed go higher yet if I put lower it would go to a number that was lowered than the already guessed number. X_X Please help and point out if I made any unnecessary line of code, thank you. btw this problem was a challenge by a book I was reading but they didn't put any answer to it.

tested on python 2.7.2

import random

def guessing():
   start = int(raw_input('enter the starting number\n'))
   stop = int(raw_input('enter the ending number\n'))
   while start < stop:
      guess = random.randint(start, stop)
      answer = ''
      while answer not in ('higher', 'lower', 'equal'):
         answer = raw_input('Is it higher, lower, or equal to %s\n'%guess)
      if answer == 'higher':
         start = guess+1
      elif answer == 'lower':
         stop = guess -1
      else:
         break
   else:
      guess = start
   print ('The answer is: %s, thank you for playing'%guess)
   return guess

guessing()

instead of random.randint

using

guess = int((start+stop)/2)

reduces the amount of average guesses

thanks so much, I think I kind of get it. I'm pretty new to this so I haven't gotten to the raw_input part, would you mind explaining what that does? thank you :D

it waits for the user to enter some text and press the enter key. It will work with IDLE and Console apps but not with programs that suppress the console window (ie .pyw and compiled wxpython apps that hide the console)

perhaps it is better if you use

int(raw_input:)

than

int(input:)

input already returns the integer value, there's no need for an int in front of the input, but if you want to use raw_input, and still use the value as an integer, you must use int(raw_input).
From the latest versions of python, 2.5->, input was depreciated and it is better to use raw_input, and for values not strings, int(raw_input).
But still, input is a good way of getting the value, but if a string is enter in the input function, there will be raised an error.

File "<string>", line 1, in <module>
NameError: name 's' is not defined

thank you very much, I think I understand it better now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.