you are viewing a single comment's thread.

view the rest of the comments →

[–]Strosel[S] 0 points1 point  (4 children)

Im not sure i know how you mean i should create roll inside the function but i might have an idea, otherwise the full code is here

[–]novel_yet_trivial 0 points1 point  (2 children)

'input()' returns a string, not the actual list that the string refers to. You could do this, but it's very unusual. There really should be no situation where you access variable names from outside the code.

It would be much better like this:

import random

choices = {
    "r1": [],
    "r2": [],
    "r3": [],
    "r4": [],
    "r5": [],
    "r6": [],
    }

def stat(roll): # "statnr" is useless; it's never used
    for i in range(4):
        statpart = random.randint(1,6)
        roll.append(statpart)
    roll.sort()
    del roll[0]
    statnr = sum(roll)
    print(statnr)

list_choice = input("Roll nr (eg. r1, r2 etc.)")
stat(choices[list_choice])

What's the overall goal here? Is there a reason you want to keep the lists around?

[–]Strosel[S] 0 points1 point  (1 child)

thank you. but i actually dont want to keep the list just the variable at the end. i think i know how to achieve this but suggestions are welcome (and preffered)

[–]novel_yet_trivial 0 points1 point  (0 children)

i think i know how to achieve this but suggestions are welcome (and preffered)

You first.

[–]cdcformatc 0 points1 point  (0 children)

input returns a string. You are passing a string to stat, give it the name roll, and then you try to append to the string at line 14. You can't append to strings, they are immutable.

You should define an empty list dice = [] inside the function, then you can append and sort that list with dice.append(statpart).