Need help coding

This is the place for queries that don't fit in any of the other categories.

Need help coding

Postby Yinna » Wed Jul 30, 2014 7:23 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':

Thanks in advance.
Yinna
 
Posts: 3
Joined: Wed Jul 30, 2014 7:19 am

Re: Need help coding

Postby metulburr » Wed Jul 30, 2014 8:08 am

I have few ideas, but I don't really know how to write the syntax

Why dont you provide the code you tried, as your question looks like a school question. We can help tweak it to perfect the guidelines. If it is for school, the professor would provide you with enough tools to write the code for a given assignment.
Please read this, especially the homework section
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1512
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Need help coding

Postby Yinna » Wed Jul 30, 2014 8:44 am

metulburr wrote:
I have few ideas, but I don't really know how to write the syntax

Why dont you provide the code you tried, as your question looks like a school question. We can help tweak it to perfect the guidelines. If it is for school, the professor would provide you with enough tools to write the code for a given assignment.
Please read this, especially the homework section


Well I thought of this for the start:

Code: Select all
>>> 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.
Yinna
 
Posts: 3
Joined: Wed Jul 30, 2014 7:19 am

Re: Need help coding

Postby Yinna » Wed Jul 30, 2014 12:48 pm

I made it after all.
For anyone interested, this is the code:
Code: Select all
>>> import random
>>> import string
>>> x=0
>>> y=0
>>> def getRandomLetter():
      return random.choice(string.ascii_letters)

   
>>> for i in range(2):
   a=getRandomLetter()
   b=getRandomLetter()
   if a and b in 'AEIOU':
      x=x+1

>>>print(a,b,x)
      
>>> for i in range(50000):
   a=getRandomLetter()
   b=getRandomLetter()
   if a and b in 'AEIOU':
      y=y+1

      
>>> print(y)
Yinna
 
Posts: 3
Joined: Wed Jul 30, 2014 7:19 am

Re: Need help coding

Postby metulburr » Wed Jul 30, 2014 3:31 pm

congrats...

You are aware that you can use files to store source code and not write that much code in the interpreter?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1512
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Need help coding

Postby Kebap » Thu Jul 31, 2014 8:58 am

Hello Yinna, welcome to Python!

Yinna wrote:As I said, I have the ideas, but I don't really know how to write the syntax.

You can ignore the syntax and write some pseudo-code first. We can then read your logic and help you with the missing syntax where you still need help. Often times the main problem is first to get the logic straight, no matter which coding language or syntax will be used in the end.

Code: Select all
if a and b in 'AEIOU':

I am afraid, this will not work as expected.

A solution and explanation for a similar problem is in the thread metulburr linked for you.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 400
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Need help coding

Postby Marbelous » Thu Jul 31, 2014 5:04 pm

Marbelous
 
Posts: 155
Joined: Fri May 31, 2013 8:12 pm


Return to General Coding Help

Who is online

Users browsing this forum: l_mono, stranac and 5 guests