在Python中创建一个高低游戏,需要很多帮助!

0 投票
6 回答
14415 浏览
提问于 2025-04-16 04:12

下面是伪代码的内容:

  1. 给用户打印一些操作说明
  2. 先设置三个变量:high = 1000,low = 1,tries = 1
  3. 只要 high 大于 low,就继续进行
  4. 猜测 high 和 low 的平均值
  5. 让用户对这个猜测做出回应
  6. 处理四种可能的结果:
    • 如果猜对了,打印一条消息,告诉用户一共猜了多少次,然后结束程序
    • 如果猜的太高了,打印一条消息,告诉用户“我会猜得更低。”
    • 如果猜的太低了,打印一条消息,告诉用户“我会猜得更高。”
    • 如果用户输入了错误的值,重新打印操作说明。

我甚至不知道从哪里开始。

6 个回答

0

你可以从第一点开始:

print "instructions to the user"

(只需要把字符串改得更清楚一点,这不是编程上的问题!),然后继续第二点(三个赋值,就像你的作业要求的那样),接着是第三点:

while high > low:

这样——你已经完成了一半的工作了(1-3点,总共6点)。那么,接下来是什么让你感到困惑呢?你知道平均值是什么意思吗?比如说guess = (high + low) // 2你能理解吗?这就是你完成第四点所需要的!你知道怎么问用户一个问题并获取他们的回答吗?查一下inputraw_input... 好吧,我已经讲了前点中的点,你至少可以“开始”了吧!-)

0

你没有说明这是Python 2还是3。以下内容适用于最近的2版本;我对3不太熟悉,但这至少可以帮助你入门。既然这是作业,我就给你推荐一些你可以研究的东西。

  • 你需要获取用户的猜测并在一个循环中检查它们。我建议你查一下while循环
  • 要获取用户输入,可以试试raw_input
  • 要输出信息,可以查查print
  • 使用if来检查用户的回答。
2

这里是你开始的地方。先把需求写成文档,然后一步一步来,边做边测试。

# Print instructions to the user
### 'print "xyz"' will output the xyz text.

# Start with the variables high = 1000, low = 1, and tries = 1
### You can set a variable with 'abc = 1'.

# While high is greater than low
### Python has a while statement and you can use something like 'while x > 7:'.
### Conditions like 'x > 7', 'guess == number' can also be used in `ifs` below.

    # Guess the average of high and low
    ### The average of two numbers is (x + y) / 2.

    # Ask the user to respond to the guess
    ### Python (at least 2.7) has a 'raw_input' for this, NOT 'input'.

    # If the guess was right, print a message that tries guesses were required
    # and quit the program
    ### Look at the 'if' statement for this and all the ones below.

    # If the guess was too high, print a message that says “I will guess lower.”
    # If the guess was too low, print a message that says “I will guess higher.”
    # If the user entered an incorrect value, print out the instructions again.

我还加了一些小注释,告诉你每个部分应该关注哪些语言元素。

撰写回答