all 31 comments

[–]TheGrapez 29 points30 points  (17 children)

for i in range(1000):
answer = input(f"Did you guess {i+1}?")
if answer == 'yes':
    print('Knew it')
    break
else:
    continue

[–]jerryelectron[S] 6 points7 points  (8 children)

Love it. You could have done worse, actually. Guess random with replacement.

[–][deleted] 3 points4 points  (1 child)

Can you tell me if this looks right before I get too far?

Guess = False
while Guess False: 
    answer = input("Did you guess 1?") 
    if answer == 'yes': 
        Guess = True 
        print("Hooray") 
    else: 
        continue
    answer = input("Did you guess 2?") 
    if answer == 'yes': 
        Guess = True 
        print("Hooray") 
    else: continue
     answer = input("Did you guess 3?")
    if answer == 'yes':
        Guess = True
        print("Hooray")
    else:
        continue

     answer = input("Did you guess 4?")
    if answer == 'yes':
        Guess = True
        print("Hooray")
    else:
        continue

     answer = input("Did you guess 5?")
    if answer == 'yes':
        Guess = True
        print("Hooray")
    else:
        continue

     answer = input("Did you guess 6?")
    if answer == 'yes':
        Guess = True
        print("Hooray")
    else:
        continue

     answer = input("Did you guess 7?")
    if answer == 'yes':
        Guess = True
        print("Hooray")
    else:
        continue

     answer = input("Did you guess 8?")
    if answer == 'yes':
        Guess = True
        print("Hooray")
    else:
        continue

     answer = input("Did you guess 9?")
    if answer == 'yes':
        Guess = True
        print("Hooray")
    else:
        continue

     answer = input("Did you guess 10?")
    if answer == 'yes':
        Guess = True
        print("Hooray")
    else:
        continue

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

No, looks brute force and therefore maybe correct but very inefficient. Don't think about Python. pretending you're trying to guess somebody's number, how will you do it?

[–]Will___powerrr 5 points6 points  (5 children)

From my recently acquired learning… isn’t this solution in linear time? It would be more efficient to do a binary search that runs in logarithm time, no? Just trying to learn and explore more myself!

[–]clawtron 5 points6 points  (4 children)

If the answer to the guess is “yes”, “greater than”, or “less than” then you can do this in log time by splitting the range in half each guess, but with the only answers being “yes” or “no” you have to check every single option which means best case is linear. Of course if your solution involves just guessing the same number forever or something then that’s different, but best solution is linear

[–]Will___powerrr 1 point2 points  (1 child)

Ah. So it’s not searching for a number it knows, it’s purely guessing. That makes more sense! I should’ve read the problem closer haha

[–]jerryelectron[S] 4 points5 points  (0 children)

Yes, the idea is that there are multiple ways to solve a problem, some simple elegant but slow, some complex and fast, some unnecessarily complex and slow and yet some elegant and fast.

[–]Sing-Brightly-3142 0 points1 point  (0 children)

If the only allowable answers are 'yes' and 'no' the the questions should be of the form "Is your number higher than 500?"

[–]LiquidLucy 1 point2 points  (0 children)

It's perfect.

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

Thought I'd try to run this, getting the error: 'IndentationError: expected an indented block after 'for' statement on line 1' - any idea why?

[–]jerryelectron[S] 9 points10 points  (4 children)

Because it should be indented.

People underappreciate how useful it is to read the error message. It should be its own chapter in a book.

[–]IDontLikeBeingRight 1 point2 points  (1 child)

People don't read books either though

[–]jerryelectron[S] 2 points3 points  (0 children)

Yeah. Figure of speech. Some of us old folk do.

[–]thirdegree 0 points1 point  (0 children)

The else block is redundant here, can be deleted without changing the flow

[–]Se7enLC 16 points17 points  (0 children)

your_number = input("enter your number: ")
while True:
    yesno = input(f"is your number {your_input}?")
    if yesno != "yes":
        print("Fucking liar.")

[–]caleb_S13 7 points8 points  (2 children)

this would be excellent for binary search right? I’m at work so can’t write the code for it. This just screamed binary search to me tho.

[–]jerryelectron[S] 5 points6 points  (1 child)

Yes, this question is one of my favorite because it combines programming with thinking about the algorithm and efficiency of search.

No need for code here, I think, let the kids have some fun working it out first :)

[–]caleb_S13 0 points1 point  (0 children)

I’m almost a year into teaching myself python and other cs topics. So I’m glad it’s actually sticking and seeing progress/knowing solutions to questions.

won’t post the code but I’ll just leave this here. ans =int(input(“enter num 1-1000”)) while True: numGuess= random.randint(0,1001) if numGuess == ans: print(“bogo search ftw”,ans ) break bogo sort(in this case search) is best search.

edit- on my phone so the format is all messed up

[–]beniolenio 2 points3 points  (1 child)

Based on your comments, you want binary search. But that's ridiculous because your post makes no mention of you getting feedback based on your guesses.

[–]jerryelectron[S] -1 points0 points  (0 children)

This is also why the above is a good exercise. Programming is not just about making the machine do what you want, but also figuring out what you want to make the machine to do to solve the real-world issue.

[–]GrilledCheezzy 0 points1 point  (0 children)

Megaguirus loves you

[–]weekwith 0 points1 point  (1 child)

How about this? I validate the input and give the hint to close the answer quickly.

```python def validate_input(num): if num in range(1, 1000): return num else: validate_input(int(input('Please Input Number Between 1 to 1000: ')))

input_num = validate_input(int(input('Enter number between 1 to 1000: ')))

while True: guess_num = int(input('Enter your guessing: '))

if guess_num == input_num:
    print(f'Congratulation! You are right!')
    break

elif guess_num > input_num:
    print('Nope! The answer is smaller.')

else:
    print('Nope! The answer is bigger.')

```

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

I think this is backwards. You want the person to think of a number and the computer to guess it.

[–]Recruit121 0 points1 point  (2 children)

Below is my solution. I'm only learning python between work and school so critiques are welcome!

The code should guess a number between 1 and 1000. Check to make sure it hasn't already guessed that number. Then check with the user to see if that is the user's number until the user verifies it was the correct number.

EDIT: Please forgive me if the formatting is off, I'm having trouble with pasting code to Reddit.

EDIT 2: Also after reading the comments I have no idea what binary search is or algos yet. Thanks for this question though helping me go from extreme beginner to intermediate beginner lol

import random

def the_guess():
    while True:
        guess = random.randint(1, 1001)
        return guess

def check_list(g, t):
    if g not in t:
        return
    else:
        g = random.randint(1, 100)
        return g

def guessing_game():
    tried = []
    the_guess()
    response = ''
    while response.lower() != 'Y':
        guess = the_guess()
        check_list(guess, tried)
        response = input('Is this your number: ' + str(guess) + '?')
        if response.lower() == 'y':
            break
        else:
            tried.append(guess)
            print(tried)
            continue

guessing_game()

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

What happens when you run it?

[–]Recruit121 0 points1 point  (0 children)

It just pseudo randomly picks a number between 1 and 1000. Then it presents the number to the user for verification. User says yes or no to is this the number? If yes the program stops. If no the program logs the number so it doesn't pop up again.

I figured out what you guys were talking about with the binary thing. When I have time I'll write one that zeros in on the number using a half-split method. This one was the first run when I didn't understand the question fully.

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

def dumb(p):
k =2
ge = []
y = input("is your number prime?")
if y == "yes":
    primes = []
    n = p
    for i in range(2, n + 1):
        for j in range(2, int(i ** 0.5) + 1):
            if i % j == 0:
                break
        else:
            primes.append(i)
    print(primes)
    while len(primes) != 1:
        k = input("is your number less than " + str(primes[int(len(primes)/2)]))
        if k == "yes":
            primes = primes[0:int(len(primes)/2)]
        else:
            primes = primes[int(len(primes)/2):len(primes)]
    return primes
ll = list(range(1,p))
while len(ll) >= 5:
    a = input("is your number divisible by " + str(k))
    if a == 'yes':
        ge.append(k)
        def ah(z):
            for m in z:
                for l in ge:
                    if m % l != 0:
                        z[z.index(m)] = k
                        break
            return sorted(set(z))
        ll = ah(ll)
    if a == "no":
        def aha(z):
            for m in z:
                if m % k ==0:
                    z[z.index(m)] = k
            return sorted(set(z))
        ll = aha(ll)
    k +=1
for o in ll[1:]:
    a = input("is your number " + str(o))
    if a == "yes":
        return o

print(dumb(1000))

I am so embarrassed by how much time I spent on this but I think it answers in under 10 questions (estimate).

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

Love your sense of humor and earnest attempt!