This is a text-based quiz example, where the program is supposed to give 5 questions for the user to answer in a random order. The problem is that it works perfectly, but only gives 3 random questions, then stops.
import random
question1=["The answer is A","A","a"]
question2=["The answer is B","B","b"]
question3=["The answer is A","A","a"]
question4=["The answer is F","F","f"]
question5=["The answer is A","A","a"]
questions=[question1,question2,question3,question4,question5]
used_questions=[]
while len(used_questions)!=len(questions):
random_question=random.choice(questions)
while random_question in used_questions:
random_question=random.choice(questions)
used_questions.append(random_question)
print([random_question[0]])
players_answer=input("")
if players_answer in random_question:
print("\nCorrect!")
else:
print("\nWrong!")
Questions 1, 3, and 5 are identical, so you only have three unique questions, so only three questions are displayed. random_question in used_questions evaluates to True as long as one element of used_questions is equal to random_question; they do not have to refer to the exact same list in memory.
As mentioned in the comments and other answers, shuffling the list is an easier approach here.
import random
questions=[
["The answer is A","A","a"],
["The answer is B","B","b"],
["The answer is A","A","a"],
["The answer is F","F","f"],
["The answer is A","A","a"]
]
random.shuffle(questions)
for question in questions:
print(question[0])
players_answer=input("")
if players_answer in question:
print("\nCorrect!")
else:
print("\nWrong!")
Result:
The answer is F
F
Correct!
The answer is A
B
Wrong!
The answer is A
Q
Wrong!
The answer is B
B
Correct!
The answer is A
A
Correct!
A much simpler approach is to use shuffle:
random.shuffle(questions)
This shuffles your questions in place. You can then just loop round the questions.
Related
Inform 7 tables are written using "tab separated values". Whenever a table contains text, it ends up looking like this:
Table of Aristotelian Questions
name difficulty text explanation follow-up
the opposite question easy "WHAT IS THE OPPOSITE OF [the subject]?[line break]" "SOMETIMES A GOOD WAY TO DESCRIBE SOMETHING IS BY TELLING[line break]WHAT IT IS NOT. THERE MAY OR MAY NOT BE A DIRECT[line break]OPPOSITE OF [the subject], BUT[line break]SEE IF YOU CAN THINK OF ONE.[line break][line break]FOR EXAMPLE, IF I WERE WRITING A PAPER ON SOLAR[line break]ENERGY, AN ANSWER TO THIS QUESTION MIGHT PRODUCE A[line break]LIST OF EARTH'S NATURAL ENERGY RESOURCES.[line break]" prompt A
the good consequences question easy "WHAT ARE THE GOOD CONSEQUENCES OF[line break][the subject]?[line break]" "WHAT GOOD WILL COME ABOUT FROM MANKIND'S CONCERN ABOUT[line break][the subject]?[line break][line break]FOR EXAMPLE, IF I WERE WRITING A PAPER ABOUT COLLEGE[line break]ACADEMICS, SOME OF THE GOOD CONSEQUENCES MAY BE A BETTER[line break]JOB IN THE FUTURE, A FULLER UNDERSTANDING[line break]ABOUT OUR WORLD, AND AN APPRECIATION FOR GOOD STUDY HABITS.[line break](STOP THE SNICKERING AND GET ON WITH AN ANSWER.)[line break]" prompt C
Is there another way to write the same table? (I'm imagining something sort of YAML-like.)
No, that's the only way to make a table in I7. But you can extract all of the text as constants:
Table of Aristotelian Questions
name difficulty text explanation follow-up
the opposite question easy Opposite question Opposite follow up prompt A
the good consequences question easy Good question Good follow up prompt C
Opposite question is always "WHAT IS THE OPPOSITE OF [the subject]?[line break]".
Opposite follow up is always "SOMETIMES A GOOD WAY TO DESCRIBE SOMETHING IS BY TELLING[line break]WHAT IT IS NOT. THERE MAY OR MAY NOT BE A DIRECT[line break]OPPOSITE OF [the subject], BUT[line break]SEE IF YOU CAN THINK OF ONE.[line break][line break]FOR EXAMPLE, IF I WERE WRITING A PAPER ON SOLAR[line break]ENERGY, AN ANSWER TO THIS QUESTION MIGHT PRODUCE A[line break]LIST OF EARTH'S NATURAL ENERGY RESOURCES.[line break]".
Good question is always "WHAT ARE THE GOOD CONSEQUENCES OF[line break][the subject]?[line break]".
Good follow up is always "WHAT GOOD WILL COME ABOUT FROM MANKIND'S CONCERN ABOUT[line break][the subject]?[line break][line break]FOR EXAMPLE, IF I WERE WRITING A PAPER ABOUT COLLEGE[line break]ACADEMICS, SOME OF THE GOOD CONSEQUENCES MAY BE A BETTER[line break]JOB IN THE FUTURE, A FULLER UNDERSTANDING[line break]ABOUT OUR WORLD, AND AN APPRECIATION FOR GOOD STUDY HABITS.[line break](STOP THE SNICKERING AND GET ON WITH AN ANSWER.)[line break]".
I am trying to solve a puzzle where I have broken it down into 2 separate problems that both require recursion. When the 1st recursive problem is solved I want to then call the next recursive problem and when that is solved the puzzle is then complete. My problem is when the 1st recursive problem comes up with a solution that solves its problem but its not the right solution to solve the puzzle and the second recursive case fails.
For example if you have a puzzle C, that is broken into 2 Parts A and B. You would then use the solution for A, to solve B, and solving B would solve C.
My question is how would the recursion be setup (where the recursive calls would go so if the 2nd part failed it would then find a new solution to the 1st part)?
Need to be more specific dude.
But you need to check if the first solution is correct before you go to the second solution... Something like this:
def first_sol(x):
solving
more solving
answer
if answer is correct:
return answer
else:
first_sol(x+1)
def second_sol(x):
solving
more solving
answer
if answer is correct:
return answer
else:
second_sol(x+1)
x= initial guess
second_sol(first_sol(x))
If you want to make it more complex, put everything inside an while and as condition, it will only stop when answer == correct.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In R, what is the difference between these two?
floating point issue in R?
This is part of a code I created. I spend days looking for the problem when finally realized that a comparison that should be TRUE was being calculated by R as FALSE. I'm using R 2.14.2 64 bits on windows. This is the code to reproduce the problem.
concList= c(1.15, 1.15, 1.15 ,1.15 ,1.15 ,1.15 )
concList=concList-0.4
a=sum(concList)
b=length(concList)*0.75
str(a)
str(b)
print(a==b)
The last print will result in FALSE even thou they are shown as exactly the same number. I tough this could be some problem on the floating point numerical representation of R, so I added the code below which solves the problem.
a=round(a,1)
b=round(b,1)
print(a==b)
My question is, is there any more elegant solution? Is this a bug that should be reported?
Thanks for your time.
Because they aren't exactly the same number. They differ by a very small amount due to the computer's representation of numbers, also known as floating point errors:
> a - b
[1] -8.881784e-16
Jon Skeet has an excellent blog post on this issue, which pops up on Stack Overflow with some regularity.
As #mrdwab suggests in the comments, you should use all.equal(a, b) to test for near equality.
I'm more or less attempting to determine crypography algoirthms and how they work. I'm a little confused on proving how one is trivial.
For example:
MAC(xbit_key,Message) = xbit_hash(Message) XOR xbit_key
Take a look at this for a general explanation and that for a good example. If it's still not clear, come back with a more specific question.
I have question that comes from a algorithms book I'm reading and I am stumped on how to solve it (it's been a long time since I've done log or exponent math). The problem is as follows:
Suppose we are comparing implementations of insertion sort and merge sort on the same
machine. For inputs of size n, insertion sort runs in 8n^2 steps, while merge sort runs in 64n log n steps. For which values of n does insertion sort beat merge sort?
Log is base 2. I've started out trying to solve for equality, but get stuck around n = 8 log n.
I would like the answer to discuss how to solve this mathematically (brute force with excel not admissible sorry ;) ). Any links to the description of log math would be very helpful in my understanding your answer as well.
Thank you in advance!
http://www.wolframalpha.com/input/?i=solve%288+log%282%2Cn%29%3Dn%2Cn%29
(edited since old link stopped working)
Your best bet is to use Newton;s method.
http://en.wikipedia.org/wiki/Newton%27s_method
One technique to solving this would be to simply grab a graphing calculator and graph both functions (see the Wolfram link in another answer). Find the intersection that interests you (in case there are multiple intersections, as there are in your example).
In any case, there isn't a simple expression to solve n = 8 log₂ n (as far as I know). It may be simpler to rephrase the question as: "Find a zero of f(n) = n - 8 log₂ n". First, find a region containing the intersection you're interested in, and keep shrinking that region. For instance, suppose you know your target n is greater than 42, but less than 44. f(42) is less than 0, and f(44) is greater than 0. Try f(43). It's less than 0, so try 43.5. It's still less than 0, so try 43.75. It's greater than 0, so try 43.625. It's greater than 0, so keep going down, and so on. This technique is called binary search.
Sorry, that's just a variation of "brute force with excel" :-)
Edit:
For the fun of it, I made a spreadsheet that solves this problem with binary search: binary‑search.xls . The binary search logic is in the second data column, and I just auto-extended that.