11 Replies - 494 Views - Last Post: 30 July 2014 - 07:43 AM Rate Topic: -----

#1 Yinna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Get random letters

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3812
  • View blog
  • Posts: 13,527
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 Yinna  Icon User is offline

  • New D.I.C Head

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

Re: Get random letters

Posted 30 July 2014 - 04:33 AM

View Postandrewsw, 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?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3812
  • View blog
  • Posts: 13,527
  • Joined: 12-December 12

Re: Get random letters

Posted 30 July 2014 - 04:54 AM

That looks to be heading in the right direction.
Was This Post Helpful? 0
  • +
  • -

#5 Yinna  Icon User is offline

  • New D.I.C Head

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

Re: Get random letters

Posted 30 July 2014 - 05:29 AM

View Postandrewsw, 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.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3812
  • View blog
  • Posts: 13,527
  • 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)

Was This Post Helpful? 1
  • +
  • -

#7 Yinna  Icon User is offline

  • New D.I.C Head

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

Re: Get random letters

Posted 30 July 2014 - 05:47 AM

View Postandrewsw, 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. :D
Was This Post Helpful? 0
  • +
  • -

#8 Yinna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3812
  • View blog
  • Posts: 13,527
  • 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.
Was This Post Helpful? 1
  • +
  • -

#10 Yinna  Icon User is offline

  • New D.I.C Head

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

Re: Get random letters

Posted 30 July 2014 - 06:44 AM

View Postandrewsw, 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

Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3812
  • View blog
  • Posts: 13,527
  • 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.
Was This Post Helpful? 1
  • +
  • -

#12 Yinna  Icon User is offline

  • New D.I.C Head

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

Re: Get random letters

Posted 30 July 2014 - 07:43 AM

View Postandrewsw, on 30 July 2014 - 06:51 AM, said:

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

Oh, I see. Thank you. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1