MITx 6.00.1x Python Notes: Gussing Secret Number

Problem
In this problem, we will create a program that guesses a secret number!
The program works as follows: the user thinks of an integer between 0 (inclusive) and 100 (not inclusive). The computer makes guesses, and you give it input - is its guess too high (input 'h') or too low (input 'l')? Using bisection search, the computer will guess the user's secret number (input 'c')!

Python code

3 条评论:

  1. sorry, it does not work if you do not consider int instead of float division in python 3. This works:

    # Paste your code into this box
    print ('Please think of a number between 0 and 100!')
    low = 0
    high = 100
    ans = (low + high) // 2
    while True:
    user = input('Is your secret number ' + str(ans) + '?\n'
    + 'Enter "h" to indicate the guess is too high. '
    + 'Enter "l" to indicate the guess is too low. '
    + 'Enter "c" to indicate I guessed correctly. ')
    if user == 'h':
    high = ans
    elif user == 'l':
    low = ans
    elif user == 'c':
    print ('Game over. Your secret number was: ' + str(ans))
    break
    else:
    print ('Sorry, I did not understand your input.')
    ans = (low + high) // 2

    回复删除

  2. Well Done ! the blog is great and Interactive it is about How to consolidate Microsoft Excel Worksheets in VBA? it is useful for students and Python Developers for more updates on python

    python online Training

    回复删除
  3. Well Done ! the blog is great and Interactive it is about Python Notes: Gussing Secret Numberit is useful for students and Python Developers for more updates on python

    python online course

    回复删除