【问题标题】:Python number guessing gamePython猜数字游戏
【发布时间】:2013-10-09 09:09:12
【问题描述】:

我在网上找到了一些练习题,大部分都可以解决,但这道题难倒了我。它不是家庭作业,所以我没有得到成绩。但是,没有提供解决方案,因此获得答案的唯一方法就是这样做。

任务要求你写一个问题,玩数字 1-100 的数字猜谜游戏。但是,这个尝试通过区间猜测来猜测用户数,例如[1, 100],并使用first+last/2生成下一个问题。

我从该站点运行了一个示例。

想一个介于 1 和 100(含)之间的数字。
用字母 y 或 Y 回答以下问题,用字母 y 或 Y 表示是,n 或 N 表示否。
间隔:[1,100]。你的号码 间隔:[1,50]。你的号码 间隔:[1,25]。你的号码 间隔:[1,13]。你的号码 间隔:[8,13]。你的号码 间隔:[11,13]。你的号码 间隔:[11,12]。你的号码 您的号码是:11

到目前为止,这是我的代码,但我什至不知道从哪里开始,因为 while 循环不断给我一个无限循环。我知道“中间”数字需要是一个整数,否则它将是一个无限循环,但我似乎无法弄清楚如何做到这一点。

x = input("Is your numbr <=50?")
count = 100
while x=="y" or "Y":
    count = count/2
    x = input("Is your number <=",count,"?")
    print(count)

如果有人有任何提示,将不胜感激。

【问题讨论】:

  • while 循环的问题在于,x=="y" or "Y" 并不代表您认为的那样。你想要x=="y" or x=="Y",或者x in ('y', 'Y')
  • 表达式x=="y" or "Y" 被评估为(x=="y") or "Y"(括号说明了评估的顺序)。由于非空字符串总是True,它就像(x=="y") or True,它总是 True。你可能想要x in ("y", "Y"),或者更好的是x.lower() == "y"

标签: python while-loop python-3.3


【解决方案1】:

一个好主意是有一个简单的while True: 循环,在其中您保持最大猜测和最小猜测。然后你问用户他们的数量是否大于两者的平均值。如果是,请将您的最小猜测更新为平均值。如果不是,则将最大猜测值降低到平均值。重复直到两次猜测相等,此时你已经找到了数字,并且可以跳出无限循环。

当然,您必须进行一些简单的奇偶校验,以确保您在每一轮中确实改变了您的猜测。你真的应该将raw_input() 用于字符串,input() 用于python 格式的数据。

【讨论】:

    【解决方案2】:

    问题就在这里:

    while x=="y" or "Y":
    

    表达式“Y”的计算结果总是为真。

    你想要

    while x == "y" or x == "Y":
    

    即使这样,当用户键入“N”时,这也会结束循环。一个工作循环是这样的:

    finished = False
    while not finished:
        if x == "y":
            upper -= (upper-lower)/2
            # prompt for input
        elif x == "n":
            lower += (upper-lower)/2
            # prompt for input
        if upper == lower or (upper - 1) == lower:
            finished = True
            # final output
    

    你应该可以从那里填空。

    【讨论】:

    • 这里的任何东西都不需要finished 标志;只是break 在最后一步。
    • 你需要做一些奇偶校验,如果upper=12和lower=11会卡住。
    • 而 x.lower() == 'y':
    • 而 x 在 ['y', 'Y']:
    • 我的 while 语句确实如此。我不敢相信我这样做了,但没有注意到。您的工作循环也完全有意义。我了解您是如何做所有事情的,我只是希望我能像您一样知道如何运用我的知识:(。非常感谢!
    【解决方案3】:

    这个问题的整个想法是让两个“界限”都从 1 和 100 开始,每次你提出一个问题“你是数字

    像这样。

    lower = 1
    high = 100
    mid = (high + lower)/2 -> at start it will be 50
    

    如果用户回答“是”,则取当前下限到该范围中间的范围,否则继续使用从 mid+1 到结尾的范围,如下所示:

    如果用户回答是:

    high = mid
    

    如果用户回答否:

    lower = mid +1
    

    这个想法的最后一部分是检测范围lower-high只包含2个数字,或者是像这样[11,12]一样的数字,你使用用户的最终答案来选择正确的答案和程序终止,完整代码在这里,您可以测试它:

    found = False
    range_lower_bound = 1
    range_high_bound = 100
    
    print "Think of a number between 1 and 100 (inclusive)."
    print "Answer the following questions with letters y or Y for yes and n or N for no."
    
    while not found:
        range_mid = (range_high_bound + range_lower_bound) / 2
        x = raw_input('interval: [%s,%s]. Is your number <= %s? ' % (range_lower_bound, range_high_bound, range_mid))
        if x.lower() == 'y':
            # Check if this is the last question we need to guess the number
            if range_mid == range_lower_bound:
                print "Your number is %s" % (range_lower_bound)
                found = True
            range_high_bound = range_mid
        # here i'm defaulting "anything" no N for simplicity
        else:
            # Check if this is the last question we need to guess the number
            if range_mid == range_lower_bound:
                print "Your number is %s" % (range_high_bound)
                found = True
            range_lower_bound = range_mid + 1
    

    希望对你有帮助!

    【讨论】:

    • 是的!非常感谢您将其分解,它可以帮助我了解在尝试解决问题时应该怎么想。我唯一的问题是你在哪里使数字浮动?他们不应该只是一个int吗?为什么它打印
    猜你喜欢
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2021-02-01
    • 2021-05-18
    • 2021-04-29
    相关资源
    最近更新 更多