can you do random.choice with functions? | SoloLearn: Learn to code for FREE!

+4

# 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()

3/6/2021 1:52:49 PM

David

+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)

+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))

+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 ;)

+2

HonFu approach is much better than mine 🙏

+1

Abhay thanks so much for responding! this helps a lot.

+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.

+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)))

+1

David i forgot to say but what i meant is to use that " if else " loop in place of print(random.choice(hint))

+1

Abhay thanks for your response. Really appreciate it!

+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]))

+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!

+1

iTech HonFu Thank you guys for your help!

0

visph thanks a lot for fixing the while loop! really appreciate it.

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.

0

iTech thank you!!

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

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)))

0

Hi , didn't add an exception in case the user CLICKS SUBMIT without entering any number.

0

Yes you can

0

In line 17 I think your wronk is (number! ='guess' ) you can just write (number! =guess)