机器人猜数Gam

2024-03-29 12:52:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我对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)

Tags: andifresponseitrandomelsetooprint
2条回答

一个简单的方法是让你的猜测机器返回数字是高还是低(或相等),并将其放入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

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

编辑:我现在使用一个最大值和一个最小值,这样每次猜测时,我都会缩小随机间隔:

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

相关问题 更多 >