0

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")
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
  • Your indentation looks really weird. Would you mind fixing that? Also, if this code works, you'll probably get better answers over at [codereview.se], but please take a look at the help topics before posting. Specifically, you'll need to change the title to a summary of the code's purpose, and add a full description in the question itself. Good luck! – zondo Apr 30 '17 at 12:44
  • You just need "switch". Look [here](http://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python) for this issue. – Sklert Apr 30 '17 at 12:45
  • Do you really need to check for "greater than or equal" when you really want to check for exactly equal only? (rhetorical question) – JJJ Apr 30 '17 at 12:45
  • Please consider http://stackoverflow.com/questions/11479816/what-is-the-python-equivalent-for-a-case-switch-statement. – toonice Apr 30 '17 at 13:03

5 Answers5

6

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.

Cicero
  • 2,872
  • 3
  • 21
  • 31
3

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()
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
3

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

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]))
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

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
gunslinger
  • 118
  • 10