【问题标题】:Shortening a long if-else check in Python using better conditions使用更好的条件缩短 Python 中的长 if-else 检查
【发布时间】:2017-09-28 02:57:00
【问题描述】:

我正在编写一个 Python 程序,我需要一个 if-else 案例来选择 1 到 9 之间的数字,每个数字都分配给一个类。有关如何缩短此代码的任何建议?

import randint

variable1 = randint(1, 9)
if variable1 >= 9:
  print ("Your class is Tiefling") 
else: 
  if variable1 >= 8: 
    print ("Your class is Half-Orc") 
  else: 
    if variable1 >= 7: 
      print ("Your class is Half-Elf") 
    else:
      if variable1 >= 6: 
        print ("Your class is Gnome")
      else:
        if variable1 >= 5:
          print ("Your class is Dragonborn") 
         else: 
           if variable1 >= 4:
            print ("Your class is Human") 
          else:
            if variable1 >= 3:
              print ("Your class is Halfling") 
            else:
              if variable1 >= 2: 
                print ("Your class is Elf") 
              else:
                if variable1 >= 1:
                  print ("Your class is Dwarf")

【问题讨论】:

  • 你的缩进看起来很奇怪。你介意修一下吗?此外,如果此代码有效,您可能会在Code Review 获得更好的答案,但请在发布前查看帮助主题。具体来说,您需要将标题更改为代码用途的摘要,并在问题本身中添加完整的描述。祝你好运!
  • 你只需要“切换”。看here这个问题。
  • 当您真的只想检查完全相等时,您真的需要检查“大于或等于”吗? (反问)

标签: python python-3.x


【解决方案1】:

使用字典的不完整版本:

val_msg = {3: 'Your class is Halfling',
           2: 'Your class is Elf',
           1: 'Your class is Dwarf'}

from random import randint

variable1 = randint(1, 3)
print(val_msg[variable1])

请注意,randint 生成整数,我将其用作字典的键。

如果你需要做更复杂的事情,你可以将函数放入字典并调用它们(当然你也可以在这里使用基于list的解决方案):

def do_halfling_stuff():
    print('Your class is Halfling')
def do_elf_stuff():
    print('Your class is Elf')
def do_dwarf_stuff():
    print('Your class is Dwarf')

val_func = {3: do_halfling_stuff,
            2: do_elf_stuff,
            1: do_dwarf_stuff}


variable1 = randint(1, 3)
func = val_func[variable1]
func()

【讨论】:

    【解决方案2】:

    使用列表的示例

    import random
    
    classes = ['Tiefling', 'Half-Orc', 'Half-Elf', '...']
    
    print('Your class is ' + classes[random.randrange(len(classes))])
    

    根据 Alexis 的评论编辑。

    【讨论】:

    • 对于列表索引,简单地使用random.randrange(len(classes))更简单,更不容易出错
    • dict 版本更加灵活恕我直言。
    • 而int 版本要简单得多。不要忘记 YAGTNI。
    【解决方案3】:

    我希望这会有所帮助!

    import random 
    classList = ['Dwarf','Elf','Halfling','Human','Dragonborn','Gnome','Half-Elf','Half-Orc','Tiefling'] 
    print 'Your class is ' + random.choice(classList)
    

    【讨论】:

      【解决方案4】:

      我猜你可以使用:

      from random import randint
      variable1 = randint(1, 9)
      my_dict = {1: "Tiefling", 2: "Half-Orc", 3: "Half-Elf", 4: "Gnome", 5: "Dragonborn", 6: "Human", 7: "Halfling", 8: "Elf", 9: "Dwarf", }
      base = "Your class is "
      print("{}{}".format(base, my_dict[variable1]))
      

      【讨论】:

        【解决方案5】:

        如果你要多次使用这个,我建议你做一个函数:

        def race_definer():
            races = {1: 'Tiefling', 2: 'Half-Orc', 3: 'Half-Elf', 4: 'Dragonborn',
                     5: 'Elf', 6: 'Gnome', 7: 'Human', 8: 'Halfling', 9: 'Elf', 0: 'Dwarf'}
        
            print('Your race is {}.'.format(races[randint(0,9)]))
        

        你可以在需要时调用该函数:

        race_definer()
        

        在使用函数之前不要忘记将 randint 导入你的程序:

        from random import randint
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-17
          • 1970-01-01
          • 2023-04-09
          相关资源
          最近更新 更多