SQL, Python, CGI Problem: Modify the robot.cgi so that the winner is no longer j
ID: 3574195 • 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 htmlExplanation / Answer
Please find below the complete content of robot.cgi :
import cgi
import random
print('Content-type: text/html ')
import MySQLdb
def createRobotTable(cursor): // createRobotTable
SQL = """CREATE TABLE Robot //Create table statement
(RobotID varchar (10) UNIQUE primary key ,
Name varchar (20) NOT NULL,
Weapon varchar (20) NOT NULL,
HitPoints int (11) default 5);"""
cursor.execute(SQL)
def insertRobot(cursor, robotID, name, weapon, hitpoints): //inserting datas into the table
SQL = "INSERT INTO Robot (RobotID, Name, Weapon, HitPoints)"
SQL += "VALUES ('" + str(robotID) + "', '" + name + "', '" + weapon + "', '"
SQL += "'" + hitpoints + "');"
cursor.execute(SQL)
robotExampleList = [ //This is robot exampleList
["ah6d8", "Megaton", "Gun", 100],
["kjqhw","BB-8", "Gun", 100],
["hdj78","Commander Data", "Gun", 100],
["jdhh78","Terminator", "Gun", 100],
["256hs","Roomba", "Gun", 100]
]
string = "i211f16_jk233"
password = "my+sql=i211f16_jk233" //password
db_con = MySQLdb.connect(host="db.soic.indiana.edu", port = 3306, user=string, passwd=password, db=string) //db connection string
cursor = db_con.cursor()
for robot in robotExampleList:
insertRobot(cursor, robot[0], robot[1], robot[2], robot[3])
form = cgi.FieldStorage()
didFight = form.getfirst('didFight',True)
if didFight == True: //condition
print("did fight")
else:
html = """
<html>
<head>
<title>Robot Fight!</title>
</head>
<body>
<H1>Choose two robots to face off!</H1><hr />
<FORM method="post" action="robot_fight.cgi">
<H3>Please select robots:</H3>
<p> Robot Name:
<select name="robot1">
{options}
</select>
<select name="robot2">
{optionsSecond}
</select>
</p>
<input type="submit" value="Fight!" />
</FORM>
<hr /></body>
</html>"""
cursor.execute("SELECT * FROM Addresses;") //Selecting every column from table Addresses
results = cursor.fetchall()
optionsStringHTML = ""
for row in results:
optionsStringHTML+="<option value='"+row[1]+"'>"+row[1]+"</option>"
print(html.format(options = optionsStringHTML, optionsSecond = optionsStringHTML))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.