[Tutor] how run it on python 3

S Tareq stareq13 at yahoo.com
Mon Jan 20 21:51:25 CET 2014


this is the coding that i am trying to run it on python 3.3. the original coding was made on python 2.7 however i have made some changes to the coding to make it work on python 3.3. the changes that i have made  on brackets and raw_input to input. the coding does not load the external file ans says invalid syntax . please help me thank You very much.   

the changed coding: 
# Import statements
import random
import datetime
#Arrays to store the definitions and keywords read from the file
keywords=[];
definition=[];
correctAnswer=[];
#Counter for the wrong Answer and counter of number of definition in
correctAnswerCounter=0 
wrongAnswer=0;
counter=0;
# Taking User input for accepting the file name to be read
filename = ("keywords.txt")
#Reading the file from start to the end
for line in open(filename,'r').readlines():
    if(counter%2==0):
        keywords.append(line);
        counter=counter+1;
        correctAnswer.append(0)
    else:
        definition.append(line);
        keys=[];
        keys=line.split(" ");
        counter=counter+1;
# Running two while loops to make the pattern recursive
while True:
# Starting the time for quiz and also, creating variables to make sure that same sequences and answers are not repeated
    a = datetime.datetime.now().replace(microsecond=0)
    prevWord=0
    prevSeq=0
    # While loop to run the code till each answer is correctly answered
    while correctAnswer.count(2)!=(counter/2):
        #While loop to generate an different random number from one the generated previously
        while True:        
            word=random.randint(0,(counter/2)-1)
            if(correctAnswer[word]!=2):
                break;
            if(prevWord==word):
                continue;
        # Displaying the new keyword each time.
        print ("Please Select the number which is the correct definition of the word:") ,keywords[word]
        #Generating an new sequence each time different from previous one
        while True:
            sequences =random.randint(0,2)
            if(prevSeq==sequences):
                continue;
            else:
                break
        #Generating an new incorrect answer each time different from previous one
        while True:
            incorrectAnswer=random.randint(0,len(correctAnswer)-1)
            if(incorrectAnswer==word):
                continue;
            else :
                break
        #Generating an new incorrect answer  each time different from previous one
        while True:
            incorrectAnswerSecond=random.randint(0,len(correctAnswer)-1);
            if (incorrectAnswer==incorrectAnswerSecond):
                continue
            if(incorrectAnswerSecond==word):
                continue
            else:
                break
        # Displaying the options to the user based on the sequence number generated
        if (sequences==0):
            print ("1."),definition[word]
            print ("2."),definition[incorrectAnswer]
            print ("3."),definition[incorrectAnswerSecond]
        elif (sequences==1):
            print ("1."),definition[incorrectAnswer]
            print ("2."),definition[incorrectAnswerSecond]
            print ("3."),definition[word]
        elif (sequences==2):
            print ("1."),definition[incorrectAnswerSecond]
            print ("2."),definition[word]
            print ("3."),definition[incorrectAnswer]
        #Taking the answer from user
        answer = input("Enter the Correct Number between 1 to 3")
        # Assign the seq and word to preseq and word
        prevSeq=sequences
        prevWord=word
        #Checking the answer if they are corret.
        if(0 == sequences):
            if(answer == "1"):
                print ("success")
                correctAnswer[word]=correctAnswer[word]+1
                correctAnswerCounter=correctAnswerCounter+1
            else:
                print ("Wrong Answer")
                print ("Correct Answer: ") ,definition[word]
                wrongAnswer=wrongAnswer+1;
        elif(1 == sequences):
            if(answer == "3"):
                print ("success")
                correctAnswer[word]=correctAnswer[word]+1
                correctAnswerCounter=correctAnswerCounter+1
            else:
                print ("Wrong Answer")
                print ("Correct Answer: ") ,definition[word]
                wrongAnswer=wrongAnswer+1;
        elif(2 == sequences):
            if(answer == "2"):
                print ("success")
                correctAnswer[word]=correctAnswer[word]+1
                correctAnswerCounter=correctAnswerCounter+1
            else:
                print ("Wrong Answer")
                print ("Correct Answer: ") ,definition[word]
                wrongAnswer=wrongAnswer+1
    # Stopping the time of the clock
    b = datetime.datetime.now().replace(microsecond=0)
    # displaying number of wrong answer and total quiz time
    print ("Total Number of Wrong Answer:"), wrongAnswer
    print ("Total Quiz Time"), (b-a)
    print ("Total Number of correct Answer"), correctAnswerCounter
    #asking user to reenter
    restart= raw_input("Do You want to start the quiz again Yes or No")
    if(restart=="no"):
        print ("Thanks for quiz")
        break;
    elif(restart==("yes"):
        wrongAnswer=0
        correctAnswerCounter=0;
        correctAnswer=[];
        i=0
        while (i<(counter/2)):
            i=i+1
            correctAnswer.append(0)

  
original Coding:
# Import statements
import random
import datetime
#Arrays to store the definitions and keywords read from the file
keywords=[];
definition=[];
correctAnswer=[];
#Counter for the wrong Answer and counter of number of definition in
correctAnswerCounter=0 
wrongAnswer=0;
counter=0;
# Taking User input for accepting the file name to be read
filename= raw_input("Enter the File Name with extension")
#Reading the file from start to the end
for line in open(filename,'r').readlines():
    if(counter%2==0):
        keywords.append(line);
        counter=counter+1;
        correctAnswer.append(0)
    else:
        definition.append(line);
        keys=[];
        keys=line.split(" ");
        counter=counter+1;
# Running two while loops to make the pattern recursive
while True:
# Starting the time for quiz and also, creating variables to make sure that same sequences and answers are not repeated
    a = datetime.datetime.now().replace(microsecond=0)
    prevWord=0
    prevSeq=0
    # While loop to run the code till each answer is correctly answered
    while correctAnswer.count(2)!=(counter/2):
        #While loop to generate an different random number from one the generated previously
        while True:        
            word=random.randint(0,(counter/2)-1)
            if(correctAnswer[word]!=2):
                break;
            if(prevWord==word):
                continue;
        # Displaying the new keyword each time.
        print "Please Select the number which is the correct definition of the word:" ,keywords[word]
        #Generating an new sequence each time different from previous one
        while True:
            sequences =random.randint(0,2)
            if(prevSeq==sequences):
                continue;
            else:
                break
        #Generating an new incorrect answer each time different from previous one
        while True:
            incorrectAnswer=random.randint(0,len(correctAnswer)-1)
            if(incorrectAnswer==word):
                continue;
            else :
                break
        #Generating an new incorrect answer  each time different from previous one
        while True:
            incorrectAnswerSecond=random.randint(0,len(correctAnswer)-1);
            if (incorrectAnswer==incorrectAnswerSecond):
                continue
            if(incorrectAnswerSecond==word):
                continue
            else:
                break
        # Displaying the options to the user based on the sequence number generated
        if (sequences==0):
            print "1.",definition[word]
            print "2.",definition[incorrectAnswer]
            print "3.",definition[incorrectAnswerSecond]
        elif (sequences==1):
            print "1.",definition[incorrectAnswer]
            print "2.",definition[incorrectAnswerSecond]
            print "3.",definition[word]
        elif (sequences==2):
            print "1.",definition[incorrectAnswerSecond]
            print "2.",definition[word]
            print "3.",definition[incorrectAnswer]
        #Taking the answer from user
        answer = raw_input("Enter the Correct Number between 1 to 3")
        # Assign the seq and word to preseq and word
        prevSeq=sequences
        prevWord=word
        #Checking the answer if they are corret.
        if(0 == sequences):
            if(answer == "1"):
                print "success"
                correctAnswer[word]=correctAnswer[word]+1
                correctAnswerCounter=correctAnswerCounter+1
            else:
                print "Wrong Answer"
                print "Correct Answer: " ,definition[word]
                wrongAnswer=wrongAnswer+1;
        elif(1 == sequences):
            if(answer == "3"):
                print "success"
                correctAnswer[word]=correctAnswer[word]+1
                correctAnswerCounter=correctAnswerCounter+1
            else:
                print "Wrong Answer"
                print "Correct Answer: " ,definition[word]
                wrongAnswer=wrongAnswer+1;
        elif(2 == sequences):
            if(answer == "2"):
                print "success"
                correctAnswer[word]=correctAnswer[word]+1
                correctAnswerCounter=correctAnswerCounter+1
            else:
                print "Wrong Answer"
                print "Correct Answer: " ,definition[word]
                wrongAnswer=wrongAnswer+1
    # Stopping the time of the clock
    b = datetime.datetime.now().replace(microsecond=0)
    # displaying number of wrong answer and total quiz time
    print "Total Number of Wrong Answer:", wrongAnswer
    print "Total Quiz Time", (b-a)
    print "Total Number of correct Answer", correctAnswerCounter
    #asking user to reenter
    restart= raw_input("Do You want to start the quiz again Yes or No")
    if(restart=="No"):
        print "Thanks for quiz"
        break;
    elif(restart=="Yes"):
        wrongAnswer=0
        correctAnswerCounter=0;
        correctAnswer=[];
        i=0
        while (i<(counter/2)):
            i=i+1
            correctAnswer.append(0)

the text External file (keywords.txt) contains these Keyword and definitions:
cellulose
Tough substance that makes up the cell walls of green plants.
respiration
A chemical reaction that causes energy to be released from glucose.
haemoglobin
A substance which joins to oxygen and carries it round the body in blood.
ventilation
Breathing.
cartilage
Tough and smooth substance covering the ends of bones to protect them.
cytoplasm
Jelly-like part of a cell where chemical reactions happen.
nucleus
Controls what happens inside a cell.
alveoli
Tiny air sacs in the lungs.
amino acids
Produced when proteins are digested.
virus
The smallest type of microbe.
white blood cells
Can engulf bacteria or make antibodies.
photosynthesis
The process of turning carbon dioxide water and light into glucose and oxygen.
stomata
Small holes in the underside of a leaf.
vaccine
Dead or inactive forms of a microorganism.
fibre
A nutrient that cannot be digested.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140120/b47cc908/attachment-0001.html>


More information about the Tutor mailing list