0

Can a random.choice be made into a variable?

Can you do something like a = random.choice() and when you need to fill the parameters you can do something llike if b == 5 a = random.choice(c)

7th Nov 2018, 9:13 PM
Fbia34
Fbia34 - avatar
9 Antworten
+ 3
random.choice is a function that returns what it chooses from the iterable. So yes, you can store that choice and use it later.
7th Nov 2018, 11:03 PM
HonFu
HonFu - avatar
+ 5
In case you don't want to use such a long word, this is also valid: import random a = random.choice print(a([1, 7, 3])) Instead of calling the function, we're just giving it another name. But if that is your goal, I'd recommend from random import choice as a print(a([1, 7, 3]))
8th Nov 2018, 3:45 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 3
Yes you can, why dont you try try it in code playground ? its faster than waiting for someone to answer
7th Nov 2018, 9:33 PM
Taste
Taste - avatar
+ 1
Fbia34 the method 'choice' from random, expects a sequence argument, so if you do this: a = random.choice() you will get an error telling you that.
7th Nov 2018, 10:44 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 1
Fbia34 The problem with your only code is not that you used random.choice wrongly, but you are missing one comma. here i corrected for you: https://code.sololearn.com/cbU63AFo2OeZ/?ref=app
8th Nov 2018, 4:12 AM
Gordon
Gordon - avatar
+ 1
Fbia34 Welcome😉 try code more basketballgirl's 800 upvote game is by random.choice you can do it too~
8th Nov 2018, 4:16 AM
Gordon
Gordon - avatar
+ 1
╰☆☆ S͎p͎e͎a͎r͎y͎ ☆☆╮ that's actually what this question is for, it's just a variation instead of earth it's space
8th Nov 2018, 4:17 AM
Fbia34
Fbia34 - avatar
0
╰☆☆ S͎p͎e͎a͎r͎y͎ ☆☆╮ oh this isn't for the art gen (which I made for a friend who said it worked fine) but thanks lol I didn't catch that
8th Nov 2018, 4:14 AM
Fbia34
Fbia34 - avatar
0
Fbia34 heres mine from the halloween contest: https://code.sololearn.com/c22kNFrS19ds/?ref=app edit: this is made for ur question. Purrfect debug, withit
8th Nov 2018, 4:19 AM
Potato Hacker
Potato Hacker - avatar