SoulMazer 26 Posting Whiz in Training

Thank you very much Murtan. Problem solved.

SoulMazer 26 Posting Whiz in Training

Thanks Murtan, but just one little question about your suggestion. So, so far I have changed line 8 to be "if answer in words.keys():". However, what would be the best way to check if the word also corresponds to the definition?

SoulMazer 26 Posting Whiz in Training

For reference, I am using Python 3.0. So, I have a small script I am working on trying to improve. I am now trying to create it with only dictionaries and loops.

My problem is that I keep getting a Key Error at one of my lines. The line with the error has an "Error" comment. Also, "words" is a dictionary. And yes, I struggle with dictionaries.
Code:

def vocabtest():
    for r in words.values():
        count = 0
        while count < 3:
            print("What word does the following definition correspond with?")
            print(r)
            answer = input("> ")
            if answer == words[r]: #Error
                print("Correct!")
                count = 100000
            elif count < 2:
                count = count + 1
                print("That is incorrect. Try again.")
            elif count == 2:
                count = count + 1
                print("That is incorrect. The answer was",str(words[r]) + ".")
                fail[r] = words[r]

If it is not obvious, I am printing the definition of the word, and requesting the user to input the word that matches the definition.

Any help to what is the cause of my error would be greatly appreciated. Thanks.

SoulMazer 26 Posting Whiz in Training

Thank you very much! Problem solved!

SoulMazer 26 Posting Whiz in Training

Hi, I have been learning Python and have a quick dictionary question.

I am trying to make a "vocabulary test" to help myself study, and am trying to do it with a dictionary. So far I have this:

testwords = {"uno":"one","dos":"two","tres":"three","cuatro":"four",\
             "cinco":"five"}

def vocabtest():
    for value in testwords:
        print "What word does the following definition correspond with?"
        print value
        answer = raw_input("> ")
        if answer == :
            print "Correct!"

vocabtest()

I need help with line 9, "if answer == :".
I am not sure how I would check to see if the user's answer is equal to the corresponding key.

Any help would be greatly appreciated. Thanks.

SoulMazer 26 Posting Whiz in Training

Problem solved. Thanks a lot.

SoulMazer 26 Posting Whiz in Training

First off, I know that to set an indexed variable, say, mylist[3] = "Hello". I also know that you wouldn't be able to set mylist[3] if you didn't first state:

mylist = ["Whatever", "Whatever", "Whatever", "Whatever"]

But, if you had a variable with an extremely large index, it would be a pain in the rear to type " "Whatever", " that many times.

So, my question is, is there a not-tedious way to do this? Like Ruby's mylist = "Whatever" * 100?

Thanks in advance.

SoulMazer 26 Posting Whiz in Training

Thanks Paulthom. Solved my problem.

Thank you much.

SoulMazer 26 Posting Whiz in Training

Well, I have recently created a "translation" program. It basically asks what languages you would like to translate between, then goes to the appropriate page on a translation website. I have integrated an option to keep using a language, rather than having to start at the main menu again. Except, my loop doesn't seem to want to reset. Could anybody help?

# Translate

import webbrowser

def engtospa():
    a = "http://translate.google.com/translate_t#en|es|"
    b = raw_input("What do you want to translate?\n> ")
    if b == "stop":
        loop1 = False
    else:
        webbrowser.open(a+b)
        print "Success."

def spatoeng():
    a = "http://translate.google.com/translate_t#es|en|"
    b = raw_input("What do you want to translate?\n> ")
    if b == "stop":
        loop1 = False
    else:
        webbrowser.open(a+b)
        print "Success."

# Main Menu

def main():
    print "Welcome to the translation program. Do you want to translate..."
    print "1 - English to Spanish"
    print "2 - Spanish to English"
    lang = input("> ")
    if lang == 1:
        print "Do you want to stay with this language?"
        print "1 - Yes"
        print "2- No"
        ans = input("> ")
        if ans == 1:
            loop1 = True
            print "You can exit this any time by entering the word 'stop'"
            while loop1 == True:
                engtospa()
        elif ans == 2:
            engtospa()
    elif lang == 2:
        print "Do you want to stay with this language?"
        print "1 - Yes"
        print "2- No"
        ans = input("> ")
        if ans == 1:
            loop1 = True
            print "You can exit this any time by entering the word 'stop'"
            while loop1 …
SoulMazer 26 Posting Whiz in Training

Thanks for all the info guys, except vegaseat, what would be the use of saving the dictionary to a file? Is it for if I wanted to say create a menu that had the option of using one list or another?

SoulMazer 26 Posting Whiz in Training

Well, rather than doing a global, and taking that passing the variable raises an error, I did everything inside the function and I checked if the number of words was equal to the number of answers. I also changed the way it would redo the problem, to make sure it wouldn't go into the negatives.

words = ["aero", "aesthet", "andr", "anthr", "arch", "arch", "ast", "baro",\
"biblio", "bio", "cardi", "chron", "cosm", "crat", "cycl", "dem", "dont",\
"dogma", "dox", "esth"]

answers = ["air", "sense", "man", "man", "chief", "ancient", "star", "weight",\
"book", "life", "heart", "time", "universe", "rule", "circle", "people",\
"tooth", "opinion", "belief", "feeling"]

def test():
    if len(words) == len(answers):
        a = 0
        loop = 1
        while loop == 1:
            if a == len(words):
                print "You are done."
                loop = 0
            else:
                b = a + 1
                print b, ".", "What does", words[a], "mean?"
                ans = raw_input("> ")
                if ans == answers[a]:
                    print "Correct!"
                    a = a + 1
                else:
                    print "That is incorrect. Try again."          

test()

Also, is there a benefit to doing this with a dictionary?

SoulMazer 26 Posting Whiz in Training

Wow you guys ARE extremely helpful. I'm really starting to like this site. Well thanks for all the extra info, I learned a few more things.

SoulMazer 26 Posting Whiz in Training

Well, I asked one of my Python friends and he gave me the answer. Feel free to delete/do whatever with post.

SoulMazer 26 Posting Whiz in Training

I started programming in Python as a hobby a few weeks ago, and I have run into a little problem. I am currently making a script to give me a sort of Vocabulary Test. It is kind of hard to explain so I will just show you the code.

words = ["aero", "aesthet", "andr", "arch", "arch", "ast", "baro",\
"biblio", "bio", "cardi", "chron", "cosm", "crat", "cycl", "dem", "dont",\
"dogma", "dox", "esth"]

answers = ["air", "sense", "man", "man", "chief", "ancient", "star", "weight",\
"book", "life", "heart", "time", "universe", "rule", "circle", "people",\
"tooth", "opinion", "belief", "feeling"]

a = 0

def test():
    a = a + 1
    print "What does", words[a], "mean?"
    ans = raw_input("> ")
    if ans == answers[a]:
        print "Correct!"
        test()
    else:
        print "That is incorrect. Try again."
        a = a - 1
        test()

test()

So, the error I get is:

Traceback (most recent call last):
File "C:\Users\Pat\Python Scripts\Scripts in Progress\Testing - Copy (10).py", line 26, in <module>
test()
File "C:\Users\Pat\Python Scripts\Scripts in Progress\Testing - Copy (10).py", line 15, in test
a = a + 1
UnboundLocalError: local variable 'a' referenced before assignment

I made my script thinking I would just be able to loop through all of the words, except since I am having a problem with my "a" variable I am stuck... And I am pretty sure I have another bug in my script with returning "a"...

Would anybody be able to help?

Thanks in advance.