python quiz, picking a random query from a list


Is something like this what you're looking for? It repeats the set of questions until the amount of right answers is >= 5. However, the way you've written it, it repeatedly asks the same question until you get it right. Is this what you intended?

import random

def questionchal(questionText, answer):
    if input(questionText + ' ') == answer:
        return True

def gamelist():
    game = input('select a game, game 1, game 2 or game 3 ')
    if game == 'game 1':
        print('okay')
    if game == 'game 2':
        print('okayy')
    if game == 'game 3':
        print('okayyy')

questionsright = 0

#creates a list of questions and their answers
questionList = [
        ('What is my name?' , 'john'),
        ('Where do i live?' , 'uni'),
        ('Am i happy or sad?' , 'happy'),
        ('Am i hungry or full?' , 'hungry'),
        ('Am i alseep or awake?' , 'awake'),
        ('how old am i?' , '19')
    ]

if __name__ == "__main__":
    while questionsright < 5:
        for questionText, answer in questionList:
            while questionchal(questionText, answer) is not True:
                print('Wrong, heres another question!')
            print('Correct! Next question!')
            questionsright +=1
            print(questionsright)

    else:
        print('Thanks for playing')
        gamelist()

and if you want it to skip to the next question if the user gets it wrong, you can do this:

if __name__ == "__main__":
    while questionsright < 5:
        for questionText, answer in questionList:
            if questionchal(questionText, answer):
                print('Correct! Next question!')
                questionsright +=1
                print(questionsright)
            else:
                print('Wrong, heres another question!')

    else:
        print('Thanks for playing')
        gamelist()


Making a quiz to ask random questions in python from csv file

You're almost there.

You can use random.choice() to select a random line from the players list.
Use the raw_input() function to get input from the user.

Then you just need to use the == operator to check if the answer is correct.

I would also recommend using the with statement to make the file automatically close.


Avoid picking the same random word in a list until the list is exhausted

An alternate approach would be to shuffle the list of words then simply iterate through it.

The Collections package implements List shuffle, although I do not know what algorithm it uses, One would hope it is a good implementation of Fisher-Yates.


Picking random color from array list

You need to get the item at the random index, currently you are trying to assign an int to a Color:

colorTextLabel.ForeColor = (Color)colors[randomColor];

And do not ever use ArrayList while you can use generics.


Picking random sub-routines from a list
import random
def func_addition():
    a = random.randint(1,25)
    b = random.randint(1,25)
    c=a+b
    answer=int(input("What is "+str(a)+" + "+str(b)+" ? "))   

def func_subtraction():
    d = random.randint(10,25)
    e = random.randint(1,10)
    f=d-e
    answer=int(input("What is "+str(d)+" - "+str(e)+" ? "))

def func_multiplication():
    g = random.randint(1,10)
    h = random.randint(1,10)
    i=g*h
    answer=int(input("What is "+str(g)+" X "+str(h)+" ? "))

my_list=[func_addition , func_subtraction , func_multiplication]
#without parentheses

name=input("What is your name ? ")
print("Hello "+str(name)+" and welcome to The Arithmetic Quiz")
random.choice(my_list)()

Picking at random from a list, allowing no repetition

By picking a random index of the list and removing the list element at that index.

If you're using the list elsewhere, you may want to create a copy of the list elsewhere in memory so you have the original.


python quiz, picking a random query from a list

Is something like this what you're looking for? It repeats the set of questions until the amount of right answers is >= 5. However, the way you've written it, it repeatedly asks the same question until you get it right. Is this what you intended?

import random

def questionchal(questionText, answer):
    if input(questionText + ' ') == answer:
        return True

def gamelist():
    game = input('select a game, game 1, game 2 or game 3 ')
    if game == 'game 1':
        print('okay')
    if game == 'game 2':
        print('okayy')
    if game == 'game 3':
        print('okayyy')

questionsright = 0

#creates a list of questions and their answers
questionList = [
        ('What is my name?' , 'john'),
        ('Where do i live?' , 'uni'),
        ('Am i happy or sad?' , 'happy'),
        ('Am i hungry or full?' , 'hungry'),
        ('Am i alseep or awake?' , 'awake'),
        ('how old am i?' , '19')
    ]

if __name__ == "__main__":
    while questionsright < 5:
        for questionText, answer in questionList:
            while questionchal(questionText, answer) is not True:
                print('Wrong, heres another question!')
            print('Correct! Next question!')
            questionsright +=1
            print(questionsright)

    else:
        print('Thanks for playing')
        gamelist()

and if you want it to skip to the next question if the user gets it wrong, you can do this:

if __name__ == "__main__":
    while questionsright < 5:
        for questionText, answer in questionList:
            if questionchal(questionText, answer):
                print('Correct! Next question!')
                questionsright +=1
                print(questionsright)
            else:
                print('Wrong, heres another question!')

    else:
        print('Thanks for playing')
        gamelist()


- Technology - Languages
+ Webmasters
+ Development
+ Development Tools
+ Internet
+ Mobile Programming
+ Linux
+ Unix
+ Apple
+ Ubuntu
+ Mobile & Tablets
+ Databases
+ Android
+ Network & Servers
+ Operating Systems
+ Coding
+ Design Software
+ Web Development
+ Game Development
+ Access
+ Excel
+ Web Design
+ Web Hosting
+ Web Site Reviews
+ Domain Name
+ Information Security
+ Software
+ Computers
+ Electronics
+ Hardware
+ Windows
+ PHP
+ ASP/ASP.Net
+ C/C++/C#
+ VB/VB.Net
+ JAVA
+ Javascript
+ Programming
Privacy Policy - Copyrights Notice - Feedback - Report Violation 2018 © BigHow