机器人猜数字游戏

时间:2017-05-23 23:26:45

标签: python random

我是Python新手。我有一个程序要求用户输入,然后程序试图随机猜测用户输入的内容,但通过询问它是“太高”还是“太低”来缩小其搜索范围。

如何在用户输入“太高”或“太低”后重新运行猜测功能?

另外,请查看我的代码以获得改进建议。谢谢!

import random

while True:
    inp = int(input("type in a number (between 0 and 100) \n> "))

    if inp > 0 and inp < 101:
        break
    else:
        print ('sorry try again')

def guessmachine(x):
    guesstries = 0
    guess = random.randint(0, 100)
    print ("my guess was %s" %guess)
    if guess != x:

        while True:

            guesstries += 1

            response = input('was my guess too high or too low? Tell the truth \n>')       
            if response == 'too high':
                guess = random.randint(0, guess)   #assigning new guess range to narrow down possibilities
                return guess
                break
                guessmachine(inp)  #how do I make it rerun guessmachine and guess all over again?

            elif response == 'too low':
                guess = random.randint(guess, 100) #assigning new guess range to narrow down possibilities
                return guess
                break
                guessmachine(inp) #how do I make it rerun guessmachine and guess all over again?

            else:
                print ('sorry didnt get that')

    else:
        print ("Found it! Your number was %s and it took me %s tries" %guess, guesstries)


guessmachine(inp)

更新

import random

while True:
    inp = int(input("type in a number (between 0 and 100) \n> "))
    if inp > 0 and inp < 101:
        break
    else:
        print ('sorry try again')

def guessmachine(x, guess, guesstries):
    print ("my guess was %s" %guess)
    if guess != x:
        while True:
            response = input('was my guess too high or too low? Tell the truth \n>')       
            if response == 'too high':
                guess = random.randint(0, guess)
                guesstries += 1
                newguess = guess
                guessmachine(inp, newguess,guesstries)


            elif response == 'too low':
                guess = random.randint(guess, 101)
                guesstries += 1
                newguess = guess
                guessmachine(inp, newguess,guesstries)

            else:
                print ('sorry didnt get that')

    else:
        print ("Found it! Your number was %s and it took me %s tries" %guess, guesstries)


guessmachine(inp, random.randint(0, 100), 1)

2 个答案:

答案 0 :(得分:0)

一种简单的方法是让猜测机返回数字是高还是低(或等于)并将其放入while循环中。在一些简单的更改中,它看起来像:

import random

MAX = 10

while True:
    inp = int(input("type in a number (between 0 and {}) \n> ".format(MAX)))

    if inp >= 0 and inp <= MAX:
        break
    else:
        print ('sorry try again')

def guessmachine(x, guess, guesstries = 0):

    print ("my guess was %s" %guess)
    if guess != x:
        while True:
            response = input('was my guess too high or too low? Tell the truth \n>')       
            if response in ('too high', 'too low'):
                return response
            else:
                print ('sorry didnt get that')

    else:
        print ("Found it! Your number was %s and it took me %s tries" %(guess, guesstries))
        return 'found'

guesses = 0
guess = random.randint(0,MAX)
while True:
    guesses += 1
    was = guessmachine(inp,guess,guesses)
    if was == 'too high':
        guess = random.randint(0,guess)
    elif was == 'too low':
        guess = random.randint(guess,MAX)
    elif was == 'found':
        break

答案 1 :(得分:0)

经过一些改进和修正,这是最后的工作:

编辑:我现在正在使用最大值和最小值,因此每次猜测时,我都会缩小随机间隔:

import random

while True:
    inp = int(input("type in a number (between 0 and 100) \n> "))
    if inp > 0 and inp < 101:
        break
    else:
        print ('sorry try again')

def guessmachine(x, guess, guesstries=1, mn=0, mx=100):
    print("my guess was %s" %guess)
    print('{',mn,mx,'}')
    if guess != x:
        while True:
            response = input('was my guess too high or too low? Tell the truth \n>')
            if response in ['too high','too low']:
                guesstries += 1
                if response == 'too high':
                    if guess<mx: mx=guess-1
                elif response == 'too low':
                    if guess>mn: mn=guess+1
                guess = random.randint(mn, mx)
                guessmachine(x, guess, guesstries, mn ,mx)
                break
            else:
                print ('sorry didnt get that')
    else:
        print ("Found it! Your number was %s and it took me %s tries" %(guess, guesstries))

guessmachine(inp, random.randint(0, 100))

测试用例I(随机,5次尝试):

type in a number (between 0 and 100)
> 47
my guess was 75
{ 0 100 } # first search in 0,100
was my guess too high or too low? Tell the truth
>too high
my guess was 13
{ 0 74 } # search in 0,74 since 75 is high, any number more than it is too
was my guess too high or too low? Tell the truth
>too low
my guess was 37
{ 14 74 } # search in 14,74 since 13 is low, any number less than it is too
was my guess too high or too low? Tell the truth
>too low
my guess was 55
{ 38 74 } # you got the idea ...
was my guess too high or too low? Tell the truth
>too high
my guess was 47
{ 38 54 }
Found it! Your number was 47 and it took me 5 tries

测试案例II(随机,6次尝试):

type in a number (between 0 and 100)
> 55
my guess was 74
{ 0 100 }
was my guess too high or too low? Tell the truth
>too high
my guess was 42
{ 0 74 }
was my guess too high or too low? Tell the truth
>too low
my guess was 72
{ 42 74 }
was my guess too high or too low? Tell the truth
>too high
my guess was 46
{ 42 72 }
was my guess too high or too low? Tell the truth
>too low
my guess was 56
{ 46 72 }
was my guess too high or too low? Tell the truth
>too high
my guess was 55
{ 46 56 }
Found it! Your number was 55 and it took me 6 tries