简体   繁体   中英

Python3 Python 3 how to use result from randint to evaluate an array and pull the correct choice

I am using Python3.

I am trying to evaluate "skill checks" for my game, and I want to create a function, that can be evluated for all, standard skill rolls.

I'm only at the most basic level for now, so, I'm just trying to create the function itself, then later, I want to use my database, but for now:

  1. My game uses success "thresholds", which,
  2. when I'm rolling dice (it was a pen-n-paper game, but, handling the dice rolls takes forever), takes the results -
  3. compares it to a range of numbers, and tells you how well you failed/succeeded.

Desired flow:

  1. 1-100 is rolled.
  2. [ignored for this question]: if a 96+ is rolled, roll again and add the results.
  3. List/Array/Dictionary/Whatever (later DataBase) is checked and the appropriate type of success is generated.

What I don't know are:

  1. How do I build this list? Should this be a dictionary, a list, or a tuple?
  2. What do I do once I have generated a random number? What do I do with that number in order to evaluate my list of thresholds?

Thresholds and their Ranges:

Total Failure: 1-25,
Partial Failure: 26-49,
Partial Success: 50-74,
Success: 75-100,
Significant Success: 101-149,
Astounding Success: 150-199,
Epic Success: 200-249,
Legendary: 250+

I would not use a data structure but a function.

def type_of_success(n):
    # input: integer random number rolled
    # output: string

    if not isinstance(n, int) or n < 1:
        raise Exception('Not a valid number')

    if n < 26:
        return 'Total Failure'
    elif n < 51:
        return 'Partial Failure'
    # etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM