9 Replies - 338 Views - Last Post: 30 October 2013 - 10:55 AM Rate Topic: -----

#1 NikBab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-October 13

[Me=Amateur]Strings and lists/arrays in python and their manipulations

Posted 28 October 2013 - 10:24 PM

THIS IS NOT A REQUEST TO DO MY HOMEWORK FOR ME



I'll start with saying that I have a little experience with learning C in high school classes, but otherwise a complete amateur. My current university CS course teaches C as a way to show what does what and along with C every homework assignment has a program in python needed as well. Python is expected to be learned by oneself, which would not be a problem by itself, but I can't find a decent tutorial that does not throw heavy technical lingo from the first page onward.

My homework(that dreadful word...!) requires me to build a simple dictionary like program - continuous input of words, while checking first letters(or second etc... if first is same) and putting them in the proper order in an array/list/whatever it's called in python, according to the order of the words. The second part of it is checking if a word is in the dictionary and printing it or a message saying otherwise if so.

So, to the actual request: a proper tutorial would be helpful, but for now I'd just like the codewords/commands for:

1. Checking the letters of a string, specifically the first one.
2. Placing the strings in a list according to the order I want them to go.
3. Checking if a value exists in list or not(I might have an idea about that, but I'd prefer to leave guesswork to astrologers).

Explanations on how each is used in various situations are welcomed warmly. Pointing to a simple tutorial along with the above will be gratefully accepted.

Would it help my chances if I asked please with a sugar on top?

Is This A Good Question/Topic? 0
  • +

Replies To: [Me=Amateur]Strings and lists/arrays in python and their manipulations

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3369
  • View blog
  • Posts: 11,410
  • Joined: 12-December 12

Re: [Me=Amateur]Strings and lists/arrays in python and their manipulations

Posted 29 October 2013 - 03:22 AM

I've read through your post a few times and it continues to puzzle me. Python is orders of magnitude simpler than C, and IMO, any tutorial will cover the basics you need to complete this assignment.

Try tutorialspoint.com, Python: The Hard Way or click the Tutorials link at the top of this page. I haven't provided any links as it is simple enough for you to search.
Was This Post Helpful? 1
  • +
  • -

#3 NikBab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-October 13

Re: [Me=Amateur]Strings and lists/arrays in python and their manipulations

Posted 29 October 2013 - 11:04 AM

A slightly different question then:

I get a syntax error from the following code about the inWord variable. What exactly is wrong with it?

#the list where I will place the words
word_list = []
#the variable for the words, placed with a _
#so I won't have to place input outside loop
word = "_"
#a while loop to receive input into 'word' variable
#and append it to the list
while word != 'STOP':
    word = raw_input("enter word into dictionary\n")
    #the word is entered into the list in lowercase,
    #to aid in the sorting process later
    word_list.append(word.lower()
#using the sorted() function to make a new list,
#sorted by alphabet precedence
inWord = raw_input("enter name to look in dictionary\n")
dict_list = sorted(word_list)
#checking with an if statement whether
#the word is in the dictionary or not
#and printing the required output
if word.lower() in word_list:
    print "found word in list at index at " + word_list.index(word.lower() + "\n")
    print "found word in sorted list at index " + dict_list.index(word.lower() + "\n")
    else
    print "did not find word"


Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3369
  • View blog
  • Posts: 11,410
  • Joined: 12-December 12

Re: [Me=Amateur]Strings and lists/arrays in python and their manipulations

Posted 29 October 2013 - 11:15 AM

What error? Post the full error details - they are there for a reason and contain important information.

In the meantime, you only have one line that uses inWord:

inWord = raw_input("enter name to look in dictionary\n")

I can't see how the error would come from here, unless you are using Python 3 (which you should really be doing).

This post has been edited by andrewsw: 29 October 2013 - 11:12 AM

Was This Post Helpful? 0
  • +
  • -

#5 NikBab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-October 13

Re: [Me=Amateur]Strings and lists/arrays in python and their manipulations

Posted 29 October 2013 - 03:01 PM

Due to the preferences of the teachers, we are using 2.7 only.

And it just says syntax error and highlights inWord in red - nothing specific.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3369
  • View blog
  • Posts: 11,410
  • Joined: 12-December 12

Re: [Me=Amateur]Strings and lists/arrays in python and their manipulations

Posted 29 October 2013 - 03:18 PM

You are missing a bracket on the previous line:

word_list.append(word.lower()

Was This Post Helpful? 1
  • +
  • -

#7 NikBab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-October 13

Re: [Me=Amateur]Strings and lists/arrays in python and their manipulations

Posted 29 October 2013 - 11:37 PM

And in a lot more apparently. I know that amateur mistakes are called such because they are made by amateurs, but this is bad even by my standards of noobyness.

Okay so now that I fixed the bracketing issues, it tells me something is wrong with the syntax of the 'else'.

#the list where I will place the words
word_list = []
#the variable for the words, placed with a _
#so I won't have to place input outside loop
word = "_"
#a while loop to receive input into 'word' variable
#and append it to the list
while word != 'STOP':
    word = raw_input("enter word into dictionary\n")
    #the word is entered into the list in lowercase,
    #to aid in the sorting process later
    word_list.append(word.lower())
#using the sorted() function to make a new list,
#sorted by alphabet precedence
inWord = raw_input("enter name to look in dictionary\n")
dict_list = sorted(word_list)
#checking with an if statement whether
#the word is in the dictionary or not
#and printing the required output
if inWord.lower() in word_list:
    print "found word in list at index at " + word_list.index(inWord.lower()) + "\n"
    print "found word in sorted list at index " + dict_list.index(inWord.lower()) + "\n"
    else
    print "did not find word"



Again, no explanation is given, just syntax error and highlights else in red.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3369
  • View blog
  • Posts: 11,410
  • Joined: 12-December 12

Re: [Me=Amateur]Strings and lists/arrays in python and their manipulations

Posted 29 October 2013 - 11:59 PM

Yes, the syntax is wrong. If you look up the if-else statement you should be able to work it out. It is at the very core of the language.
Was This Post Helpful? 1
  • +
  • -

#9 NikBab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-October 13

Re: [Me=Amateur]Strings and lists/arrays in python and their manipulations

Posted 30 October 2013 - 09:52 AM

Do you know somewhere discreet where I could bury myself? Missing that : was embarrassing.

Unless I messed up something else?
Was This Post Helpful? 0
  • +
  • -

#10 Papillon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 30-October 13

Re: [Me=Amateur]Strings and lists/arrays in python and their manipulations

Posted 30 October 2013 - 10:55 AM

You forgot indentation. One of the first things to learn when learning python :)/>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1