1

I am a beginer python learner. I am trying to create a basic dictionary where random meaning of words will come and user have to input the correct word. I used the following method, but random doesn't work. I always get the first word first and when the last word finishes, I get infinite 'none' until I kill it. Using python 3.2

from random import choice


print("Welcome , let's get started")
input()

def word():
    print('Humiliate')
    a = input(':')
    while a == 'abasement':
        break
    else:
        word()
    #   --------------------------------------------------------- #



def word1():
    print('Swelling')
    a = input(':')
    while a == 'billowing':
        break
    else:
        word()
#   ------------------------------------------------------------ #


wooo = [word(),word1()]
while 1==1:
    print(choice(wooo))

is there any faster way of doing this and get real random? I tried classes but it seems harder than this. Also, is there any way I can make python not care about weather the input is capital letter or not?

2
  • I would restructure the code in a cleaner way. You could e.g. build a list of 2-tuples, each tuple consisting of ("question", "correct_answer"). Then, I'd choose one of these tuples randomly and provide it to a function that is actually printing the first element in the tuple and asking for user input. By doing so you avoid writing the same code multiple times. Commented Aug 30, 2012 at 16:51
  • Thanks, I will try creating one like suggested. Commented Aug 30, 2012 at 17:08

2 Answers 2

2

To answer one part of your question ("is there any way I can make python not care about weather the input is capital letter or not?"): use some_string.lower():

>>> "foo".lower() == "foo"
True
>>> "FOO".lower() == "foo"
True

An this is to help you how you could improve the structure of your code:

import sys
from random import choice

WORDPAIRS = [('Humiliate', 'abasement'), ('Swelling', 'billowing')]

def ask():
    pair = choice(WORDPAIRS)
    while True:
        answer = raw_input("%s: " % pair[0]).lower()
        if answer == pair[1]:
            print "well done!"
            return


def main():
    try:
        while True:
            ask()
    except KeyboardInterrupt:
        sys.exit(0)


if __name__ == "__main__":
    main()

It works like that:

$ python lulu.py 
Swelling: lol
Swelling: rofl
Swelling: billowing
well done!
Humiliate: rofl
Humiliate: Abasement
well done!
Swelling: BILLOWING
well done!
Humiliate: ^C
$
Sign up to request clarification or add additional context in comments.

3 Comments

I never used .lower function before. How do i use it? I tried adding somewhere like this: while a == 'Billowing'.lower(): but it doesn't do the job.
It is a string method. Just apply it to a string s via s.lower() and it returns the same string with all capital letters replaced with their lowercase counterpart. That's why "FOO".lower() returns "foo". Keep your reference string ("billowing") in lowercase and convert the test string (user-given) to lowercase before comparing test string with reference string. By doing so, the test is not case-sensitive. Example: a.lower() == 'billowing'. And upvote answers that helped :-).
Thanks for the explanation :) I am using n = 'abAsement'.lower() while a == n:
2
wooo = [word, word1]
while 1:
    print(choice(wooo)())

But in any case it will print you None, cause both of your functions return nothing (None).

1 Comment

Thanks :) it works! I tried getting rid of none with return but it messes up the work I want the function to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.