for:

import random
keys='abcdefghijklmnopqrstuvwxyz '
random.choice(keys)

How would I count how many times it takes random.choices(keys) to generate "m"?

Recommended Answers

All 5 Replies

do u mean like:

import random
summy=0
keys='abcdefghijklmnopqrstuvwxyz '

for i in range(100):
    y=random.choice(keys)
    if y!='m':
        summy+=1
    elif y == 'm':
        continue
print summy

Perhaps not the best way to do it, but maybe something like this?

import random

tries = 0
keys='abcdefghijklmnopqrstuvwxyz '
key = random.choice(keys)

while key != "m":             #While key is not equal to 'm'
    tries += 1                #Add one to tries
    key = random.choice(keys) #Choose another key

if key == "m":                #Key 'm' was chosen
    print "Tried %s times until we got m." % tries
commented: geat post +1

Perhaps not the best way to do it, but maybe something like this?

import random

tries = 0
keys='abcdefghijklmnopqrstuvwxyz '
key = random.choice(keys)

while key != "m":             #While key is not equal to 'm'
    tries += 1                #Add one to tries
    key = random.choice(keys) #Choose another key

if key == "m":                #Key 'm' was chosen
    print "Tried %s times until we got m." % tries

Yes! Exactly like that! Thank you so much! yahoo!

You're very welcome.

Thanks for the correction ..er.. I mean direction no suggestion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.