0

I'm confused, the if statement should by all means run imo, but of course this is programming so its not going to work.

def create_player():
    name = str()
    names = []
    print("hey there")
    if numberofplayers == 2:
        name = input("Please enter Player 1's name: ")       
        names.append(name)
        name = input("Please enter Player 2's name: ")
        names.append(name)
    elif numberofplayers == 3:
        name = input("Please enter Player 1's name: ")       
        names.append(name)
        name = input("Please enter Player 2's name: ")
        names.append(name)
        name = input("Please enter Player 3's name: ")
        names.append(name)
    elif numberofplayers == 4:
        name = input("Please enter Player 1's name: ")       
        names.append(name)
        name = input("Please enter Player 2's name: ")
        names.append(name)
        name = input("Please enter Player 3's name: ")
        names.append(name)
        name = input("Please enter Player 4's name: ")
        names.append(name)
        print("Welcome to the game " + names[0] + ", " +names[1]+ ", "+names[2]+ " and "+names[3]+"!")

print("test")
numberofplayers = int(0)
numberofplayers = input("# of players")
create_player()
print ("aha")

(By the way this code is not be any means anywhere near finished) The random print operations were there for testing, the program seems to ignore the whole if statement, as it prints all of the print operations outside of the if statement, but none that are inside the statement. Any help would be very much appreciatted, thank you in advance.

5
  • What Python version are you using? Commented Feb 27, 2014 at 21:34
  • 1
    You need to add global numberofplayers inside create_player() Commented Feb 27, 2014 at 21:35
  • Please fix your indents in the sample code you provided. Commented Feb 27, 2014 at 21:41
  • Thank you :) and I think its 3.3, not sure, on a school computer. Commented Feb 27, 2014 at 21:54
  • You can simplify this: for n in range(numberofplayers): names.append(input("Enter player {0}'s name".format(n+1))). This removes much of the duplication and works for any number of players. Commented Feb 27, 2014 at 22:00

2 Answers 2

2

You need to cast numberofplayers to int, otherwise you are comparing and int and a string:

numberofplayers = int(input("# of players"))

input() in Python 3 returns a string object, in difference with former Python2's input().

This is what is going on:

>>> a = input()
2
>>> a == 2
False
>>> a == '2'
True
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I can see now what is happening, the if statement wasnt finding the items stated in the argument.
Actually, the if statement wasn't succeeding because the comparisons yielded False
1

While Paulo's answer fixes your immediate problem, there's a few other things to point out.

  • Since your function creates at least two players, name if correctly: create_players. It avoids confusion later.

  • Don't rely on globals or values outside of your function scope. You should pass them explicitly into your functions.

    def create_players(numberofplayers):
        ...
    
    numberofplayers = int(input('# of players'))
    create_players(numberofplayers)
    
  • These lines don't do anything useful:

    name = str()
    
    numberofplayers = int(0)
    

    You don't need to define variables as types in order to use them, and you're immediately replacing these with real values.

  • Your branching if-elif would be better as a single for loop:

    names = []
    for player in range(1, numberofplayers+1):
        names.append( input("Please enter Player %d's name: " % player) )
    print('Welcome to the game ' + ' and '.join([', '.join(names[:-1]), names[-1]]) + '!')
    

2 Comments

What do you mean by pass explicitly? Sorry I'm relatively new to programming, studying it at high school.
When you have x = 1; do_something_with_x() you're relying on x being defined outside of the function. It's generally a lot more flexible to do x = 1; do_something(x); here it's passing the value into the function rather than finding it outside of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.