在Python中创建一个高低游戏,需要很多帮助!
下面是伪代码的内容:
- 给用户打印一些操作说明
- 先设置三个变量:high = 1000,low = 1,tries = 1
- 只要 high 大于 low,就继续进行
- 猜测 high 和 low 的平均值
- 让用户对这个猜测做出回应
- 处理四种可能的结果:
- 如果猜对了,打印一条消息,告诉用户一共猜了多少次,然后结束程序
- 如果猜的太高了,打印一条消息,告诉用户“我会猜得更低。”
- 如果猜的太低了,打印一条消息,告诉用户“我会猜得更高。”
- 如果用户输入了错误的值,重新打印操作说明。
我甚至不知道从哪里开始。
6 个回答
0
你可以从第一点开始:
print "instructions to the user"
(只需要把字符串改得更清楚一点,这不是编程上的问题!),然后继续第二点(三个赋值,就像你的作业要求的那样),接着是第三点:
while high > low:
这样——你已经完成了一半的工作了(1-3点,总共6点)。那么,接下来是什么让你感到困惑呢?你知道平均值是什么意思吗?比如说guess = (high + low) // 2
你能理解吗?这就是你完成第四点所需要的!你知道怎么问用户一个问题并获取他们的回答吗?查一下input
和raw_input
... 好吧,我已经讲了前五点中的六点,你至少可以“开始”了吧!-)
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.
我还加了一些小注释,告诉你每个部分应该关注哪些语言元素。