Python Forum
not able to complete a project - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: not able to complete a project (/thread-11223.html)

Pages: 1 2


not able to complete a project - olmin - Jun-29-2018

The project is that you have to create a program in which you pick a random number between 1 to 50 that the computer has to guess.
This challenge is in the book Python Programming For the Absolute Beginner, 3rd edition...Page 85
Please Help!! I'm new to python!


RE: not able to complete a project - j.crater - Jun-29-2018

What have you tried? Post your attempt at solving the task (in Python code tags) and errors if you get them (in error tags). You can find help on that here. We will help you correct mistakes and point you in right direction, but not write the code for you.


RE: not able to complete a project - olmin - Jun-29-2018

I'm not able to think of anything other than this:

num=int(input ('User\'s Input:\n'))
import random
num2=random.randint(1,50)
while num2 != num:
    if num2 > num:
        print ('Lower')
    if num2 < num:
        print ('Higher')
    else:
        print ('Comp guessed it right')
        break

input ('Press enter to exit')
I created an infinite loop.
I'm not able to think how am gonna make comp keep guessing until the number matches User's input

Thank you...


RE: not able to complete a project - ichabod801 - Jun-29-2018

In your example, you need to change num2 in your loop. If it is too low, you want to raise it; if it is too high, you want to lower it.

Traditionally, rather than asking the user the number and guessing the number you know, you would instead give a guess and repeatedly ask the user if it is too high or two low. Then adjust it as I was saying above.


RE: not able to complete a project - Zombie_Programming - Jun-29-2018

Try to read through your code and analyze what is going on. You declare a variable called "num" that gets a user's input for a number 1-50. You import the random library and declare another variable that is assigned a random number 1-50.
When you go into the while loop, you get the infinite loop because you check if the num2 != num, however when it checks if it is higher or lower than the user's number, you never change the num2 variable to something else. Here you should regenerate a new random number ( a new computer guess )
while num2 != num:
    if num2 > num:
        print('Lower')
        num2 = random.randint(1,50)
    elif num2 < num:
        print('Higher')
        num2 = random.randint(1,50)
    else:
        print('Computer guessed correctly')
        break
You can also save the computers original guess and use that so that it progressively gets closer and closer to the correct answer. My code continuously generates a new number regardless of how close you are to the correct number. I'll leave you to figure that out on your own.


RE: not able to complete a project - volcano63 - Jun-29-2018

Why are you using random and not binary search?

(Jun-29-2018, 04:00 PM)Zombie_Programming Wrote:
while num2 != num:
....
    else:
        print('Computer guessed correctly')
        break

You can do without the else clause - and print the message after the loop


RE: not able to complete a project - Zombie_Programming - Jun-29-2018

(Jun-29-2018, 07:11 PM)volcano63 Wrote: Why are you using random and not binary search? You can do without the else clause - and print the message after the loop

I figured I'd set it up for him so that he could use that as a basis to have the computer generate a new random number based on if the guess was higher or lower. So if the comps. guess was higher, it could generate a new number like so num2 = random.randint(1, lastGuess) But yes it could be simplified to just
# Check if computer's guess is right, if not gen. new #
while num2 != num:
    random.randint(1,50)
# Print computer is right
print("Computer Guessed Correctly!")
You have to make sure that the computer keeps 'Guessing' till the numbers are equal. If you don't do that, you'll throw yourself into an infinite loop.


RE: not able to complete a project - olmin - Jun-30-2018

With the help of the above discussion, I created
the program, it does look good ...but the computer takes very long time to guess user's given no.
is it possible to reduced it further more??
if there is..Please do help!
num=int(input ('User\'s Input:\n'))

import random

num2=random.randint(1,50)

if num > 50:
    print ('\aError! Choose a number between 1 and 50')

else:
    while num2 != num:
        if num2 > num:
            print(' ',num2,'Lower')
            num2 = random.randint(1,num2)
        elif num2 < num:
            print(' ', num2,'Higher')
            num2 = random.randint(num2,50)
        else:
            break

    print('Computer guessed correctly. It was', num2,"isn't it.")

        
input ('Press enter to exit')
Thank you guys!!


RE: not able to complete a project - j.crater - Jun-30-2018

To me it doesn't seem like it takes long at all.
Anyhow, you can optimize for minimum number of guess attempts by using "binary search" algorithm. It is pretty simple and you will find numerous sources on it online.


RE: not able to complete a project - volcano63 - Jun-30-2018

You are not really narrowing guess range - at least, you don't do it consistently. Here is how you should do it

guess = random.randint(1, 50)
high = 50
low = 1
while guess != num:
    if guess > num:
        print(' ', guess, 'Lower')
        high = guess - 1
    else:
        print(' ', guess, 'Higher')
        low = guess + 1
    guess = random.randint(low, high)
If you guess at the loop end equals to the entered number - the while test will end the loop, and if the guess is wrong and not bigger than the number - then you don't need to check if it's smaller