Hi all!

Is my first post here and well, im very new with python (just aroun 10 days). I did the famous "Guess my number" game. (Python pick up a number and user must guess it).
All right, i have not problems with this. But now im trying in the other way.

I mean, user pick up a number between 1-20 (for example), if the number isnt between 1-20, python warns about this.

Python have 5 tries to guess the number, and user should tell: first, if the number given by python is the pick up number or not. If user says "No" but is Yes, python "Kick the user for cheater", otherwise, if user say Yes -Always- (ever if isnt the user´s number) python wins.

So, if user says "No" and really is No, python ask "lower or higher?".

Im trying to do this loop with little i know about python. Here is my code. (Im stuck):

# Guess My Number Inverted
# This time, user pick up the number
# and python must guess it.


number = int(input("pick a number between 1 and 100: "))
while number < 1 or number > 100:
    print("I told you a number between 1 - 100 bitch!!")
    print("Try again and this time dont fuck with me.")
    number = int(input("Escribe un número entre el 1 y el 100: "))

import random

tries = 0
guess = 0

input("Ok... Let´s finish with this shit.")

while tries < 6:
    guess = random.randint(1, 20)
    print("your number is", guess, "?")
    tries +=1
    response = input("\nYes/No: ")
    if response == "Yes":
        print("Perfect! i won!!")
        print("Get the fuck off of here, loser")
        input("Please, press -Enter- To get the fuck off of here, loser.")
        break
    elif response == "No":
        if guess == number:
            print("You fool!!! This is your number!!!!")
            print("Get the fuck off of here and comeback when you can play fair.")
            input("Press -Enter- to exit, cheater.")
            break
        elif guess != number:
            clue = input("Damn you!! lower or higher??: ")
            if clue == "lower":
                guess2 = random.randint(1, guess )
                tries +=1
                print("Your number is ", guess2, "?")
            elif clue == "higher":
                guess2 = random.randint(guess, 20)
                tries +=1
                print("Your number is ", guess2, "?")

if tries == 6 and guess2 != number:
    print("oooooh what´s wrong? i though i was the best...")
    print("Ok, you won, but you´ll not so lucky the next time.")
    input("press -Enter- to exit, winner.")

I think that my problem is with "guess2" and the refresh between lower and higher values of guess.

i dont know how do a loop and refresh the "guess2" between lower and higher :/.

If you could give me some clue, i thank you so much.

Thanks guys! ;)

Keep lower and higher bounds both in variables, which you update by user response for random, and call it only in one place in loop.

First of all, thanks so much for your fast answer pyTony.

I´ve tried follow your tip but i have some doubt:

Ok. "higher" and "lower" both in variables; i suposse that the code looks like this:

number = int(input("pick a number between 1 and 100: "))
while number < 1 or number > 100:
    print("I told you a number between 1 - 100 bitch!!")
    print("Try again and this time dont #### with me.")
    number = int(input("Escribe un número entre el 1 y el 100: "))
import random
tries = 0
guess = 0
higher = 0
lower = 0

Nice. but i dont understand how can i update the random. I mean, i understand how can i do this one time, but for example.

  • I pick up "12"

  • Python pick up a random 1-20 first time, for example, 18.

  • I say "No"

  • Python ask me if lower or higher.

  • i say then "lower".

  • python give me a number between 1, and Guess (because i told him "lower"). Ok, all good right now.

(but here is where im stuck)

  • If python tell me "10"

  • i say "No" and "higher"

  • But python give me a number between (10 , and 20, not 10, and the last number higher, in this case "18", i mean, i could give me 18 one more time, or 19 and 20.)

in my very first days with PY i could only learn "if, elif, else and while". Maybe my proposal exercise is beyond me right now? Can i achieve it only with these conditionals?

I´ll keep trying until i succeed but i wonder to know if it´s possible or not.

Thanks a lot again mate :)

How higher 0 in beginning, the number can be more than 0, can't it? And it can not be 0 so lower is too low also.

By the way need the computer use random numbers by your assignement? Using average of higher and lower bounds is more efficient.

Ok. i´ll keep trying and explore other ways to achieve it trying average of higher/lower.
I´ll keep moving in the theory

Thanks a lot again pyTony ;)!

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.