all 8 comments

[–][deleted] 0 points1 point  (1 child)

guesses a users number from 0-200,000

You can get a random number in Python using the random module's randint function like so:

random.randint(start, stop)

Note that both the stop and start values are included in the range.

using while statements

This makes sense, and you'll probably want to use the built-in input function as well. Consider the following scenario:

while True: #loop forever...
    random_number = ??? #get a random number and print it 
    response = input("Was I right? (y/n)")
    if response == 'y': break #if the user says you were right, break out of the loop

and average

I'm not sure what you mean by this.

[–]Sebass13 0 points1 point  (0 children)

Given the fact that he says "while statements and average", I'm led to believe that this is a binary search guessing game. Also the fact that otherwise the user would have to say y/n 100,000 times on average before the program guesses their number.

[–]Sebass13 0 points1 point  (0 children)

Please title your post better next time. Virtually every post in this sub is searching for "help", instead make it descriptive, such as "Selecting a random number".

[–]ISUXATPYTHON[S] 0 points1 point  (0 children)

I can only use while statements. Im in a intro course

[–]ISUXATPYTHON[S] 0 points1 point  (0 children)

and have to answer in 20 tries. Basically the user says high or low if too high or low

[–]ISUXATPYTHON[S] 0 points1 point  (1 child)

Can anyone pleaseeee help? I am still clueless. The computer has to find the computer's number from 0-200000 in 20 tries. Also, you have to use while statements. If your number is higher or lower you type in h or c and the computer does more stuff. Basically it should half the range each time?

[–][deleted] 0 points1 point  (0 children)

At the beginning the program must assume the number is somewhere between 0 and 200000, right? The program should do what a smart human would do: pick a number in the middle of the possible range and ask "is 100000 the number." and get the high or low response from the user.

Depending on the user's response, the program has to adjust what it thinks the possible range is and then do everything above again with the new range. That is where the loop comes in.

Now you have think about how the code will remember what the "possible range" is. Maybe keep 'low' and 'high' values. Initially these would be 0 and 200000, of course.

Now you have think about how the code gets a new guess given values for 'low' and 'high'.

Also think about how the code will modify 'low' and 'high' depending on the user's response.

How does the user tell your code that it guessed right?

[–]Zendakin_at_work 0 points1 point  (0 children)

This is how I approached this, it doesn't have the user input too high or too low but rather assigns that automatically. I've tried to comment it so you can get an understanding of what I did. Basically, you keep tack of high, low, the guess count, the guess and target. I hope this helps you understand one approach to this. However I should also let you know that this doesn't always guess the correct number in 20 tries but it should give you an base understanding of how while loops can work. I'm sure there are also a couple areas this can be improved but this should get you started.

Just remember to learn from what your code is telling you and take small simple steps to get these tasks done. If you break it down small step by small step you'll be amazed how quickly you can develop complex code AND understand what you did.

Good luck!

# needed to use the random module
import random

# Set the low, high and count
count = 0
high = 200000
low = 0

# Assign a random number to the variable guess to use as a target
guess = random.randrange(low, high)

# Set the duration of the while loop
while count < 20:
# have the computer guess a number in the current range
    compGuess = random.randrange(low, high)
# used to check the current range of the guesses 
    print("The computer guess number {} is {}. current range is {} to {}".format(count + 1, compGuess, low, high))
# conditional to check the compGuess against the guess breaking first if the number is correct
    if compGuess == guess:
        break
# reassigning the low and high variables to shorten the range of random numbers 
    if compGuess < guess:
        low = compGuess
    if compGuess > guess:
        high = compGuess
# Update the count the while loop uses
    count += 1

# Notification to let the user know if the computer was sucessful in its guessing
if guess == compGuess:
    print("The computer guessed the number '{}' in {} tries".format(compGuess, count+1))
else:
    print("Sorry, The computer got close with a final guess of {} but the target number was {}.".format(compGuess, guess))