[Tutor] could please convert this code to 3.X by using 2to3

S Tareq stareq13 at yahoo.com
Tue Feb 18 21:18:02 CET 2014


i am to convert coding to python 3.X. and some one told me to use 2to3 program but  i don't how to use 2to3 program i was reading and followed the rules in python website ( http://docs.python.org/3/library/2to3.html?highlight=2to3#module-lib2to3 ) but it says error and don't work. is there any other way to convert. the original coding is used 2.7 python version. can you please help to convert this in any way. **sorry for bad English**  this is the coding:  


def quiz():
    print ("welcome")
    print ("""This is a quiz which will help you to learn and revise the
different Biology keywords and their definitions.

The question will be  repeated continuously until you have correctly matched every keyword with its definition exactly twice.

Good luck!
""")
    choose = int(input("1) Start Quiz  2) Exit Program "))
    if choose == 1:
        quiz2()
    elif choose == 2:
        quit()
def quiz2():
    # 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
    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
                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
                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
                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)
        #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
            correctAnswer=[];
            i=0
            while (i<(counter/2)):
                i=i+1
                correctAnswer.append(0)
quiz()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140218/72c6bded/attachment-0001.html>


More information about the Tutor mailing list