【问题标题】:i cant seem to get my .append to loop this is what i have so far. I the while function as well我似乎无法让我的 .append 循环这是我到目前为止所拥有的。我也是 while 函数
【发布时间】:2017-12-24 18:07:31
【问题描述】:

我似乎无法让我的 .append 循环这是我目前所拥有的。我也是 while 函数。

a = input('options '
          '1.add a player'
          '2.remove a player'
          '3.exit')
player = []
if a == 1: player.append('players name')

【问题讨论】:

  • 您到底想实现什么?代码中没有循环,应该重复什么动作?
  • 实际上无法理解,您想达到什么目的。如果是无限循环,只需用“while True:”块包装这段代码,并添加“if a == 3:brake”即可退出循环。

标签: python loops


【解决方案1】:

输入返回一个字符串,而不是一个 int,所以 a 将包含类似“1”的东西。所以在你的 if 语句中,比较必须是 a=="1"。

【讨论】:

  • 旁注:这只适用于 Python 3,但如果 OP 使用的是 Python 2,则用户输入会被解释(可能应该使用 raw_input()))
【解决方案2】:

在输入函数后添加“a=int(a)”是另一种解决问题的方法,这样就可以改变“a”的数据类型。

【讨论】:

    【解决方案3】:

    好的,这就是我目前所拥有的。我得到了一切工作,除了我需要它循环所有选项直到退出。

    a = input('options '
              '1.add a player'
              '2.remove a player'
              '3. Print roster.'
              '4. name correction'
              '5.exit')
    player = []
    while a == '1':
     player.append(input("players name"))
     a = input('options '
              '1.add a player'
              '2.remove a player'
              '3. Print roster.'
              '4. name correction'
              '5.exit')
    
      if a == '2':
        player.remove(input("players name"))
        a = input('options '
              '1.add a player'
              '2.remove a player'
              '3. Print roster.'
              '4. name correction'
              '5.exit')
    
      if a == '3':
       print(player)
       a = input("options enter"
                 "1.add a player"
                 "2.remove a player"
                 "3. Print roster."
                 "4. name correction"
                 "5.exit")
      if a == '4':
       player.remove(input("players name to correct"))
       player.append(input('correction'))
       a = input('options enter'
              '1.add a player'
              '2.remove a player'
              '3. Print roster.'
              '4. name correction'
              '5.exit')
    

    【讨论】:

      【解决方案4】:

      我现在开始了。

      a = input('options '
            '1.add a player'
            '2.remove a player'
            '3.exit')
      player = []
      while a == '1':
        player.append(input("players name"))
        a = input('options '
                '1.add a player'
                '2.remove a player'
                '3.exit')
      
      
      
      print(player)
      

      【讨论】:

      • 如果你输入“2”会发生什么?
      • 会是假的,关闭程序
      猜你喜欢
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 2017-12-04
      • 2013-11-12
      • 1970-01-01
      • 2011-06-10
      • 2018-01-14
      相关资源
      最近更新 更多