can you do random.choice with functions? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

can you do random.choice with functions?

Hey Everyone, Would someone be able to look at my code and let me know what I'm doing wrong? I'm trying to randomly print either a variable or a function from a list. When the function gets called it comes out as a hex. Any help is appropriated Also if anyone sees anything in the code that should be changed, please let me know. Thank you! The codes also in this link: https://code.sololearn.com/c9a13A15A5a1 import random def num_guess(): number = random.randrange(1, 20) print(number) print("Guess a number!") guess = int(input()) def divisible(guess, number): if guess % number == 0: print("You're guess was divisable by the number") else: return False between = ("The number is between " + str(number - 2) + " and " + str(number + 2)) multiable = "hint2" hint = [between, divisible] score = 100 while (number != 'guess'): if score < 50: print("You loose!") break elif guess == number: print("You guessed the number! \n") break elif guess < number: print("You guessed low") print(random.choice(hint)) score -= 10 guess = int(input()) else: print("You guessed high") print(random.choice(hint)) score -= 10 guess = int(input()) print("You're score is: " + str(score)) num_guess()

6th Mar 2021, 1:52 PM
David
David - avatar
19 Answers
+ 5
You aren't actually calling function , for that it needs to called using () otherwise it is returning reference to the function . Instead you can do it like , if random.choice(hint) ==divisible: print(divisible(guess, number) else: print(between)
6th Mar 2021, 2:29 PM
Abhay
Abhay - avatar
+ 5
In Python, a function name can be used like any other variable name. For example you can put it into a list. And this list, you can pass to choice, just like any other iterable. Then, you only need to call, what you randomly chose, with your arguments. from random import choice def add(a, b): return a+b def sub(a, b): return a-b print(choice([add, sub])(5, 7))
7th Mar 2021, 12:28 AM
HonFu
HonFu - avatar
+ 2
fixed and improved while loop: while (number != 'guess'): if score < 50: print("You loose!") break # break, so next code is implicitly wrapped in a else block if guess == number: # 'elif' works, but 'if' is enough print("You guessed the number! \n") break # break, so next code is implicitly wrapped in a else block if guess < number: # 'elif' works, but 'if' is enough print("You guessed low") else: print("You guessed high") # this will be executed in all non-break cases: h = random.choice(hint) # select the hint, so if function we call it with expected arguments print(h if type(h) is str else h(guess,number)) # python pseudo (shorthand) )ternary operator score -= 10 guess = int(input()) however, this code requires real time input/output, so cannot be run in sololearn code playground ;)
6th Mar 2021, 2:34 PM
visph
visph - avatar
+ 2
HonFu approach is much better than mine 🙏
7th Mar 2021, 12:51 AM
iTech
iTech - avatar
+ 1
Abhay thanks so much for responding! this helps a lot.
6th Mar 2021, 2:46 PM
David
David - avatar
+ 1
Abhay ive been working on what you said but i still dont think im doing it right. if i have the if statement outside of the loop, how would i call the random.choice? do i leave the “print(random.choice(hint))” in the while loop? i added my updated code in the solorlearn link.
6th Mar 2021, 4:02 PM
David
David - avatar
+ 1
you have some indentations errors because you set a master function governing all functions which is a threading approach. My question is: do u need a master function? Well for the sake of help, tell me exactly what is your purpose from this code and I am gonna code one. else here's is another approach to call functions from master function without indentation governing def allfunc(func): return func def double(x): return x*2 def tripple(x): return x*3 def mul(x): return x*x print(allfunc(double(2)))
6th Mar 2021, 4:49 PM
iTech
iTech - avatar
+ 1
David i forgot to say but what i meant is to use that " if else " loop in place of print(random.choice(hint))
6th Mar 2021, 4:54 PM
Abhay
Abhay - avatar
+ 1
Abhay thanks for your response. Really appreciate it!
6th Mar 2021, 5:11 PM
David
David - avatar
+ 1
# ok, here's a model code or template which will chose random function each time its executed but i don't advise you to use it coz its kinda hard to maintain, anyway, hope it helps you towards your objective. import random x = int(input("enter a number: ")) def allfunc(func): f = random.randrange(len(func)) return func[f]() def double(x): return x*2 def tripple(x): return x*3 def mul(x): return x*x print(allfunc([double,tripple,mul]))
6th Mar 2021, 7:13 PM
iTech
iTech - avatar
+ 1
iTech sure! so once the code is run, its generates a random number the user has to guess. if the user inputs the wrong number, one of two hits will print along with another saying whether you guessed too high or too low. the user has 6 guesses. each wrong guess is 10 points off your 100 score. once the score reaches 40, the code quits saying you lost. thanks for taking a look!
6th Mar 2021, 10:54 PM
David
David - avatar
+ 1
iTech HonFu Thank you guys for your help!
7th Mar 2021, 7:46 PM
David
David - avatar
0
visph thanks a lot for fixing the while loop! really appreciate it.
6th Mar 2021, 2:47 PM
David
David - avatar
0
iTech thank you for your help. the purpose of the second function is because its the only way i can think of how to randomly call the hints. im just look for when the user inputs the wrong number, random.choice is called and prints one of the two hints. i also updated my code in the above sololearn link so it does run. but if theres a differnt way i should have it please let me know. thanks again.
6th Mar 2021, 5:06 PM
David
David - avatar
0
iTech thank you!!
6th Mar 2021, 10:12 PM
David
David - avatar
0
ok gonna have a look in your code but first i need a description of what exactly your code do? consider me a dumb user and explain what ur code do
6th Mar 2021, 10:27 PM
iTech
iTech - avatar
0
#UPDATE with *args to make it flawless, you can adjust it to fit strings by setting if/else block. The difference between my code and HonFu code is that I am governing all functions from one function. So you have 2 great options : mine and @HonFu method. Thank you as well, because of you I optimized my technique. import random def allfunc(func, *args): f = random.randrange(len(func)) return func[f](*args) def double(x): return x*2 def tripple(x): return x*3 def mul(x): return x*x print(allfunc([double,tripple,mul],(7))) print(allfunc([double,tripple,mul],(91)))
7th Mar 2021, 9:45 PM
iTech
iTech - avatar
0
Hi , didn't add an exception in case the user CLICKS SUBMIT without entering any number.
8th Mar 2021, 11:18 AM
manu13 Rap
manu13 Rap - avatar
0
In line 17 I think your wronk is (number! ='guess' ) you can just write (number! =guess)
9th Mar 2021, 6:55 AM
مبشر موس ابراهيم ابكر
مبشر موس ابراهيم ابكر - avatar