0

I am trying to write a piece of code that prompts the user to input a number, and the loop will only stop if the input number matches the random generated number. The random number should be in within the range the user input.

import random
randNumber = random.randint(min, max)

min = int(input("Choose a minimum number range: "))
max = int(input("Choose a maximum number range: "))

while True:

  if guess > randNumber:
    print("Too big, try again")

  if guess < randNumber:
    print("Too small, try again")

  if guess == randNumber:
    print("BINGO!")
    break

The system keeps telling me "tuple object not callable" for my 2nd line.

1
  • You have randNumber = random.randint(min, max) before you even define min and max; why would you expect that to work? (even ignoring the fact that those are reserved words) Commented Nov 16, 2020 at 17:48

1 Answer 1

1

Two problems. min and max are reserved top-level functions in Python, so you're actually passing a function object at the top. (A common practice when you want to use reserved names is to add an underscore, e.g. min_ and max_.)

The other problem is randInt must come after you ask for user input (e.g. below a max_ = int(input(... line, as min_ and max_ are not defined.

Sign up to request clarification or add additional context in comments.

5 Comments

Only a minor information: There is no need to add underscores to min and max if the randInt comes after the inputs. Because then, the built-in functions will simply be overwritten. But yes, it is still a good idea to rename them, of course!
Thank you so much for the quick response, now I understand a lot clearer than before. However, I renamed them into min_ and max_, and moved the two lines (min_ = int(...) to before import, it still shows the same error to the same line. Does it have to do with me using Google Console on Chrome to run the code?
@AliceGong The line also has it's inputs changed? e.g. randint(min, max) to randint(min_, max_)? If so, I could only guess Google Console is caching previous inputs and that's causing you problems? Feel free to paste the new code here.
@Honno here is the revised code: min_ = int(input("Choose a minimum number range: ")) max_ = int(input("Choose a maximum number range: ")) import random randNumber = random.randint(min_, max_) Thank you so much for your kind help
@AliceGong That looks good to me! I tested it locally, it works. I'm not familiar with Google Console, so I recommend search around on how to "refresh" your instance, or try running this code on another Python interpreter program (e.g. IDLE that comes packaged with Python downloads).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.