简体   繁体   English

如果语句未在Python中的模块内部执行

[英]If statement not executing inside module in Python

I'm confused, the if statement should by all means run imo, but of course this is programming so its not going to work. 我很困惑,if语句一定要运行imo,但是当然这是编程的,所以它不起作用。

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. (通过这种方式,代码几乎不可能完成)在程序中进行随机打印操作时,该程序似乎忽略了整个if语句,因为它将所有打印操作打印在if语句之外,但是没有一个语句中的内容。 Any help would be very much appreciatted, thank you in advance. 非常感谢您的帮助,在此先感谢您。

You need to cast numberofplayers to int , otherwise you are comparing and int and a string : 您需要将numberofplayersint ,否则将与int和一个string进行比较:

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

input() in Python 3 returns a string object, in difference with former Python2's input() . Python 3中的input()返回一个字符串对象,与以前的Python2的input()

This is what is going on: 这是怎么回事:

>>> a = input()
2
>>> a == 2
False
>>> a == '2'
True

While Paulo's answer fixes your immediate problem, there's a few other things to point out. 当Paulo的答案解决了您眼前的问题时,还有一些其他事情需要指出。

  • Since your function creates at least two players, name if correctly: create_players . 由于您的函数至少会创建两个播放器,因此请正确命名: 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: 您的分支if-elif作为单个for循环会更好:

     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]]) + '!') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM