fork download
  1. import sys
  2. import random
  3. import mysql.connector
  4. mydb=mysql.connector.connect(host="localhost",user="root",passwd="root",database="quiz")
  5. mycursor=mydb.cursor()
  6. def Home():
  7. f=1
  8. while f!=3:
  9. print("Welcome to Quiz")
  10. print("1. Enter Questions")
  11. print("2. Take Quiz")
  12. print("3. Exit")
  13. f=int(input("Enter your choice:"))
  14. if f==1:
  15. Question()
  16. elif f==2:
  17. Quiz()
  18. elif f==3:
  19. print("Exiting the Quiz")
  20. mycursor.close()
  21. mydb.close()
  22. sys.exit();
  23. else:
  24. Home()
  25. def Question():
  26. ch='Y';
  27. while ch=='Y' or ch=='y':
  28. print("Welcome to Question Portal")
  29. q=input("Enter the question :")
  30. op1=input("Enter the option 1 :")
  31. op2=input("Enter the option 2 :")
  32. op3=input("Enter the option 3 :")
  33. op4=input("Enter the option 4 :")
  34. ans=0
  35. while ans==0:
  36. op=int(input("Which option is correct answer (1,2,3,4)"))
  37. if op==1:
  38. ans=op1
  39. elif op==2:
  40. ans=op2
  41. elif op==3:
  42. ans=op3
  43. elif op==4:
  44. ans=op4
  45. else:
  46. print("Please choose the correct option as answer")
  47. mycursor.execute("Select * from question")
  48. data=mycursor.fetchall()
  49. qid=(mycursor.rowcount)+1
  50. mycursor.execute("Insert into question values",(qid,q,op1,op2,op3,op4,ans))
  51. mydb.commit()
  52. ch=input("Question added successfully.. Do you want to add more (Y/N)")
  53. Home()
  54. def Quiz():
  55. print("Welcome to Quiz portal")
  56. mycursor.execute("Select * from question")
  57. data=mycursor.fetchall()
  58. name=input("Enter your name :")
  59. rc=mycursor.rowcount
  60. noq=int(input("Enter the number of questions to attempt (max %s):"%rc))
  61. l=[]
  62. while len(l)!=noq:
  63. x=random.randint(1,rc)
  64. if l.count(x)>0:
  65. l.remove(x)
  66. else:
  67. l.append(x)
  68.  
  69. print("Quiz has started")
  70. c=1
  71. score=0
  72. for i in range(0,len(l)):
  73. mycursor.execute("Select * from question where qid=%s",(l[i],))
  74. ques=mycursor.fetchone()
  75. print("Q.",c,":",ques[1],"\nA.",ques[2],"\t\tB.",ques[3],"\nC.",ques[4],"\t\tD.",ques[5])
  76.  
  77. c+=1
  78. ans=None
  79. while ans==None:
  80. choice=input("Answer (A,B,C,D):")
  81. if choice=="A":
  82. ans=ques[2]
  83. elif choice=="B":
  84. ans=ques[3]
  85. elif choice=="C":
  86. ans=ques[4]
  87. elif choice=="D":
  88. ans=ques[5]
  89. else:
  90. print("Kindly select A,B,C,D as option only")
  91. if ans==ques[6]:
  92. print("Correct")
  93. score=score+1
  94. else:
  95. print("Incorrect.. Correct answer is :",ques[6])
  96. print("Quiz has ended !! Your final score is :",score)
  97. input("Press any key to continue")
  98. Home()
  99. Home()
  100.  
Success #stdin #stdout 0.02s 25884KB
stdin
Standard input is empty
stdout
import sys
import random
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="root",database="quiz")
mycursor=mydb.cursor()
def Home():
    f=1
    while f!=3:
        print("Welcome to Quiz")
        print("1. Enter Questions")
        print("2. Take Quiz")
        print("3. Exit")
        f=int(input("Enter your choice:"))
        if f==1:
            Question()
        elif f==2:
            Quiz()
        elif f==3:
            print("Exiting the Quiz")
            mycursor.close()
            mydb.close()
            sys.exit();
        else:
            Home()
def Question():
    ch='Y';
    while ch=='Y' or ch=='y':
        print("Welcome to Question Portal")
        q=input("Enter the question :")
        op1=input("Enter the option 1 :")
        op2=input("Enter the option 2 :")
        op3=input("Enter the option 3 :")
        op4=input("Enter the option 4 :")
        ans=0
        while ans==0:
            op=int(input("Which option is correct answer (1,2,3,4)"))
            if op==1:
                ans=op1
            elif op==2:
                ans=op2
            elif op==3:
                ans=op3
            elif op==4:
                ans=op4
            else:
                print("Please choose the correct option as answer")
    mycursor.execute("Select * from question")
    data=mycursor.fetchall()
    qid=(mycursor.rowcount)+1
    mycursor.execute("Insert into question values",(qid,q,op1,op2,op3,op4,ans))
    mydb.commit()
    ch=input("Question added successfully.. Do you want to add more (Y/N)")
Home()
def Quiz():
    print("Welcome to Quiz portal")
    mycursor.execute("Select * from question")
    data=mycursor.fetchall()
    name=input("Enter your name :")
    rc=mycursor.rowcount
    noq=int(input("Enter the number of questions to attempt (max %s):"%rc))
    l=[]
    while len(l)!=noq:
        x=random.randint(1,rc)
        if l.count(x)>0:
            l.remove(x)
        else:
            l.append(x)
            
    print("Quiz has started")
    c=1
    score=0
    for i in range(0,len(l)):
        mycursor.execute("Select * from question where qid=%s",(l[i],))
        ques=mycursor.fetchone()
        print("Q.",c,":",ques[1],"\nA.",ques[2],"\t\tB.",ques[3],"\nC.",ques[4],"\t\tD.",ques[5])

        c+=1
        ans=None
        while ans==None:
            choice=input("Answer (A,B,C,D):")
            if choice=="A":
                ans=ques[2]
            elif choice=="B":
                ans=ques[3]
            elif choice=="C":
                ans=ques[4]
            elif choice=="D":
                ans=ques[5]
            else:
                print("Kindly select A,B,C,D as option only")
    if ans==ques[6]:
        print("Correct")
        score=score+1
    else:
        print("Incorrect.. Correct answer is :",ques[6])
    print("Quiz has ended !! Your final score is :",score)
    input("Press any key to continue")
    Home()
Home()