My assignment is to write a golf program that tallies the scores and prints out names and final scores in this format: Player4:21, Player2:33 etc etc. My question is can you use the range function to assign input to the proper variable name in Python 2.7. Here's what I have so far.

def players ():
    playerQty = 0
    
    playerQty = input ("How many players are playing? ")
    if playerQty > 4:
        print ("Too many players, please enter a smaller number.")
        playerQty = input ("How many players are playing? ")        
        
    for x in range (playerQty):
        players = raw_input("What is player's name? ")
       
def scores():
    accum = 0
    
    for x in range(19):
        print ("You are on hole", x+1)
        for x in range(playerQty):
            scores = input ("What was your score for this hole?")

I want to assign their names to different variable name per player, but with the quantity of players being a variable, it's proving to be difficult. Once I can achieve that I can assign scores to specific players, turn them into a list, sort them, then get the proper output. Any, and all, help would be greatly appreciated.

Recommended Answers

All 4 Replies

First you must learn to return values or to append to list or add to dictionary the values. playerQty etc is local variables in function. See my post of Dice throwing game variable players and throws for my preferred form of input by function (of course you need to adapt the prompt form), which is able to deal with wrong types of input.

I would recommend to forget the input form in Python2, so that is why I always overwrite it to use only raw_input style, but name it input. That way my program runs in both Python2 and Python3.

Ok....I read your dice program, but still confused. Where did you get the input for number of players? I realized, after I posted my code, that some things were wrong. I need to pass playerQty to the next function, and that there are only 18 holes in golf and not 19.

Here is what I'm asking worded a bit clearer.

for x in range(playerQty):
   player(x)= raw_input("What is your name? ")

In the raw input statement, can x be used to create different variable names? For example player0=Bill, player1=Mike etc etc.

You need to stop little and read for example this http://diveintopython.org/native_data_types/lists.html and continue then your coding. You win the time you spend reading this by saving the time of debugging/banging your head to the wall ;)

The 19th hole on the golf course is the bar in the club house!

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.