【问题标题】:Why is my int() function not working python 2.7 [duplicate]为什么我的 int() 函数不起作用 python 2.7 [重复]
【发布时间】:2015-01-04 18:53:28
【问题描述】:
def game():    

modex = False
while modex == False:
    mode = raw_input("please select a mode: ")
    try:
    ## THE PROBLEM IS HERE!
        int(mode)
        if mode == 1:
            modex = True
            break
        elif mode == 2:
            modex = True
            break
        elif mode == 3:
            modex = True
            break
        else:
            print "invalid #. try again"
            modex == False
            continue

    except:
        print "invalid # try again"
        continue
game()

我似乎无法将模式转换为整数,例如:

输入:1 output: invalid # 重试

对于我正在制作的游戏,我有 3 种模式,因此 try-except 语句中的 if 语句有 3 种模式

你能帮帮我吗?我正在使用 python 2.7

【问题讨论】:

  • 应该是mode = int(mode)
  • 使用modexbreak。也就是说,使用breakcontinue 是不必要的。
  • while True 会做同样的事情,modex 是多余的,你似乎也没有返回任何东西,所以我看不出这个函数实际上在做什么。

标签: python python-2.7 int


【解决方案1】:

int(mode) 值分配给某物。最佳方法:mode = int(mode)

你也可以试试:mode = int(raw_input:('please select a mode:'))

【讨论】:

  • 感谢 toni,我的代码现在可以运行了!
猜你喜欢
  • 2013-06-13
  • 1970-01-01
  • 2018-06-06
  • 2019-10-06
  • 1970-01-01
  • 2017-09-23
  • 2014-06-01
  • 2021-04-06
  • 2013-04-04
相关资源
最近更新 更多