Created
March 26, 2017 18:37
-
-
Save gurpreetsingh00885/73bfd1643e6f9a68690155a57a1c6533 to your computer and use it in GitHub Desktop.
This is the solution to task 1 and 2!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"**** DON't FORGET TO HIT +1 **** It really helps." | |
import random | |
def quiz(): | |
no_of_ques=3 | |
name_of_stud=raw_input("Please Enter Your Name: ").upper() | |
score=0 | |
score2=range(no_of_ques) | |
for i in range(no_of_ques): | |
no1,no2,operator=random.randint(1,10),random.randint(1,10),random.choice(["+","-","*"]) #Generating random numbers and an operator. | |
real_ans={"+":(no1+no2),"-":(no1-no2),"*":(no1*no2)}[operator] #Generating the answer | |
print("\nQ%d. What is %d %s %d?" %((i+1),no1,operator,no2)) | |
user_ans=input("Enter your answer: ") | |
if(user_ans==real_ans): | |
print("Yeah! You are right!") | |
score+=1 | |
score2[i]=1 | |
else: | |
print("I'm afraid your answer is incorrect and %d %s %d = %d" %(no1,operator,no2,real_ans)) | |
score2[i]=0 | |
avg_score=float(score)/float(no_of_ques) | |
return (name_of_stud,score,score2[-3:],avg_score) | |
def main(): | |
class1_data,class2_data,class3_data=[],[],[] | |
no_of_students=[input("Enter no. of students in class 1: "),input("Enter no. of students in class 2: "),input("Enter no. of students in class 3: ")] | |
for i in range(3): | |
print("Class %d" %(i+1)) | |
current_class={0:class1_data,1:class2_data,2:class3_data}[i] | |
for j in range(no_of_students[i]): | |
print("Student %d" %(j+1)) | |
current_class.append(quiz()) | |
fil=open("studentdata.txt","w") | |
for x in class1_data: | |
class1_data[class1_data.index(x)]=str(x) | |
for x in class2_data: | |
class2_data[class2_data.index(x)]=str(x) | |
for x in class3_data: | |
class3_data[class3_data.index(x)]=str(x) | |
text="" | |
if len(class1_data): | |
text="*GS*00885*".join(class1_data)+"\n" | |
else: | |
text+="('', 0, [0, 0, 0], 0.0)\n" | |
if len(class2_data): | |
text+="*GS*00885*".join(class2_data)+"\n" | |
else: | |
text+="('', 0, [0, 0, 0], 0.0)\n" | |
if len(class3_data): | |
text+="*GS*00885*".join(class3_data) | |
else: | |
text+="('', 0, [0, 0, 0], 0.0)\n" | |
fil.write(text) | |
fil.close() | |
print "Data Successfully Written to 'studentdata.txt'" | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment