5
\$\begingroup\$

I'm trying to learn Python and this is my fist functioning little bit I wrote for a campaign I'm doing in D&D. I was wondering if I was doing anything wrong or if anyone had tips on how to make it more readable, function smoother, or be more interactive.

from random import randint
spells = '''Rol 
#A LOT OF TEXT FROM HERE

I copied this part from roll20 website list of spells then just broke it up biased on the line breaks. 
#to here...
'''

spells=spells.split('''\n\n''')

for i in [348, 332, 315, 294, 262, 224, 192, 149, 94, 44, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]:
  del spells[i]

def ask():
  print('"So ya done goofed I see, usin magic where there not be, I have your roll and the spell out of control, just tell me what you need!"')
  while True:
    print()
    print('if you need a random spell type random or r, if you need a specific spell enter a number from 1 to '+str(len(spells)))
    need=input()
    if need.isdigit():
      need=int(need)
      if need > len(spells)  :
        print('"THERES NOT THAT MANY SPELLS YE TROLLUP"')
      elif need<1:
        print('"thee list start at ONE ye trollup"')
      else:
        print(spells[need-1])

    elif need.lower().startswith('r'):  
      need=randint(0,len(spells)+1)
      need=need-1
      print(spells[need])

    else:
      print('FOLLOW MY INSTRUCTS WITH INTIGER OR RANDOM YE GOD FEARING REBEL')



ask()

Also, to get that fun little list that I made to get rid of useless strings in my spells list, I went ahead and wrote this:

guess=0
correctGuess=[]
for i in spells:
  if len(i)<15: #because I knew the strings I wanted to remove were under 15 in length 
    print(i)
    x=i
    while x != spells[guess]:
      guess=guess+1
      if x==spells[guess]:
        print('That spell is '+str(guess))
        correctGuess.append(guess)

#reversed list so that when it removes the specific threads it isn't changing the length that the next thread to be removed is at 
revCorrectGuess=list(reversed(correctGuess))

Then I manually copied the output list and took out the bit to make my code a little shorter.

Full code

\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

As a beginner Pythonista, one of the easiest ways to get tips on writing Python is to run your script(s) through pep8. That said, here are some non-PEP8-related tips:

  • Avoid pre-processing constant data on every run. After deleting what I assume are non-applicable spells simply replace the original array with the output of print(spells).
  • Reusing variables like you use spells and need is a code smell - it's hard to read the code and understand immediately what the type is at any given point. Instead you can either use a separate variable to avoid confusion or (in the need case) reverse the random/int check:

    if need == 'r':
        […]
    else:
        try:
            spell_number = int(need)
        except ValueError:
            print("Arr or a number, dammit!")
    
  • randint "[returns] a random integer N such that a <= N <= b." So you should print(spells[randint(0, len(spells)-1)]).
  • Your code is screaming for a class like Spellbook to reuse within other code.
  • Ditto for a main function with some argparse magic to enable scripting.
  • An IDE like PyCharm will do spell checking for you (snarky slang not included in the last community edition).
\$\endgroup\$
2
  • \$\begingroup\$ An IDE like VSCode, (not sure about PyCharm) would also complain about how long some lines are. \$\endgroup\$
    – Luke
    Commented May 22, 2017 at 3:51
  • \$\begingroup\$ I didn't know IDE existed, argparse, or PEP8, thank you very much! Talk about game changers. I feel like I picked up this foreign technology then every time I work on it I get blasted into the future with 'But wait! There\'s more!' I'm not sure I understand what you mean by "code smell" however I can understand how your example of code is more readable. Thank you very much for the feedback :) it helps \$\endgroup\$ Commented May 22, 2017 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.