python - How to choose a random input from 7 given input? -
i'm creating lottery hack machine gets winning numbers of last 7 days , tries select winner the 7 , shuffle numbers of selection , print result. occurs @ random.
#lottery hack print "welcome lottery hack machine!!!\n" today = int(raw_input( "please enter today's date: " )) if today<=31: print "please enter 4-digit prize winning lottery number last 7 days" y = raw_input( "enter 7 numbers separated commas: " ) input_list = y.split(',') numbers = [float(x.strip()) x in input_list] elif today>31: print "a month has 31 days ;p"
you can use random.choice
function this. returns random element sequence pass it.
import random print "welcome lottery hack machine!!!\n" today = int(raw_input( "please enter today's date: " )) if today<=31: print "please enter 4-digit prize winning lottery number last 7 days" y = raw_input( "enter 7 numbers separated commas: " ) input_list = y.split(',') numbers = [float(x.strip()) x in input_list] print random.choice(numbers) elif today>31: print "a month has 31 days ;p"
if want shuffle entire list in place instead of printing random elements 1 @ time, can use random.shuffle
function.
import random print "welcome lottery hack machine!!!\n" today = int(raw_input( "please enter today's date: " )) if today<=31: print "please enter 4-digit prize winning lottery number last 7 days" y = raw_input( "enter 7 numbers separated commas: " ) input_list = y.split(',') numbers = [float(x.strip()) x in input_list] random.shuffle(numbers) print numbers elif today>31: print "a month has 31 days ;p"
as clarified in comments, need approach combines these 2 approaches.
import random print "welcome lottery hack machine!!!\n" today = int(raw_input( "please enter today's date: " )) if today<=31: print "please enter 4-digit prize winning lottery number last 7 days" y = raw_input( "enter 7 numbers separated commas: " ) input_list = y.split(',') numbers = [list(x.strip()) x in input_list] choice = random.choice(numbers) random.shuffle(choice) print ''.join(choice) elif today>31: print "a month has 31 days ;p"
Comments
Post a Comment