Python 机器人猜数字游戏

Python 机器人猜数字游戏,python,random,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:

我是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)
随机导入
尽管如此:
inp=int(输入(“键入一个数字(介于0和100之间)\n>”)
如果inp>0且inp<101:
打破
其他:
打印('抱歉,再试一次')
def机器(x):
猜测=0
guess=random.randint(01100)
打印(“我的猜测是%s”%guess)
猜猜看!=x:
尽管如此:
猜测次数+=1
response=input('我的猜测是过高还是过低?说实话\n>')
如果响应==“过高”:
guess=random.randint(0,guess)#分配新的猜测范围以缩小可能性
返回猜测
打破
猜机器(inp)#如何让它重新运行猜机器并重新猜一遍?
elif响应==“太低”:
guess=random.randint(guess,100)#分配新的猜测范围以缩小可能性
返回猜测
打破
猜机器(inp)#如何让它重新运行猜机器并重新猜一遍?
其他:
打印('对不起,没有收到')
其他:
打印(“找到了!您的号码是%s,我尝试了%s次”%guess,guesstries)
机器(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)
随机导入
尽管如此:
inp=int(输入(“键入一个数字(介于0和100之间)\n>”)
如果inp>0且inp<101:
打破
其他:
打印('抱歉,再试一次')
def猜测机器(x,猜测,猜测):
打印(“我的猜测是%s”%guess)
猜猜看!=x:
尽管如此:
response=input('我的猜测是过高还是过低?说实话\n>')
如果响应==“过高”:
guess=random.randint(0,guess)
猜测次数+=1
新猜
猜测机器(inp、newguess、猜测尝试)
elif响应==“太低”:
guess=random.randint(guess,101)
猜测次数+=1
新猜
猜测机器(inp、newguess、猜测尝试)
其他:
打印('对不起,没有收到')
其他:
打印(“找到了!您的号码是%s,我尝试了%s次”%guess,guesstries)
猜测机(inp,random.randint(01100),1)

一种简单的方法是让猜测机器返回数字是高还是低(或相等),然后将其放入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
随机导入
最大值=10
尽管如此:
inp=int(输入(“键入一个数字(介于0和{})\n>”。格式(MAX)))

如果inp>=0且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=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))
测试用例II(随机,6次尝试):


你是想让用户猜计算机在想什么,对吗?不,计算机是想猜用户输入了什么。好吧,我想知道如何让它保存从中得到的猜测结果。例如,如果它猜10,我说“它太高了”,下次它可能会继续猜2,我会说“太低了”,然后它会猜50。我如何使它存储10过高的内存,并且不应该超过10?实际上,如果它猜测10并说“过高”,它就不能猜测10以上的任何数字。请参阅上面的编辑。谢谢您的代码。我得到了不可靠的结果。它一遍又一遍地重复同样的猜测。另外,请看我对Geek先生回复的评论,看看我想解决什么问题。谢谢我刚才回答了你关于如何在循环中调用函数的问题,但没有改变你的逻辑。请看我在这段代码中的答案,以获得更好的代码:除了细微的更改,代码正是您想要的。
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
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