【问题标题】:Help with a guess my number program in python [closed]帮助猜测我在python中的数字程序[关闭]
【发布时间】:2011-09-18 23:32:39
【问题描述】:

我正在尝试编写一个 python 程序,程序在其中猜测用户编号。我觉得自己像个白痴,我正在关注的部分是数学,我觉得这是我已经研究了几个小时的事情之一,其他人将能够带来新的视角和轻松解决。我想一开始我会几乎完全将用户组件从其中移除。到目前为止,这是我所拥有的,它被困在逻辑部分,最终只是一直说它太低或太高,无穷无尽:

MIN=0
MAX=100
firstguess = MAX - MIN
firstguess = firstguess/2

while 1==1:
number = int(raw_input("Enter your number 0-100:"))
print firstguess
oldguess = firstguess
if firstguess > number:
    print "First guess is too high."
    raw_input()
    guess = int(25)
    print guess
    while guess != number:
        if guess > number:
            print "My guess was too high."
            raw_input()
            nextguess = oldguess - guess
            nextguess = nextguess/2
            nextguess = guess - nextguess
            oldguess = guess
            guess = nextguess
        elif guess == number:
            print "I win!"
            exit
        elif guess < number:
            print "My guess was too low."
            raw_input()
            nextguess = oldguess - guess
            nextguess = nextguess/2
            nextguess = nextguess + guess
            oldguess = guess
            guess = nextguess
elif firstguess == number:
    print "I win!"
elif firstguess < number:
    print "My first guess was too low."
    raw_input()
    guess = 75
    print guess
    print guess
    while guess != number:
        if guess > number:
            print "My guess was too high."
            raw_input()
            nextguess = oldguess - guess
            nextguess = nextguess/2
            nextguess = guess - nextguess
            oldguess = guess
            guess = nextguess
        elif guess == number:
            print "I win!"
            exit
        elif guess < number:
            print "My guess was too low."
            raw_input()
            nextguess = oldguess - guess
            nextguess = nextguess/2
            nextguess = nextguess + guess
            oldguess = guess
            guess = nextguess

【问题讨论】:

  • 咳嗽作业咳嗽
  • 不是一个答案,而是:做软件设计的首要和最重要的原则是识别模式。您的代码有 4 个非常相似的部分,您可能应该重构代码并将相似的部分提取到一个函数中。实际上,成功的软件开发主要是在代码或现实世界中发现模式并概括事物......
  • 大家好,我想让你们知道这实际上不是家庭作业,我的学校不提供编程课程,所以我在自学。
  • 这是来自class.coursera.org 的课程,您必须遵守荣誉守则。 Uve 违反了该密码。 class.coursera.org/interactivepython-2012-001/wiki/…

标签: python logic


【解决方案1】:

好的,所以 1) 我不知道 python 和 2) 我不知道你的代码做了什么,也懒得搞清楚。为什么?因为我认为你过于复杂了。

我相信你可以通过简单的二分搜索来解决这个问题。在伪代码中:

low = 0                        // lowest possible value of user's number
high = 100                     // highest possible value of user's number

while low < high
    guess = (low + high) / 2   // guess the average of low and hight
    if guess is too low        // if guess is too low
        then low = guess + 1   // then the lowest possible value is guess + 1
    if guess it too high       // if guess is too high
        then hight = guess - 1 // then the highest possible value is guess - 1
    if guess equals the value
        we've found the answer

at this point (low + high) / 2 should be the answer

该算法将搜索空间减半并检查用户号码在哪一侧。示例在1-10 范围内运行,其中用户号码为3L 代表低,H 代表高,G 代表猜测:

L         G         H  | guess is too high so we set high to guess-1
0 1 2 3 4 5 6 7 8 9 10 |
                       |
L   G   H              | guess is too low so we set low to guess+1
0 1 2 3 4 5 6 7 8 9 10 |
                       |
      L H              | guess is in the same position as low and is correct so
0 1 2 3 4 5 6 7 8 9 10 | we've found the number

【讨论】:

    【解决方案2】:

    我可能正在做你的功课,但你似乎对这个问题做出了诚实的尝试:

    bounds = [0, 100]
    minimum, maximum = bounds
    
    number = int(raw_input('Pick a number [{0}-{1}]: '.format(minimum, maximum)))
    
    while guess != number:
      guess = int((maximum + minimum) / 2)
      raw_input('Guessing {0}'.format(guess))
    
      if guess > number:
        maximum = guess
      elif guess < number:
        maximum = bounds[1]
        minimum = guess
    
    print 'I win!'
    

    逻辑很简单:

    1. 选择猜测的下限和上限。
    2. 让用户选择一个号码,存储为number
    3. 虽然猜测不等于数字:
      1. 选择最小和最大范围界限之间的中点。
      2. 如果猜测值大于数字,则数字一定小于猜测值,因此我们将最大猜测值设置为当前猜测值。
      3. 如果猜测值小于数字,则我们将最大值设置为可能的最大数字,并将最小值设置为猜测值。
    4. 一旦猜到数字,循环就会中断,我们就赢了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-21
      相关资源
      最近更新 更多