0

I am making 2 sets of question, first set (list1) is all yes and second (list2) is all no 10 questions each then I used random.choice to randomly pick a question from the 2 sets. I want to make an output of "correct" whenever random.choice picks a question from list 1 when the user answered "Y" same with list2 if the user answered "N". Here is my code:

import random


Count = 0

list1 = ['1+1=2?','2+2=4?','3+3=6?','4+4=8?','5+5=10?','15+15=30?','16+16=32?','17+17=34?','18+18=36?','19+19=38?']
list2 = list2 = ['20+20=10?','6+6=2?','7+7=11?','8+8=32?','9+9=21?','10+10=0?','11+11=4?','12+12=12?','13+13=25?','14+14=13?']
list = list1 + list2

while(Count < 5):
    print(random.choice(list))
    Answer = input("Answer: ").upper()
    Count = Count+1
    if(list == list1):
       if(Answer == "Y"):
          print("Correct")
       else:
          print("Incorrect")
    if(list == list2):
       if(Answer == "N"):
          print("Correct")
       else:
          print("Incorrect")

I cannot figure out how to output the correct and incorrect statement. Any help would be truly appreciated.

Gelo
  • 43
  • 1
  • 5
  • use the `in` keyword to check if the random question you got belongs to the first list or the second one like: `if random.choice(list) in list1:` – Ma0 Sep 06 '16 at 11:16
  • You could build up the equation on the fly, then use the `operator` module to replace the operator character, e.g. `+` with `operator.add`, [see here](http://stackoverflow.com/a/26261125/2296458) for an example – Cory Kramer Sep 06 '16 at 11:16
  • Well, you *could* use `in` to find which list your random choice ended up coming from, but I'd say that's not very Pythonic (or efficient,come to that). I think, however, you would be better off using a dict to map questions to correct\incorrect then doing a lookup on that. – SiHa Sep 06 '16 at 11:17
  • Also, you might find this interesting: `question = random.choice(list)`, `print(eval(question.replace('?', '').replace('=', '==')))` – Ma0 Sep 06 '16 at 11:22

4 Answers4

2

First off, don't use python keywords and built-in type names as your variable names.

You can check the membership of random choice simply with in operator.

import random

list1 = ['1+1=2?','2+2=4?','3+3=6?','4+4=8?','5+5=10?','15+15=30?','16+16=32?','17+17=34?','18+18=36?','19+19=38?']
list2 = ['20+20=10?','6+6=2?','7+7=11?','8+8=32?','9+9=21?','10+10=0?','11+11=4?','12+12=12?','13+13=25?','14+14=13?']
lst = list1 + list2

for _ in range(5):
    value = random.choice(lst)
    print(value)
    Answer = input("Answer: ").upper()
    if(Answer == "Y"):
        if value in list1:
            print("Correct")
        else:
            print("Incorrect")
    elif(Answer == "N"):
        if value in list2:
            print("Correct")
        else:
            print("Incorrect")
    else:
        print("Enter Y or N")
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • Thanks for the help I understand it better now but one more thing, may I ask what is the function of "_" in the for loop? I am still learning python as you can see. Thanks – Gelo Sep 07 '16 at 04:07
  • @Gelo It's a prereserved variable in python, which make you don't define new one specially for throwaway variables. – Mazdak Sep 07 '16 at 06:59
0

My solution is slightly different! Save the equations in the list with the double == and after do in this way:

import random

list1 = ['1+1==2','2+2==4','3+3==6','4+4==8','5+5==10','15+15==30','16+16==32','17+17==34','18+18==36','19+19==38']
list2 = ['20+20==10','6+6==2','7+7==11','8+8==32','9+9==21','10+10==0','11+11==4','12+12==12','13+13==25','14+14==13']
lst = list1 + list2

for _ in range(5):
    value = random.choice(lst)
    print(value)
    Answer = input("Answer: ").upper()
    state = eval(value)
    if(Answer == "Y" and state is True) or (Answer == "N" and state is False):
            print("Correct")
    else:
            print("Incorrect")

The eval function take in input one string containing the operation and solve it. In this case it's a logical operation so it will return you True or False. Try the eval function on the Python shell if you don't know it.

In this way if you want you could use only one list.

In that case:

import random

lst = ['1+1==2','2+2==4','3+2==6','4+3==8','5+5==10','15+8==30','16+16==32','17+22==34','18+30==36','19+19==38']

for _ in range(5):
    value = random.choice(lst)
    print(value)
    Answer = input("Answer: ").upper()
    state = eval(value)
    if(Answer == "Y" and state is True) or (Answer == "N" and state is False):
            print("Correct")
    else:
            print("Incorrect")
melix
  • 468
  • 4
  • 16
0

A more pythonic way would be:

list1 = ['1 + 1 = 2? ', '2 + 2 = 4? ', '3 + 3 = 6? ', '4 + 4 = 8? ',
         '5 + 5 = 10? ', '15 + 15 = 30? ', '16 + 16 = 32? ', '17 + 17 = 34? ',
         '18 + 18 = 36? ', '19 + 19 = 38? ']
list2 = ['20 + 20 = 10? ', '6 + 6 = 2? ', '7 + 7 = 11? ', '8 + 8 = 32? ',
         '9 + 9 = 21? ', '10 + 10 = 0? ', '11 + 11 = 4? ', '12 + 12 = 12? ',
         '13 + 13 = 25? ', '14 + 14 = 13? ']

for _ in range(5):
    chosen_list = random.choice([list1, list2])
    chosen_question = random.choice(chosen_list)
    answer = raw_input(chosen_question).upper()
    if chosen_question in list1 and answer == 'Y':
        print('Correct')
    elif chosen_question in list2 and answer == 'N':
        print('Correct')
    else:
        print('Incorrect')
Tiger-222
  • 6,677
  • 3
  • 47
  • 60
0

Rather than searching the two lists for the chosen question it's simpler to randomly choose a list and then choose a question from that list. This approach will have the same probability of choosing a given question as your approach as long as both lists have the same length.

We can still combine the two lists into a single data structure; the code below combines them into a dictionary, with the answer as the key.

from random import choice

questions = {
    'Y': [
        '1+1=2?', '2+2=4?', '3+3=6?', '4+4=8?', '5+5=10?', '15+15=30?', 
        '16+16=32?', '17+17=34?', '18+18=36?', '19+19=38?',
    ],
    'N': [
        '20+20=10?', '6+6=2?', '7+7=11?', '8+8=32?', '9+9=21?', '10+10=0?', 
        '11+11=4?', '12+12=12?', '13+13=25?', '14+14=13?',
    ],
}

for _ in range(5):
    key = choice('YN')
    print(choice(questions[key]))
    answer = input("Answer: ").upper()
    if answer == key:
        print("Correct")
    else:
        print("Incorrect")

typical output

14+14=13?
Answer: N
Correct
19+19=38?
Answer: n
Incorrect
11+11=4?
Answer: Y
Incorrect
3+3=6?
Answer: y 
Correct
13+13=25?
Answer: n
Correct

We can make the code more compact by replacing the if...else block with this:

print(("Incorrect", "Correct")[answer == key]) 

That works because False has a numeric value of 0 and True has a numeric value of 1.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182