Little Cat
Little Cat

Reputation: 1

Shortening a long if-else check in Python using better conditions

I am writing a Python program, where I need to have an if-else case to choose a number between 1, and 9, each number is assigned to a class. Any suggestions on how to make this code shorter?

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")

Upvotes: 0

Views: 183

Answers (5)

gunslinger
gunslinger

Reputation: 118

If you are going to use this many times i recommend that you make a function:

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)]))

Than you can just call the function when you need it:

race_definer()

Just don't forget to import randint into your program before using the function:

from random import randint

Upvotes: 0

Pedro Lobito
Pedro Lobito

Reputation: 99041

I guess you can use:

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]))

Upvotes: 0

hiro protagonist
hiro protagonist

Reputation: 46921

an incomplete version using a dictionary:

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])

note that randint produces integers that i use as keys for the dictionary.

if you need to do more complicated things, you can put functions into the dictionary and call them (of course you can do the same with the list-based solutions here):

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()

Upvotes: 3

Cicero
Cicero

Reputation: 2972

Example using a list

import random

classes = ['Tiefling', 'Half-Orc', 'Half-Elf', '...']

print('Your class is ' + classes[random.randrange(len(classes))])

edited based on alexis's comment.

Upvotes: 6

Yeong Zhen Hong
Yeong Zhen Hong

Reputation: 71

I hope this helps!

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

Upvotes: 3

Related Questions