What have I got wrong here?

This is the place for queries that don't fit in any of the other categories.

What have I got wrong here?

Postby coatsoft » Tue Dec 10, 2013 6:45 pm

The task is to make a capital cities quiz which will delete the question when it is asked so it will not be asked again. It is not working correctly though. Can anyone spot the problem? Here is my code:
Code: Select all
counter = -1
import random
questions = ["What is the capital of France?", "Which country is London locates in?"]
answers = ["Paris", "England"]
print("Enter your answers with a capital letter at the start")
correct = False
while (len(questions)) > 0:
    choice = random.choice(questions)
    option = input(choice)
    each1 = 0
    answersl = (len(answers)) - 1
    for each2 in questions:
        counter = counter + 1
        if each2 == choice:
            break
   
    for each in answers:
        each1 = (each1) + 1
        if each == option:
            each1 = (each1) -1
            if (counter) == (each1):
                print("correct")
                correct = True
               
                break
    if correct == False:
        print("WRONG")
    answers.remove(each)
    questions.remove(choice)
    counter == 0
   
   


If you enter an incorrect answer and then a correct answer it will class the correct answer as incorrect
Thanks in advance :)
Last edited by coatsoft on Tue Dec 10, 2013 7:05 pm, edited 1 time in total.
coatsoft
 
Posts: 7
Joined: Fri Oct 04, 2013 8:40 pm

Re: What have I got wrong here?

Postby Yoriz » Tue Dec 10, 2013 7:02 pm

What does
It is not working correctly though
mean.
What is expected to happen and what is happening instead.

Also take a look at this link for a simpler way of achieving this.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 781
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: What have I got wrong here?

Postby coatsoft » Tue Dec 10, 2013 7:09 pm

Yoriz wrote:What does
It is not working correctly though
mean.
What is expected to happen and what is happening instead.

Also take a look at this link for a simpler way of achieving this.


If you enter an incorrect answer and then a correct answer it will class the correct answer as incorrect
also if you input more than one incorrect answer it will only count the first one
coatsoft
 
Posts: 7
Joined: Fri Oct 04, 2013 8:40 pm

Re: What have I got wrong here?

Postby coatsoft » Tue Dec 10, 2013 7:41 pm

Yoriz wrote:What does
It is not working correctly though
mean.
What is expected to happen and what is happening instead.

Also take a look at this link for a simpler way of achieving this.

I have looked at the link but really havent covered this in class as I am very new to python and for the benefit of the task do you know how to solve the problem using my original method?
Thanks
coatsoft
 
Posts: 7
Joined: Fri Oct 04, 2013 8:40 pm

Re: What have I got wrong here?

Postby Yoriz » Tue Dec 10, 2013 7:47 pm

The code is not written in a way python code would be written.
It's overly complicated then it needs to be so finding whats wrong is not a simple task.

For starters at the end of your loop your are doing
Code: Select all
counter == 0

this is just comparing the equality of the two values
your counter would need to be set back to
Code: Select all
counter = -1

But even doing that im sure your still going to have other problems.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 781
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: What have I got wrong here?

Postby coatsoft » Tue Dec 10, 2013 8:03 pm

Yoriz wrote:The code is not written in a way python code would be written.
It's overly complicated then it needs to be so finding whats wrong is not a simple task.

For starters at the end of your loop your are doing
Code: Select all
counter == 0

this is just comparing the equality of the two values
your counter would need to be set back to
Code: Select all
counter = -1

But even doing that im sure your still going to have other problems.

deary me

no it's still messed up
coatsoft
 
Posts: 7
Joined: Fri Oct 04, 2013 8:40 pm

Re: What have I got wrong here?

Postby Yoriz » Tue Dec 10, 2013 8:07 pm

Im sorry to say but its bad code, telling you how patch up bad code is not a good thing,
i dont know what you have learnt so far to be able to know how to help you solve it in a way you would understand.

For instance this
Code: Select all
    for each2 in questions:
        counter = counter + 1
        if each2 == choice:
            break

Could be replaced with
Code: Select all
index = questions.index(choice)

and then to get the answer at the same index as the question you would just need
Code: Select all
answer = answers[index]
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 781
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: What have I got wrong here?

Postby coatsoft » Tue Dec 10, 2013 8:23 pm

Yoriz wrote:Im sorry to say but its bad code, telling you how patch up bad code is not a good thing,
i dont know what you have learnt so far to be able to know how to help you solve it in a way you would understand.

For instance this
Code: Select all
    for each2 in questions:
        counter = counter + 1
        if each2 == choice:
            break

Could be replaced with
Code: Select all
index = questions.index(choice)

and then to get the answer at the same index as the question you would just need
Code: Select all
answer = answers[index]

yep don't know about .index
really new just started at GCSE in september
coatsoft
 
Posts: 7
Joined: Fri Oct 04, 2013 8:40 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], snippsat, Yoriz and 3 guests