# Get random letters

Page 1 of 1

## 11 Replies - 1584 Views - Last Post: 30 July 2014 - 07:43 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=351290&amp;s=eddf34e6147761deae368f295736a3b1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Yinna

Reputation: 0
• Posts: 7
• Joined: 30-July 14

# Get random letters

Posted 30 July 2014 - 04:11 AM

Hello all,

I am quite new to Python and I need to code something, but I am not really sure how to make it. I have few ideas, but I don't really know how to write the syntax.

The problem is as it follows:
You have a Python function getRandomLetter that returns a random letter of the alphabet. Call the function two times - count the number those calls that result in a vowel to see if you get two vowels in a row. Repeat THAT process 100,000 times and count the number of times two vowels were selected in a row.

HINT: Use the in operator to determine if a character is in a string:
If getRandomLetter() in 'AEIOU':

Well I thought of this for the start:

```>>> import random
>>> import string
>>> def getRandomLetter():
random.choice(string.ascii_letters)

>>> for i in range(2):
getRandomLetter()
#I should compare the two letters and see if both are vowels. I thought of something like this.
if getRandomLetter in 'AEIOU':
x=x+1
#x being the number of times I need to call the program until it gives me two consecutive vowels

>>> for i in range(100000):
getRandomLetter()
#Here I need to see how many consecutive vowels there are after 100000 calls.
if getRandomLetter() in 'AEIOU':
y=y+1
#y being the number of consecutive vowels there are and in the end it should show the total of them after the 100000 calls.

```

The problem is that I don't really know how to see if there are two vowels in a row and how many of these 2 consecutive vowels there are after I call the program 100,000 times.
As I said, I have the ideas, but I don't really know how to write the syntax.

Is This A Good Question/Topic? 0

## Replies To: Get random letters

### #2 andrewsw

• blow up my boots

Reputation: 6541
• Posts: 26,484
• Joined: 12-December 12

## Re: Get random letters

Posted 30 July 2014 - 04:20 AM

I have changed your topic title. "Need help coding" is useless as a title, everyone could use this as a title and it tells us nothing about your question.

Your getRandomLetter function should return a value, and you will need to store the returned value in a variable. You need two such variables.

### #3 Yinna

Reputation: 0
• Posts: 7
• Joined: 30-July 14

## Re: Get random letters

Posted 30 July 2014 - 04:33 AM

andrewsw, on 30 July 2014 - 04:20 AM, said:

I have changed your topic title. "Need help coding" is useless as a title, everyone could use this as a title and it tells us nothing about your question.

Your getRandomLetter function should return a value, and you will need to store the returned value in a variable. You need two such variables.

So I should do something like:
```<<< for i in range(2):
a=getRandomLetter()
b=getRandomLetter()
#And for the 100000 calls, then they should be halfed I guess.
```

So, with that, can I compare a and b as follows?
```<<< if a in 'AEIOU' and b in 'AEIOU':
y=y+1
#y being the number of consecutive vowels
```

Also, did I write the syntax correctly?

### #4 andrewsw

• blow up my boots

Reputation: 6541
• Posts: 26,484
• Joined: 12-December 12

## Re: Get random letters

Posted 30 July 2014 - 04:54 AM

That looks to be heading in the right direction.

### #5 Yinna

Reputation: 0
• Posts: 7
• Joined: 30-July 14

## Re: Get random letters

Posted 30 July 2014 - 05:29 AM

andrewsw, on 30 July 2014 - 04:54 AM, said:

That looks to be heading in the right direction.

It seems to be working fine, thanks a lot. Just a little problem though.
When I use the function as it is
```>>> def getRandomLetter():
random.choice(string.ascii_letters)
```

I don't get any letters for the variables a and b.
```<<< for i in range(2):
a=getRandomLetter()
b=getRandomLetter()
```

But when I try without the function, like:
```<<< for i in range(2):
a=random.choice(string.ascii_letters)
b=random.choice(string.ascii_letters)
```

it works. Any idea why is it so? The assignment says I need to use that function but I don't really know why it doesn't work like that.

### #6 andrewsw

• blow up my boots

Reputation: 6541
• Posts: 26,484
• Joined: 12-December 12

## Re: Get random letters

Posted 30 July 2014 - 05:41 AM

You need to return the value from your function:
```    return random.choice(string.ascii_letters)
```

### #7 Yinna

Reputation: 0
• Posts: 7
• Joined: 30-July 14

## Re: Get random letters

Posted 30 July 2014 - 05:47 AM

andrewsw, on 30 July 2014 - 05:41 AM, said:

You need to return the value from your function:
```    return random.choice(string.ascii_letters)
```

Everything works now. Thanks a lot.

### #8 Yinna

Reputation: 0
• Posts: 7
• Joined: 30-July 14

## Re: Get random letters

Posted 30 July 2014 - 06:24 AM

One more question, if I may.
Can I make
```>>> for i in range(2):
a=getRandomLetter()
b=getRandomLetter()
if a and b in 'AEIOU':
x=x+1

```

to repeat until x=1?

### #9 andrewsw

• blow up my boots

Reputation: 6541
• Posts: 26,484
• Joined: 12-December 12

## Re: Get random letters

Posted 30 July 2014 - 06:28 AM

The break statement is used to break out of a loop.

### #10 Yinna

Reputation: 0
• Posts: 7
• Joined: 30-July 14

## Re: Get random letters

Posted 30 July 2014 - 06:44 AM

andrewsw, on 30 July 2014 - 06:28 AM, said:

The break statement is used to break out of a loop.

Is this alright? It doesn't seem to work.
```>>> repeat=0
>>> x=0
>>> while repeat:
x=x+1
for i in range(2):
a=getRandomLetter()
b=getRandomLetter()
if a and b in 'AEIOU':
repeat=1
break
```

### #11 andrewsw

• blow up my boots

Reputation: 6541
• Posts: 26,484
• Joined: 12-December 12

## Re: Get random letters

Posted 30 July 2014 - 06:51 AM

As repeat is 0 that while-loop is never entered.

### #12 Yinna

Reputation: 0
• Posts: 7
• Joined: 30-July 14

## Re: Get random letters

Posted 30 July 2014 - 07:43 AM

andrewsw, on 30 July 2014 - 06:51 AM, said:

As repeat is 0 that while-loop is never entered.

Oh, I see. Thank you.