【问题标题】:Store numbers and compare based on user input存储数字并根据用户输入进行比较
【发布时间】:2017-01-26 02:00:09
【问题描述】:

目前正在努力从头开始学习 Python 3.5,并且非常享受这个过程。最新的事情是编写一个执行以下操作的基本程序..

  • 让用户想一个介于 1 到 100 之间的数字
  • 猜猜是什么
  • 要求用户输入“1”如果太低,“3”如果太高,“2”如果 正确
  • 根据用户输入提供一个新号码
  • 限制自己尝试 10 次,然后羞愧地退出
  • 做对了就庆祝

现在我想我已经得到了所有的工作减去更高/更低的功能。我的问题是这个。

注意 这与该主题的其他问题不同,因为我不想输入任何数字供 PC 使用。还希望生成限制在给定值范围内的伪随机数,以供 PC 进行后续猜测。不增加。

“我如何实现一个功能,查看 var 'guess' 并根据用户输入将其修改为更高或更低,同时保持其高于 0 和低于 100”。

代码:

import random  

print("\tWelcome to the psychic computer")
print("\nI want you to think of a number between 1 and 100.")
print("I will try to guess it in 10 or less tries.\n")
print("\n Press 1 for a lower guess, 3 for a higher one and 2 if I get it       right\n")

tries = int(1)

guess = random.randint(1, 100)
print("\nI guess ", + guess, " Is this correct?")


while tries < 10:
    feedback = int (input("\nEnter 1 if too high, 3 if too low or 2 if bang   on\n\n"))
    tries += 1
    if feedback == 1:
        print("Balls! Guess I need to go Higher will", + guess, + "do?")

    elif feedback == 3:
        print("\nSh*te! Lower it is then...")

    elif feedback == 2:
        print("YEEAAAHH BOYEEEE! Told you I was phychic.")
        input("\nHit enter to quit")
        end

    elif feedback != (1,2,3):
        print("Not a valid guess. Try again.")
        break

    if tries >= 9:
        print("\nSh*t, guess I'm not so psychic after all")
    input("\nHit enter to exit")

【问题讨论】:

  • 您的 ifelif 块在 while 循环之外。你的实际代码是这样的,还是只是复制代码时出错?
  • 复制代码时出错。道歉。
  • 那么请改正。
  • 完成。我可能在另一篇文章中遗漏了一些东西,但看起来建议的答案都没有给出前一个数字和 0 或 100 之间的随机猜测——它们似乎会增加,如果可能的话,我很想避免这种情况。我还想避免将答案输入电脑。

标签: python python-3.x


【解决方案1】:

您可以执行二进制搜索之类的操作。使用 low_number 和 high_number 通过取两者的中间值来计算猜测。这些最初可以设置为 0 和 100。您还应该跟踪您最后的猜测。如果你最后的猜测太低,你可以设置你的 low_number 等于那个guess+1。如果您最后的猜测太高,您可以将 high_number 设置为该 guess-1。

【讨论】:

  • 好的,这是有道理的。感谢您的建议。
【解决方案2】:

好的,这就是我目前所拥有的。它的工作原理是它将根据用户输入以(半)随机更高或更低的猜测来响应,但尚未将原始猜测存储为参考。我明天会看看那个。再次感谢您的建议。

顺便说一句,一旦工作,我计划使用数学并提出一个解决方案,总能在 x 次尝试中得到正确的结果..

    import random  

    print("\tWelcome to the psychic computer")
    print("\nI want you to think of a number between 1 and 100.")
    print("I will try to guess it in 10 or less tries.\n")
    print("\n Press 1 for a higher guess, 3 for a lower one and 2 if I get it    right\n")

    tries = int()
    guess = random.randint(1, 100)
    high_number = int(100 - guess)
    low_number = int(1 + guess)

    print("\nI guess ", + guess, " Is this correct?")

    while tries < 10:

    feedback = int (input("\nEnter 1 to go higher, 3 to go lower or 2 if bang    on\n\n"))
    tries += 1
    if feedback == 1:
        guess = random.randint(high_number, 100)
        print("Balls! Guess I need to go Higher how about", + guess)

        elif feedback == 3:
            guess = random.randint(1, low_number)
            print("\nSh*te! Lower it is then... what about", + guess)    

        elif feedback == 2:
            print("YEEAAAHH BOYEEEE! Told you I was phychic.")
            input("\nHit enter to quit")
            end

        elif feedback != (1,2,3):
            input("Not a valid guess. Try again.")
            break

if tries >= 9:
    print("\nSh*t, guess I'm not so psychic after all")
input("\nHit enter to exit")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    相关资源
    最近更新 更多