Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4620350
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:32:55+00:00 2026-05-22T02:32:55+00:00

I am learning python atm, and was doing an exercise from this site —

  • 0

I am learning python atm, and was doing an exercise from this site—

Instruct the user to pick an arbitrary number from 1 to 100 and proceed to guess it correctly within seven tries. After each guess, the user must tell whether their number is higher than, lower than, or equal to your guess.

the code I wrote, but did not match the solution was this–

import random
pick = int(input('number? '))
count = 0

while count <= 7:
    number = random.randint(1, 10)
    count += 1
    print('is the number ', number, '?')

if number <= pick:
    print('press enter if higher')
    higher = input()
elif number >= pick:
    print('press enter if lower')
    lower = input()
elif number == pick:
    print('good?')
    yes = input()
    break

print('end')

I couldn’t get this one right. When number ==pick, the loop did not end. Was it because of the random.randint that was mucking the elif number==pick ?

The solution given in the site was way different from the one I wrote. How could I have done this better?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:32:56+00:00Added an answer on May 22, 2026 at 2:32 am

    Well here’s a quick rewrite I did just based on the problem text you posted:

    import random
    input("Think of a number from 1 to 10, then press enter.  Don't tell me, I'll guess it.")
    low = 1
    high = 10
    done = False
    
    for guesses in range(7):
        number = random.randint(low, high)
        print("I'm guessing", number)
        happy = False
        while not happy:
            resp = input('Enter H if your number is higher, L if lower or E for equal: ')[0].upper()
            if resp == 'E':
                if guesses:
                    plural = 'es'
                else:
                    plural = ''
                print('Yay!  I got it in {} guess{}.'.format(guesses+1, plural))
                happy = True
                done = True
                break
            elif resp == 'H':
                happy = True
                low = number + 1
            elif resp == 'L':
                happy = True
                high = number -1
            else:
                print("That wasn't a valid response.")
        if done:
            break
    
    print('end')
    

    What I did differently:

    • The program doesn’t get your number as input (it’s trying to guess it after all).
    • I set low & high to the initial range and set up a done flag as I’ll need to break out of two levels of loop on success
    • next I guess based on low-high range.
    • ask for H, L or E to indicate where my guess falls
    • modify the range based on what user tells me

    The main difference is that this code somewhat intelligently guesses the number. (Though with 7 guesses it could still fail.) A more intelligent algorithm would be to guess midpoints each time rather than randomly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was just reading through Learning Python by Mark Lutz and came across this
I learning Python (coming from a dotnet background) and developing an app which interacts
learning python, coming from a php background. Keeping it short. Is there a way
After learning python (cpython) and using this page to search for tkinter widgets' options,
Possible Duplicate: Learning Python: If condition executing all the time This Python code is
I am learning Python for a class now, and we just covered tuples as
I've been learning python for a while now with some success. I even managed
I recently started learning Python and I was rather surprised to find a 1000
WARNING: I have been learning Python for all of 10 minutes so apologies for
I'm learning Python now because of the Django framework. I have been a Perl

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.