[Tutor] help

Alan Gauld alan.gauld at yahoo.co.uk
Sat Oct 31 12:19:18 EDT 2020


On 31/10/2020 14:50, VISHAL GAUR wrote:
> Please tell me some suggestions for this problem, I have tried this code
> but it is not giving me the correct output...

It might help if you told us what output it was giving
and why you believe it is wrong.

> *Problem*: Use Python to calculate how many different passwords can be
> formed with 6 lower case English letters. For a 1 letter password, there
> would be 26 possibilities. For a 2 letter password, each letter is
> independent of the other, so there would be 26 times 26 possibilities.

So lets tabulate that:

1 place  -> 26
2 places -> 26*26
3 places -> 26*26*26
4 places -> 26*26*26*26
etc...

> pass_probability = 1
> for  i in range(1,7):
>     pass_probability = pass_probability * i * 26

What does your for loop do?
It sets i to 1,2,3,4,5,6.
So the calculations go:

pass_probability = 1
i = 1
pass_probability = 1 * 1 * 26  -> 26   # ok so far
i = 2
pass_probability =  26 * 2 * 26 -> 1352   # broken already!
i = 3
pass_probability = 1352 * 3 * 26  ... Can you see where its going wrong?

There are at least 2 ways to fix this.
Hint: One of them does not even require a for loop!


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list