I am attempting to design and implement a horse race game, but I keep getting lost in the
while loops and if statement in the horse selection and wager prompt. Would someone please help me get passed this problem? Thanks.

print ("It's a beautiful day out here folks.  A fine day for a horse race.  Grab your mint julips, extravagant hats, and head over to the betting booths to place your bets.")

horse1 = "Achangeisgonnacome"
horse2 = "It'srainingmen"
horse3 = "Makemyday"
horse4 = "It'scurtainsforyou"
horse5 = "Speedygonzales"

odds1 = "30 to 1"
odds2 = "3 to 1"
odds3 = "25 to 1"
odds4 = "100 to 1"
odds5 = "2 to 1"

print ("For horse1, %s, the odds are %s"
       % (horse1, odds1))
print ("For horse2, %s, the odds are %s"
       % (horse2, odds2))
print ("For horse3, %s, the odds are %s"
       % (horse3, odds3))
print ("For horse4, %s, the odds are %s"
       % (horse4, odds4))
print ("For horse5, %s, the odds are %s"
       % (horse5, odds5))

selection = raw_input("Which horse will it be(horse1, horse2, horse3, horse4, or horse5)?: ")
if selection == "horse1":
    print ("%s it is."
           %(horse1))
bet = raw_input(int("And how much do you want to wager on %s (1,2,3,4,5,6,7,8,9)?: "
                        % (horse1))
    if (bet > 5):
        print ("You must feel really good about this one")
    else:
        print "You don't seem very confident"


elif:            
    selection == "horse2"
        bet = raw_input("And how much do you want to bet on %s? "
                        % (horse2))
        if bet > 5:
            print "You must feel really good about this one"
        else:
            print "You don't seem very confident"
elif:            
    selection == "horse3"
        bet = raw_input("And how much do you want to bet on %s? "
                        % (horse3))
        if bet > 5:
            print "You must feel really good about this one"
        else:
            print "You don't seem very confident"
elif:            
    selection == "horse4"
        bet = raw_input("And how much do you want to bet on %s? "
                        % (horse4))
        if bet > 5:
            print "You must feel really good about this one"
        else:
            print "You don't seem very confident"
else:            
    selection == "horse5"
        bet = raw_input("And how much do you want to bet on %s? "
                        % (horse5))
        if bet > 5:
            print "You must feel really good about this one"
        else:
            print "You don't seem very confident"

Recommended Answers

All 8 Replies

Maybe this could help you to start, change your betting according to new data structure of dictionary instead of simple scalar variables:

print ("It's a beautiful day out here folks.  A fine day for a horse race.  Grab your mint julips, extravagant hats, and head over to the betting booths to place your bets.")

horseodds ={"Achangeisgonnacome":30,
            "It'srainingmen":3,
            "Makemyday" : 25,
            "It'scurtainsforyou":100 ,
            "Speedygonzales" : 2}


for i,horse in enumerate(horseodds.keys()):
    print('For horse %i: "%s", the odds are %i to 1'
       % (i+1, horse, horseodds[horse]))

selection=1

while selection:
    selection = int(raw_input("Which horse will it be(1..%i)?: " % len(horseodds)))
    if selection in range(1,len(horseodds)+1):
        print("Horse number %i has name: %s" % (selection, horseodds.keys()[selection-1]))
##if selection == "horse1":
##    print ("%s it is."
##           %(horse1))
##    bet = raw_input(int("And how much do you want to wager on %s (1,2,3,4,5,6,7,8,9)?: "
##                        % (horse1))
##    if (bet > 5):
##        print ("You must feel really good about this one")
##    else:
##        print "You don't seem very confident"

I kind of see what you mean, but when I try and run what I have I continually get a syntax error at the "if bet >5:" line. It seems like I have a problem with the way I've arranged my if statements...Thanks for the help. Still having some problems though.

selection = raw_input("Which horse will it be(1..%i)?: " % len(horseodds))
if selection == "horse1":
    print ("%s it is."
           %(horse1))
    bet = raw_input(int("And how much do you want to wager on %s (1,2,3,4,5,6,7,8,9)?: "
                        % (horse1))
    if (bet > 5):
        print ("You must feel really good about this one")
    else:
        print "You don't seem very confident"

I added a while loop without error checking and example how to index by numbers dictionary for selecting a choice.

See update of code in post before. You would need to adapt your code of betting according to this example.

I see what you mean. Some of the functions you used are new to me. How do I link the bet to the horse? Is it as easy as linking "selection" to the bet prompt? I've only been at Python for a couple of weeks. I appreciate your patience.

while bet:
    bet = int(raw_input("And how much do you want to wager on %s?: " % (selection))
    if (bet > 5):
        print "You must feel good about this one."
    else:
        print "You don't seem very confident."

Make for example horsebets like horseodds, where the values are list of bets for the horse and the better's name or number (if you do realistic betting with many bets and betters).

Maybe it is better to move immediately from number to name as by my example:

selection = horseodds.keys()[selection-1])

You must add the error handling for inputs not numbers, if that is required.

I'm down to the horse race itself. I'm trying to use the random.shuffle(list) in order to randomize the horses at certain parts of the race. This is what I have. For some reason, instead of coming back as a randomized list of horses, its coming back as None. What am I doing wrong?

if bet > 1:
    print """Here come the horses now, folks.  They're being loaded into the 
          starting gate.  It wont be long now."""
    time.sleep(3)
    print "And they're off"
    lead = [horse1, horse2, horse3, horse4, horse5]
    x = random.shuffle(lead)
    time.sleep(3)
    print ("As they come out of the first turn, it's %s." % (x))
    time.sleep(3)
    print ("As they pass the halfway mark, it's %s."
           % (x))

random.shuffle orders the list, does not have result. Take the first element of the list after shuffle.

from __future__ import print_function
from random import shuffle
from time import sleep
print ("It's a beautiful day out here folks.  A fine day for a horse race.  Grab your mint julips, extravagant hats, and head over to the betting booths to place your bets.")

horseodds ={"Achangeisgonnacome":30,
            "It'srainingmen":3,
            "Makemyday" : 25,
            "It'scurtainsforyou":100 ,
            "Speedygonzales" : 2}


for i,horse in enumerate(horseodds.keys()):
    print('For horse %i: "%s", the odds are %i to 1'
       % (i+1, horse, horseodds[horse]))

print('Give horsenumber 0 to finish!')

selection=1

while selection:
    selection = int(raw_input("Which horse will it be(1..%i)?: " % len(horseodds)))
    if selection in range(1,len(horseodds)+1):
        print("Horse number %i's name is: %s" % (selection, horseodds.keys()[selection-1]))
        selection = horseodds.keys()[selection-1] 
        horselist=horseodds.keys()
        for i in range(5):
            shuffle(horselist)
            print('Lap %i, leading horse is: %s!' % (i+1,horselist[0])
                  if horselist[0] != selection else
                  'Lap %i, ** Your horse %s is leading!! **' % (i+1,horselist[0]))
            sleep(3)

        shuffle(horselist)

        print('Race finnished. Winner is: ',horselist[0])
        print('You won' if horselist[0]==selection else 'You lost')

Got it. Banging my head against the screen didn't work. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.