在python中创建高低游戏并需要很多帮助!

时间:2010-09-16 02:33:14

标签: python

这是Pseudocode

  1. 向用户打印说明
  2. 从变量high = 1000,low = 1和tries = 1
  3. 开始
  4. 虽然高于低
  5. 猜猜高低的平均值
  6. 要求用户回复猜测
  7. 处理四种可能的结果:
    • 如果猜测正确,请打印一条消息,尝试猜测并退出程序
    • 如果猜测太高,请打印一条消息,说“我会猜到更低。”
    • 如果猜测太低,请打印一条消息“我会猜得更高。”
    • 如果用户输入的值不正确,请再次打印说明。
  8. 我甚至不知道从哪里开始。

6 个答案:

答案 0 :(得分: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.

我还添加了一个小评论,详细说明了每个部分应该查看哪些语言元素。

答案 1 :(得分:0)

你没有说这是不是Python 2或3.以下应该适用于2的最新版本;我不熟悉3,但它可能至少会让你从那里开始。看到这是如何做作业,我只是建议你研究一些东西。

  • 你想得到猜测并在一个循环中检查它们。我建议查找while循环。
  • 要获取用户输入,请尝试raw_input
  • 要输出消息,请查找print
  • 使用if检查用户的回复。

答案 2 :(得分:0)

从第一点开始:

print "instructions to the user"

(只是更改字符串以提供更多信息,这不是编程问题! - ),然后继续第二点(三个作业,就像您的作业分配所说),然后是第三点:

while high > low:

那里 - 已经有一半的工作(6分中的1-3分)。除此之外,还有什么问题?你知道 average 意味着什么,所以(比方说)guess = (high + low) // 2对你来说是可以理解的,或者是什么?这就是第4点所需要的!您知道如何向用户询问问题并获得他们的回复吗?查看inputraw_input ...好吧,我已经涵盖了六点中的第一个,当然你可以至少“开始“现在! - )

答案 3 :(得分:0)

好的,这不是答案,但您需要查看该程序:

  1. 打印说明。
  2. 随机编号或使用自己的编号。 (对于兰特数,你需要使用模数除法技巧)
  3. 可能使用while循环:检查猜测的数字是高于还是低于或等于
  4. 如果打印量较高,如果打印量较低,则在等间断或呼叫退出的情况下(可能会打破会很好)。
  5. 伪代码:

    print "intructions"
    thenumber = rand() % 1000+1
    while (true)
        getInput(guess);
        if (guess > thenumber)
            print "Guess lower"
        else if (guess < thenumber)
            print "Guess higher")
        else
            exit //or break.
    

    虽然只是伪代码。

答案 4 :(得分:0)

print ("*********** Hi Lo Game ***********")
import random
x = (random.randint(1,100))
num1 = int(input("Enter your number:"))
while num1 < x:
   print ("Too Low")
   num1 = int(input("Enter your number:"))
while num1 > x:
   print ("Too High")
   num1 = int(input("Enter your number:"))
if num1 == x:
   print ("Congratulations! You are Correct")

答案 5 :(得分:-1)

运行高/低游戏w /得分

from random import randint
import time
print '='*20
print 'The Up / Down Game'
print 'Enter up or down !'
print 'Get 10 in a row for a reward!'
print '='*20
print "=    "+'GAME START'+"    ="
print '='*20
print ''
ans = ' '
score = 0
while True:

n1 = randint(2,13)
n2 = randint(2,13)
print "I have = %s" % (n1)

ans = raw_input("What do you choose: ")
if ans == 'up':
    print "Your number is : "
    time.sleep(0.5)
    print "."
    time.sleep(0.5)
    print ". %s" % (n2)
    time.sleep(1)
    if n1 > n2:
        print "Sorry you lost."
        time.sleep(2)
        print "Final score = %s" % (score)
        time.sleep(2)
        print "="*20
        print "Try Again"
        print "="*20
        score = 0

    elif n1 <= n2:
        score += 1
        if score > 1:
            print "That's %s in a row" % (score)
        elif score == 1:
            print "Thats  1 point"
        elif score == 10:
            print "Congratz you got the reward!!!"

elif ans == 'down':
    print "Your number is : "
    time.sleep(0.5)
    print "."
    time.sleep(0.5)
    print ". %s" % (n2)
    time.sleep(1)
    if n1 < n2:
        print "Sorry you lost."
        time.sleep(2)
        print "Final score = %s" % (score)
        time.sleep(2)
        print "="*20
        print "Try Again"
        print "="*20
        score = 0

    elif n1 >= n2:
        score += 1
        if score > 1:
            print "That's %s in a row" % (score)
        elif score == 1:
            print "Thats  1 point"
        elif score == 10:
            print "Congratz. You got the reward"


    else:
        tryAgain = raw_input("enter up or down only")