Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    85
    Rep Power
    10

    generating random......


    wus up everybody this is my 1st python program
    well let me give u an over view of it..its like a family fued type of deal

    i got everything rite but theres one problem i have randomly gen. the questions after the 1st question im using

    question = random.randrange(1,100+1)

    if question == 1:
    print 'QUESTION..........'
    print 'multiple choice answers'

    if question == 2:
    print '..............'

    after the 2 dif teams answer the question i want it to automaically generate the next question is there a dif way to produce this result...if yall need i will write my source
    NOTE: it may not be professional but im sure one of yall can set me in the rite direction.. ty for ur time
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Do you mean generate a random question that has been already created by yourself? If so, you can create an array of all possible questions and use the choice function of the random module to make life easier.
    Code:
    from random import *
    question_list = ['Who was the first President?', 'What color underwear does Spongebob wear on Thursday?', 'Yes or No?']
    print choice(question_list)
    This will print a random element from the question_list array onto the screen.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    85
    Rep Power
    10
    ok umm so basically what i will do is add this block of code with all my questions "question_list" and im assumthin that "question_list" is self generating so all i wud have to do is type "print question_list"...
    2nd i never used this method b4 so i wud i add a raw_input to each question and is it necessary to add a if-else statment on the answers

    if it is poossible n not to much trouble can u show me a quick 1/2 a prgm of using the randmon chioce method with a raw_input n a way to see if the answer foew each problem is right or wrong

    and thank you for the help....im goin to play wit the line u sent me so yea
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    That code just prints a *random* question from the question_list array. All of your questions should be added in question_list. Whenever you want to ask a question, do this:
    Code:
    print choice(question_list)
    Remember to also do this:
    Code:
    from random import *
    If you would like to you can add some if statements to make sure a question isn't used more than once.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    85
    Rep Power
    10
    ok but sense the questions are randomly generated how do i make it print the multiple chioce(a. b. c. d.) along with each specified question....and make sure the Raw_input is the correct answer, then once the question is answered i want to it automatically keep generating....then ill make my if-else statement

    sorry if i didnt clearify that the 1st post but thats what i was meaning to say

    plz bare wit me im totally new to this concept of arrays

    Thank u
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    I can't give you all that code. Then I would not be letting you get the full experience of creating your own complete program. All I can say is that to print a random question, use the choice() function that I already showed you how to use.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    85
    Rep Power
    10
    ok i understand now....ty for that much its a big help off to go play wit it..peace out man
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    85
    Rep Power
    10
    ok i understand the question_list part but when it comes to answerin the random questions i get lost...ive tried to make a list such as:


    Code:
    question_list = ['blue or white', 'what color are you'
    print choice(question_list)
    ]
    answer_1 = raw_input('')
    answers = ['blue', 'red', '15']
    
    if (question_list):
       answer_1 = raw_input('')
       if (answer_1) == (answers):
          print 'correct'
       else:
          print 'wrong'
    everytime i enter a str from 'answers' i get the 'WRONG from the else block....y is that

    '
  16. #9
  17. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Ok, I've come up with an idea and I'm not sure why I didn't think of this earlier. Use a dictionary and a list. This way you can record the questions with their answers in one variable. I'll show you how you can use it. First, create a list of all possible questions.
    Code:
    question_list = ['question1', 'question2', 'question3']
    Next, create a dictionary with the questions and their answers.
    Code:
    qDictionary = {'question1':answer goes here, 'question2':answer goes here, 'question3':answer goes here}
    Note: The question (as an element) in the question_list variable should be *exactly* the same as the item to the left of each ':' in the qDictionary variable. Now, where you have
    Code:
    print choice(question_list)
    You should do this instead:
    Code:
    a = choice(question_list) #replace a with whatever you want
    print a
    Now you can use a as the variable used for the random question. To check if it's answer is correct or not, you can use our qDictionary. Here's an example:
    Code:
    a = choice(question_list)
    users_answer = raw_input(a + ' : ')
    if users_answer == qDictionary[a]:
        print 'Correct!'
    else:
        print 'Wrong!'
    I'm sure that by this post you will understand everything that you need to know. If you want to learn more about dictionary's, check the main Python website.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    85
    Rep Power
    10
    there we go now im gettin sumplace ty so much man ill be checkin in/out until i get it up n running correctly....after i do that much i plan on makin a decent GUI....which GUI is better Tk or Wx im using a linux box and i will sooner or later put on windows using PY2EXE for distrub.
  20. #11
  21. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Well, for GUI developement I prefer wxPython. Like Tkinter it supports all major platforms but wxPython has many more features to offer you. And I'm glad that I could help you out with your program.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    85
    Rep Power
    10
    is it possible to loop random questions....i kno a random one starts at the beginning of the program but after the question has been answered, i want to loop the "question' and 'qDictionary' to a diferent question not the same one that was previous generated at the start....dont tell me the code i jus wud like a tip lol i think u helped enuf
    i been playing wit this damn thing for 3 hours straight tryin to ggenerate a WHOLE new question w/o closing the program

    ty for the tip...

    Code:
    while (pts_1) or (pts_2) != 10:
       question
       qDictionary
    this gave me an contin loop but im workin on it now so ill hit u up later on 2morrow peace out man
  24. #13
  25. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Code:
    asked_questions = []
    while condition_here:
        a = choice(question_list)
        if a in asked_questions:
            continue # restart loop
        users_answer = raw_input(a + ' : ')
        if users_answer == qDictionary[a]:
            print 'Correct!'
            asked_questions.append(a)
        else:
            print 'Wrong!' 
            asked_questions.append(a)
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    85
    Rep Power
    10
    ok i c now i was puttin WHILE with the wrong code append.[a] im gonna assum after it generates a question it puts the questions into another list so they wont be displayed again...correct?

    imma test a few things dont tell me the code jus a yes or no to my question above will do ty
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    85
    Rep Power
    10
    Code:
    asked_questions = []
    while True:
        question = choice(question_list)
        if question in asked_questions:
            continue # restart loop
            question.remove(question)
        users_answer = raw_input(question + ' : ')
        if users_answer == qDictionary[question]:
            print 'Correct!'
            asked_questions.append(question)
        else:
            print 'Wrong!' 
            asked_questions.append(question)
    i figured out sumthin with a lucky guesse lol..im not sure if it a real code but im sure it worked question.remove(question) line is what i added to your code and from what i saw all 10 of my questions being displayed w/o repeats...i wud like to send u the prgrm to test it n look at the source code...it only has ten questions for now im no where near done adding questions...i got atleast 90 more to go it called Cadet Fued for my JROTC class so yea
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo