Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

SQL, Python, CGI Problem: Modify the robot.cgi so that the winner is no longer j

ID: 3573683 • Letter: S

Question

SQL, Python, CGI Problem:

Modify the robot.cgi so that the winner is no longer just a 50% chance.

Instead, randomly assign “hits” until one robot has been hit a number of times equal to its hit points, at which time that robot loses and the other robot is declared the winner and has its win recorded in the database.

The result should look something like tis: http://cgi.soic.indiana.edu/~dpierz/robot_fight2.cgi (Links to an external site.)

So far, robot.cgi looks like this:

http://cgi.soic.indiana.edu/~dpierz/robot_fight.cgi

and Robot table from SQL looks like this:

1 /usr/bin/env python3 3 import cgi random, MySQLdb print Content-type: text/html ') 7 form cgi. FieldStorage 9 string 1211f16 k233 10 password my sql 1211 f16 k233 11 12 db con MySQLdb. connect (host ndiana.edu port 3 3306, user 3 string, passwd password db string) 13 14 cursor db con.cursor 15 16 def get weapon (cursor, winner) try 17 SQL SELECT Weapon FROM Robot WHERE Name +Winner 18 cursor.execute(SQL) 19 weapon cursor. fetchall [01 [01 20 except: 21 weapon something went wrong with SQL 22 return weapon 23 24 25 def update winner (cursor winner) try 26 SQL UPDATE Robot SET Wins Wins 1 WHERE Name "+winner+" 27 cursor. execute (SQL) 28 db con commit 29 except: 30 print ("Winner not updated 31 32 33 def robot fight (cursor robot1 robot2): winner random. choice ([robot1, robot2]) 34 output ""+winner+" wins the round with its +get weapon (cursor inner) 35 36 output +J Congratulations "+winner+"" update winner (cursor winner) return output 37 38 39 40 html

Explanation / Answer

def robot_fight(cursor, robot1, robot2):

    hits1 = 0

    hits2 = 0

    while robot1.hitpoint>hits1 && robot2.hitpoint>hits2:

        hit = random.random()

        if hit<0.5:

            hits1 += 1

        else:

            hits2 += 1

    if robot1.hitpoint==hits1:

        winner = robot2

    else:

        winner = robot1

#rest of the code is same