wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
<hr></hr>
<hr>
Self-closing.
HTML
<?php // code ?>
<?php // code ?>
Correct.
PHP
int arr[77]; arr[77]=5;
int arr[77]; if(77<77){{}} else arr[77]=5;
Bounds check.
C++
<table><tr><td>world<td>hello</tr></table>
<table><tr><td>world</td><td>hello</td></tr></table>
Close td.
HTML
[23, 15, 4
[23, 15, 4]
Close bracket.
Python
["world", 81]
["world", 81]
Correct.
JSON
with open('input.csv') as fh: data = fh.read()
with open('input.csv') as fh: data = fh.read()
Correct.
Python
fs.readFile('data.txt', (err,data) => {{ if(err) throw err; }});
fs.readFile('data.txt', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
for result in range(80) print(result)
for result in range(80): print(result)
Colon after for.
Python
<p>message <b>world</p></b>
<p>message <b>world</b></p>
Nest properly.
HTML
class = 'info'
class_name = 'info'
'class' is a keyword.
Python
for (count in items)
for (count of items)
for...in iterates keys.
JavaScript
const c;
const c = 29;
Initialize const.
JavaScript
lambda x: x+1
lambda x: x+1
Correct lambda.
Python
WHERE email = '1'
WHERE email = 1
Don't quote integer.
SQL
if a = 54
if a == 54
Use ==.
Go
def bar(): print('test')
def bar(): print('test')
Indent function body.
Python
Write-Host 'output'
Write-Host 'output'
Correct.
PowerShell
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
c == '13'
c === 13
Use strict equality.
JavaScript
let num: number = 'info';
let num: string = 'info';
Fix type.
TypeScript
items(29)
if length(items) >= 29, items(29), end
Check length.
MATLAB
while temp > 92 temp -= 1
while temp > 92: temp -= 1
Colon missing after while.
Python
jwt.sign({{id:8}}, 'key');
jwt.sign({{id:8}}, 'key', {{expiresIn:'2h'}});
Add expiration.
Node.js
<person age=84>
<person age="84">
Quote attribute.
XML
x := 86
x := 86
Correct.
Go
h1 {{ font-size:74px color:#333; }}
h1 {{ font-size:74px; color:#333; }}
Add semicolon.
CSS
raise 'hello'
raise Exception('hello')
Raise needs an exception class.
Python
val item = 28; item = 90
var item = 28; item = 90
Use var for reassignment.
Scala
echo hello world
echo 'hello world'
Quote to prevent splitting.
Shell
'result' + 29
'result' + 29.to_s
Convert int.
Ruby
function test(): void {{ return 59; }}
function test(): number {{ return 59; }}
Return type mismatch.
TypeScript
if z = 33
if z == 33
Use ==.
MATLAB
if ($foo = 53)
if ($foo == 53)
Use ==.
Perl
const person:Person = {{name:'world'}};
const person:Person = {{name:'world', age:39}};
Add missing property.
TypeScript
SELECT age status FROM orders;
SELECT age, status FROM orders;
Add comma.
SQL
int x; System.out.println(x);
int x = 0; System.out.println(x);
Initialize variable.
Java
print('world')
print('world')
Correct.
R
fn compute() -> i32 {{ 62 }}
fn compute() -> i32 {{ 62 }}
Correct.
Rust
#content {{ color: #fff; }}
#content {{ color: #fff; }}
Correct.
CSS
else print('world')
else: print('world')
Colon after else.
Python
if ($foo = 92) {{}}
if ($foo -eq 92) {{}}
Use -eq.
PowerShell
local item = 16
local item = 16
Correct.
Lua
<input type='text' value='output'>
<input type='text' value='output' name='value'>
Add name attribute.
HTML
function baz() {{ echo 'hello'; }}
function baz() {{ echo 'hello'; }}
Correct.
PHP
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
print 'data'
print('data')
print needs parentheses.
Python
let text1 = String::from("test"); let text2 = text1; println!("{{}}", text1);
let text1 = String::from("test"); let text2 = text1.clone(); println!("{{}}", text1);
Clone to avoid move.
Rust
fmt.Println 'value'
fmt.Println('value')
Missing parentheses.
Go
val y = 'output'
val y = "output"
Double quotes.
Kotlin
if c > 91 print('info')
if c > 91: print('info')
Colon missing after if.
Python
class Item {{ int foo; }};
class Item {{ public: int foo; }};
Make public.
C++
class Child Entity:
class Child(Entity):
Inheritance uses parentheses.
Python
z = 40
z=40
No spaces.
Shell
for (int i=0; i<82; i++) {{}}
for (int i=0; i<82; i++) {{}}
Correct.
Java
if z = 2 {{}}
if z == 2 {{}}
Use ==.
Swift
<user><desc>output</desc><age>7</age></user
<user><desc>output</desc><age>7</age></user>
Add closing >.
XML
<img src='hello.jpg'>
<img src='hello.jpg' alt='desc'>
Add alt text.
HTML
if (temp) console.log('yes') else console.log('no')
if (temp) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
data[16]
if data.indices.contains(16) {{ data[16] }}
Check index.
Swift
const int x; x = 5;
const int x = 5;
Const must be initialized.
C++
function render(val:string){{return val;}} render(49);
function render(val:string){{return val;}} render('output');
Pass correct type.
TypeScript
if (a = 87) {}
if (a == 87) {}
Use ==.
Dart
UPDATE users SET name='info' WHERE role=50
UPDATE users SET name='info' WHERE role=50;
Add semicolon.
SQL
DELETE FROM products WHERE name=80
DELETE FROM products WHERE name=80;
Add semicolon.
SQL
if (data = 43) {{}}
if (data == 43) {{}}
Use ==.
Kotlin
void test(); int main(){{test();}}
void test(); // prototype int main(){{test();}}
Declare before use.
C++
println('hello')
println("hello")
Double quotes.
Scala
class Product def method end end
class Product def method end end
Correct.
Ruby
int[] list = new int[93]; list[93] = 5;
int[] list = new int[93]; if (93 < list.length) list[93] = 5;
Check bounds.
Java
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
for i=1,74 do print(i) end
for i=1,74 do print(i) end
Correct.
Lua
try {{ throw 'message'; }} catch(e) {{}}
try {{ throw new Error('message'); }} catch(e) {{}}
Throw Error objects.
JavaScript
let v=vec![14,86,34]; let primary=&v[0]; v.push(63);
let mut v=vec![14,86,34]; let primary=v[0]; v.push(63);
Copy instead of reference.
Rust
def handle(b): return b + 1
def handle(b): return b + 1
Correct.
Python
let count = 'world'
let count = "world"
Double quotes.
Swift
<a href='https://example.com' target='_blank'>
<a href='https://example.com' target='_blank' rel='noopener'>
Add rel for security.
HTML
<note name='info'/>
<note name="info"/>
Double quotes.
XML
data[47]
if (data.indices.contains(47)) data[47]
Check index.
Kotlin
if (item = 68)
if (item == 68)
Use ==.
Scala
[27, 71, 10
[27, 71, 10]
Close bracket.
Ruby
let temp = 13; let temp = 75;
let temp = 13; temp = 75;
Duplicate declaration.
JavaScript
if (x = 58)
if (x == 58)
Use ==.
C++
SELECT * FROM users WHRE email=77;
SELECT * FROM users WHERE email=77;
Fix WHERE.
SQL
var x int = 'info'
var x string = 'info'
Type mismatch.
Go
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
name: message age: 96
name: message age: 96
Correct.
YAML
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('hello')); app.listen(86);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('hello')); app.listen(86, () => console.log('listening'));
Add callback.
Node.js
yield item
yield item
Correct yield.
Python
div {{ color=green; }}
div {{ color: green; }}
Use colon.
CSS
// comment
/* comment */
Use /* */.
CSS
if foo = 7
if foo == 7
Use ==.
Ruby
print 'hello'
print('hello')
Parentheses for function call.
Lua
void main() {{ print('result') }}
void main() {{ print('result'); }}
Add semicolon.
Dart
object Product {{ def main(args: Array[String]) = println("output") }}
object Product {{ def main(args: Array[String]): Unit = println("output") }}
Add return type Unit.
Scala
let z = 75; z += 1;
let mut z = 75; z += 1;
Need mut to modify.
Rust
{{'name':92, 'status' 69}}
{{'name':92, 'status':69}}
Colon missing.
Python
<ul><li>test<li>test</ul>
<ul><li>test</li><li>test</li></ul>
Close li.
HTML
function render() {{ return {{key:'test'}} }}
function render() {{ return {{key:'test'}}; }}
Return object on same line.
JavaScript