I didnt rtfm - help!

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

I didnt rtfm - help!

Postby Sonjam » Sun Apr 28, 2013 11:55 am

Hello! I'm a total beginner who's writing my first Python program. It's based on the chinese astrological calendar. The calendar consists of 12 animals and 5 elements and is divided into cycles of 60 years, in which the animal changes every year and the element changes every other year. The user is supposed to be able to find out his/her animal and element plus get a matching personality description. I have to use all these files, which is really frustrating.. Anyhow, I've had problems with this one method for weeks, one that is supposed to return the personality description. The file I have to use looks like this:

Metal Boar - 1911, 1971, 2051

Followed by a personality description:

"A proud, passionate Boar with overpowering sentiments, who values his
reputation. Intense and more dominating than others, this type of Boar
often has excessive appetites and could lack refinement or tact..."

Now, say the user is a Boar. There are five Boars in the Boar-file, since there are five elements. Based on the input of the user, I have to FIND the personality description (a chunk of text) that comes after the "Element Animal, xxxx, xxxx, xxxx" - up until the next "Element Animal, xxxx, xxxx, xxxx". This is what I've done so far:


Code: Select all
    def personalitydescription (self):
        # Provides a personality description based on the user's element and animal.
        djur = input("What is your animal?")
        element = input("What is your element?")
        """Creates a list that opens all twelve animal files."""   
        animals = ["rat", "ox", "tiger", "rabbit", "dragon", "snake", "horse", "sheep", "monkey", "rooster", "dog", "boar"]
        for animal in animals:
            file = open("5_" + animal + ".txt", "r")
        # Finds the string "-" followed by year(s) of birth. Finds the element and animal occuring before the string "-".     
        for animal in file:
            if "-"  ...       


I have no idea how to write my for / if in order to return the relevant personality description.
Could somebody please help me? :roll:
Last edited by Sonjam on Mon Apr 29, 2013 11:35 am, edited 1 time in total.
Sonjam
 
Posts: 7
Joined: Sun Apr 28, 2013 11:10 am

Re: Horoscope programme - help!

Postby Yoriz » Sun Apr 28, 2013 12:12 pm

You cant have done the tutorial that is in the python documentation yet then
http://docs.python.org/2/tutorial/controlflow.html#if-statements
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 836
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Horoscope programme - help!

Postby Sonjam » Sun Apr 28, 2013 8:00 pm

Yoriz wrote:You cant have done the tutorial that is in the python documentation yet then
http://docs.python.org/2/tutorial/controlflow.html#if-statements


Well, actually this isn't a regular if-statement (at least for me, it isn't), and I find it quite difficult to search in the tutorial, that's why I'm asking. I wish it was just an if/elif/else.
Sonjam
 
Posts: 7
Joined: Sun Apr 28, 2013 11:10 am

Re: I didnt rtfm - help!

Postby Yoriz » Sun Apr 28, 2013 8:37 pm

Your question said you have no idea how to write if and you code showed no attempt to use if, so it made it sound like you had never used if before, but if the case is that you do know what an if statement is but you want to find a cut & paste solution to using if in your own code, then you are not likely to find that as your code is only relevant to how you have coded it , unless you find someone that has coded your problem in exactly the same way.

I don't really understand what you are asking for as there is only a regular if-statement.
i noted that in your code you are opening animal files in a loop but only storing the result in a variable named file so this is just overwritten in each loop, should this be a list instead as you are trying to iterate over it in the next lines.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 836
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: I didnt rtfm - help!

Postby Kebap » Mon Apr 29, 2013 10:38 am

Sonjam wrote:I didnt rtfm (...) which is really frustrating (...) :( Haven't found this information anywhere. Could somebody please help me? :roll:

Yeah, right? :) Did you ever program before, maybe in another language than Python? I too don't exactly understand what kind of help you would expect from us now. Maybe do read some basic tutorial and start with easy problems to get a general overview. If you don't like reading, I loved the video series named Google Python Course or something.
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: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: I didnt rtfm - help!

Postby Sonjam » Mon Apr 29, 2013 11:29 am

Kebap wrote:
Sonjam wrote:I didnt rtfm (...) which is really frustrating (...) :( Haven't found this information anywhere. Could somebody please help me? :roll:

Yeah, right? :) Did you ever program before, maybe in another language than Python? I too don't exactly understand what kind of help you would expect from us now. Maybe do read some basic tutorial and start with easy problems to get a general overview. If you don't like reading, I loved the video series named Google Python Course or something.


No, I have never programmed before, but this is my task and I have to do it. If you can't help me, then that's fine. I'm not doing this because I think it's an awesome idea.
Perhaps I didn't express myself clearly: the if-statement is for finding the relevant Element + Animal and the personality description that follows. There are five different texts in each file. So basically, I need the program to return the personality description IF the element and animal matches the user's input.
Sonjam
 
Posts: 7
Joined: Sun Apr 28, 2013 11:10 am

Re: I didnt rtfm - help!

Postby Sonjam » Mon Apr 29, 2013 11:39 am

Yoriz wrote:Your question said you have no idea how to write if and you code showed no attempt to use if, so it made it sound like you had never used if before, but if the case is that you do know what an if statement is but you want to find a cut & paste solution to using if in your own code, then you are not likely to find that as your code is only relevant to how you have coded it , unless you find someone that has coded your problem in exactly the same way.

I don't really understand what you are asking for as there is only a regular if-statement.
i noted that in your code you are opening animal files in a loop but only storing the result in a variable named file so this is just overwritten in each loop, should this be a list instead as you are trying to iterate over it in the next lines.


I do know what an if-statement is, I've used them many times. I just don't know what do do with it in this method. :D I edited my first post. (:
I'm actually not sure if it should be a list, but I could use readlines() on it, I guess.
Sonjam
 
Posts: 7
Joined: Sun Apr 28, 2013 11:10 am

Re: I didnt rtfm - help!

Postby Sonjam » Mon Apr 29, 2013 11:43 am

Sonjam wrote:
Yoriz wrote:Your question said you have no idea how to write if and you code showed no attempt to use if, so it made it sound like you had never used if before, but if the case is that you do know what an if statement is but you want to find a cut & paste solution to using if in your own code, then you are not likely to find that as your code is only relevant to how you have coded it , unless you find someone that has coded your problem in exactly the same way.

I don't really understand what you are asking for as there is only a regular if-statement.
i noted that in your code you are opening animal files in a loop but only storing the result in a variable named file so this is just overwritten in each loop, should this be a list instead as you are trying to iterate over it in the next lines.


I do know what an if-statement is, I've used them many times. I just don't know what do do with it in this method. :D I edited my first post. (:
I was thinking that if I make the files into a list with readlines(), the personality description will be returned as a list. I actually don't know how that works either.
Sonjam
 
Posts: 7
Joined: Sun Apr 28, 2013 11:10 am

Re: I didnt rtfm - help!

Postby KHarvey » Mon Apr 29, 2013 2:45 pm

Sonjam wrote:
Kebap wrote:
Sonjam wrote:I didnt rtfm (...) which is really frustrating (...) :( Haven't found this information anywhere. Could somebody please help me? :roll:

Yeah, right? :) Did you ever program before, maybe in another language than Python? I too don't exactly understand what kind of help you would expect from us now. Maybe do read some basic tutorial and start with easy problems to get a general overview. If you don't like reading, I loved the video series named Google Python Course or something.


No, I have never programmed before, but this is my task and I have to do it. If you can't help me, then that's fine. I'm not doing this because I think it's an awesome idea.
Perhaps I didn't express myself clearly: the if-statement is for finding the relevant Element + Animal and the personality description that follows. There are five different texts in each file. So basically, I need the program to return the personality description IF the element and animal matches the user's input.


Can you post what one of your text files looks like?
Personally I would do something like this:
Code: Select all
animals = ["rat", "ox", "tiger", "rabbit", "dragon", "snake", "horse", "sheep", "monkey", "rooster", "dog", "boar"]
for animal in animals:
   with open("5_" + animal + ".txt", "r") as file:
      file_line = file.readline()
      if animal in file_line:
         print file_line


Using with open means that you don't have to close the file afterwards. Also this is assuming your text file looks something like this:
Code: Select all
rat  2008, 1996, 1984, 1972, 1960  Rat or Lao Shu has xxxxx

That means that I am looking for the word "rat" on each line of your text file, if it finds the word "rat" it displays the rest of the text.
KHarvey
 
Posts: 34
Joined: Tue Mar 19, 2013 5:13 pm
Location: US

Re: I didnt rtfm - help!

Postby Yoriz » Mon Apr 29, 2013 7:11 pm

Do you need to use files as it looks like you need to learn how to use them yet.
Have you used dictionary's, you could create a dictionary with a key that is a tuple of (element, animal)
see the following example
Code: Select all
METAL_BOAR = """Metal Boar - 1911, 1971, 2051

A proud, passionate Boar with overpowering sentiments, who values his
reputation. Intense and more dominating than others, this type of Boar
often has excessive appetites and could lack refinement or tact..."""

WOODEN_DONKEY = """Wooden Donkey - 1931 1968 2007

A splintered old donkey, with underwhelming attributes who values not getting
woodworms...
"""

calendar = {('Metal', 'Boar'): METAL_BOAR,
            ('Wooden', 'Donkey'): WOODEN_DONKEY}

element = 'Metal'
animal = 'Boar'
print calendar[(element, animal)]
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 836
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: I didnt rtfm - help!

Postby Sonjam » Thu May 02, 2013 9:04 am

Yoriz wrote:Do you need to use files as it looks like you need to learn how to use them yet.
Have you used dictionary's, you could create a dictionary with a key that is a tuple of (element, animal)
see the following example
Code: Select all
METAL_BOAR = """Metal Boar - 1911, 1971, 2051

A proud, passionate Boar with overpowering sentiments, who values his
reputation. Intense and more dominating than others, this type of Boar
often has excessive appetites and could lack refinement or tact..."""

WOODEN_DONKEY = """Wooden Donkey - 1931 1968 2007

A splintered old donkey, with underwhelming attributes who values not getting
woodworms...


"""

calendar = {('Metal', 'Boar'): METAL_BOAR,
            ('Wooden', 'Donkey'): WOODEN_DONKEY}

element = 'Metal'
animal = 'Boar'
print calendar[(element, animal)]


Oh, thank you so much! I will try :D
Sonjam
 
Posts: 7
Joined: Sun Apr 28, 2013 11:10 am

Re: I didnt rtfm - help!

Postby Sonjam » Thu May 02, 2013 9:19 am

Code: Select all
Can you post what one of your text files looks like?
Personally I would do something like this:
[code]
animals = ["rat", "ox", "tiger", "rabbit", "dragon", "snake", "horse", "sheep", "monkey", "rooster", "dog", "boar"]
for animal in animals:
   with open("5_" + animal + ".txt", "r") as file:
      file_line = file.readline()
      if animal in file_line:
         print file_line
[/code]

Using with open means that you don't have to close the file afterwards.  Also this is assuming your text file looks something like this:
[code]
rat  2008, 1996, 1984, 1972, 1960  Rat or Lao Shu has xxxxx

That means that I am looking for the word "rat" on each line of your text file, if it finds the word "rat" it displays the rest of the text.


The file(s) I have to use look(s) like this:

Metal Boar - 1911, 1971, 2051

Followed by a personality description:

"A proud, passionate Boar with overpowering sentiments, who values his
reputation. Intense and more dominating than others, this type of Boar
often has excessive appetites and could lack refinement or tact..."

There are five different personality descriptions in EACH file, since there are five different elements.
Sonjam
 
Posts: 7
Joined: Sun Apr 28, 2013 11:10 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 7 guests