开发者

Creating a high and low game in python and need a lot of help!

Here is the Pseudocode

  1. Print instructions to the user
  2. Start with the variables high = 1000, low = 1, and tries = 1
  3. While high is greater than low
  4. Guess the average of high and low
  5. Ask the user to respond to the guess
  6. Handle the four possible outcomes:
    • If the guess was right, print a message that tries guesses were required and quit the program
    • 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.

I don't even know where to begin.


Here's where you begin. Insert the specifications as documentation, then do one at a time, testing along the way.

# 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.

I've also added a small comment detailing what language elements you should look in to for each section.


You don't say if this is Python 2 or 3. The following should be good for recent versions of 2; I'm not familiar with 3, but it will probably at least get you started there too. Seeing as how this is homework, I'm just going to recommend some things for you to research.

  • You'll want to get the guesses and check them in a loop. I recommend looking up the while loop.
  • To get the user inputs, try raw_input.
  • To output messages, look up print.
  • Use if for checking the user's responses.


You begin with point one:

print "instructions to the user"

(just change the string to be more informative, that's not a programming problem!-), then continue with point two (three assignments, just like your homework assignment says), then with point three:

while high > low:

There -- that's half your work already (points 1-3 out of 6). What's giving you problems beyond that? Do you know what average means, so (say) guess = (high + low) // 2 is understandable to you, or what? That's all you need for point 4! Do you know how to ask the user a question and get their response? Look up input and raw_input... OK, I've covered the first five of the six points, surely you can at least "get started" now!-)


Ok, this isn't the answer but you need to look at the program:

  1. Print out instructions.
  2. Make a random number or a use your own. (For rand numbers you need to use a modulus divide trick)
  3. Probably using a while loop: check if the number guessed is higher or lower or equal
  4. In case of higher print higher, in case of lower print lower, in case of equal break or call exit (probably breaking will do fine).

Pseudo Code:

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.

Just pseudo code though.


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")


Well working Hi/Low game w/score

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")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜