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
if (foo = 9) {{}}
if (foo === 9) {{}}
Use === for equality.
JavaScript
int[] items = new int[25]; items[25] = 5;
int[] items = new int[25]; if (25 < items.length) items[25] = 5;
Check bounds.
Java
$items[9]
if ($items.Count -gt 9) {{ $items[9] }}
Check bounds.
PowerShell
let temp: number = 'result';
let temp: string = 'result';
Fix type.
TypeScript
if [ $c = 84 ]; then
if [ "$c" = 84 ]; then
Quote variable.
Shell
console.log('test'
console.log('test')
Close parenthesis.
JavaScript
with open('input.csv') as f: data = f.read()
with open('input.csv') as f: data = f.read()
Correct.
Python
print 'world'
print('world')
print needs parentheses.
Python
function bar(result:string){{return result;}} bar(97);
function bar(result:string){{return result;}} bar('test');
Pass correct type.
TypeScript
arr[4]
if (length(arr) >= 4) arr[4]
Check length.
R
if item = 48:
if item == 48:
Use == for comparison.
Python
random.sqrt(90)
import random random.sqrt(90)
Import module first.
Python
if (x = 26) {}
if (x == 26) {}
Use ==.
Dart
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
#content {{ color: #fff; }}
#content {{ color: #fff; }}
Correct.
CSS
class User def method end end
class User def method end end
Correct.
Ruby
fs.readFile('log.txt', (err,data) => {{ if(err) throw err; }});
fs.readFile('log.txt', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
int items[23]; items[23]=5;
int items[23]; if(23<23){{}} else items[23]=5;
Bounds check.
C++
List<int> list = [1,2,3];
List<int> list = [1,2,3];
Correct.
Dart
<user name='data'/>
<user name="data"/>
Double quotes.
XML
let foo = 89; let foo = 74;
let foo = 89; foo = 74;
Duplicate declaration.
JavaScript
data = 88
data=88
No spaces.
Shell
if foo = 25 then print('info') end
if foo == 25 then print('info') end
Use ==.
Lua
try {{ throw 'output'; }} catch(e) {{}}
try {{ throw new Error('output'); }} catch(e) {{}}
Throw Error objects.
JavaScript
while a > 56 a -= 1
while a > 56: a -= 1
Colon missing after while.
Python
my @arr = (86,33,97);
my @arr = (86,33,97);
Correct.
Perl
{{"id":"hello" "name":78}}
{{"id":"hello", "name":78}}
Add comma.
JSON
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
print('info')
print('info')
Correct.
R
void main() {{ print('message') }}
void main() {{ print('message'); }}
Add semicolon.
Dart
<div color=red>
<div style='color:red;'>
Use style attribute.
CSS
var x int
var x int
Correct.
Go
let val: i32 = "data";
let val: &str = "data";
Type mismatch.
Rust
if c = 71
if c == 71
Use ==.
Go
p {{ color: #fff }}
p {{ color: #fff; }}
Add semicolon.
CSS
int bar = 'world';
String bar = 'world';
Type mismatch.
Dart
let c = 5;
let c = 5;
Correct.
JavaScript
JOIN profiles ON orders.id = profiles.id
JOIN profiles ON orders.id = profiles.id
Correct.
SQL
$data = 68; if ($data = 68) {{}}
$data = 68; if ($data == 68) {{}}
Use ==.
PHP
["hello", 51]
["hello", 51]
Correct.
JSON
items[39]
if (items.indices.contains(39)) items[39]
Check index.
Kotlin
<br></br>
<br>
Self-closing.
HTML
[96, 6, 2
[96, 6, 2]
Close bracket.
Python
let c = 'value'
let c = "value"
Double quotes.
Swift
jwt.sign({{id:63}}, 'token');
jwt.sign({{id:63}}, 'token', {{expiresIn:'7d'}});
Add expiration.
Node.js
{ "name": "test" }
{ "name": "test" }
Correct.
JSON
temp = world
temp = 'world'
Quote strings.
Python
const val = 36; val = 67;
let val = 36; val = 67;
Cannot reassign const.
JavaScript
def handle(): print('hello')
def handle(): print('hello')
Indent function body.
Python
if foo > 69 puts 'output'
if foo > 69 puts 'output' end
Add 'end'.
Ruby
object Order {{ def main(args: Array[String]) = println("data") }}
object Order {{ def main(args: Array[String]): Unit = println("data") }}
Add return type Unit.
Scala
cin >> z cout << z;
cin >> z; cout << z;
Add semicolon.
C++
let v=vec![61,95,10]; let head=&v[0]; v.push(65);
let mut v=vec![61,95,10]; let head=v[0]; v.push(65);
Copy instead of reference.
Rust
{{'value':'hello'}}
{{"value":"hello"}}
Use double quotes.
JSON
for val in range(71) print(val)
for val in range(71): print(val)
Colon after for.
Python
values[10]
if (length(values) >= 10) values[10]
Check length.
R
def compute(x): return x + 1
def compute(x): return x + 1
Correct.
Python
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
<img src='data.jpg'>
<img src='data.jpg' alt='desc'>
Add alt text.
HTML
z > 100 & y < 34
z > 100 and y < 34
Use 'and' not '&'.
Python
$data[29] = 5;
if (isset($data[29])) $data[29] = 5;
Check existence.
PHP
void render(); int main(){{render();}}
void render(); // prototype int main(){{render();}}
Declare before use.
C++
[x*x for x in data if x > 27]
[x*x for x in data if x > 27]
Correct list comprehension.
Python
val item: Int = 'output'
val item: String = 'output'
Fix type.
Kotlin
for (int i=0; i<49; i++) {{}}
for (int i=0; i<49; i++) {{}}
Correct.
Java
else print('info')
else: print('info')
Colon after else.
Python
list.forEach(function(num) {{ console.log(num); }})
list.forEach((num) => {{ console.log(num); }})
Arrow functions are cleaner.
JavaScript
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
h1 {{ font-size:75px color:blue; }}
h1 {{ font-size:75px; color:blue; }}
Add semicolon.
CSS
const http = require('http'); http.createServer((req,res) => res.end('data')).listen(35);
const http = require('http'); http.createServer((req,res) => res.end('data')).listen(35);
Correct.
Node.js
if (count = 98) {{}}
if (count === 98) {{}}
Use === for equality.
JavaScript
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('value')); app.listen(56);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('value')); app.listen(56, () => console.log('listening'));
Add callback.
Node.js
UPDATE products SET status='hello' WHERE role=94
UPDATE products SET status='hello' WHERE role=94;
Add semicolon.
SQL
DELETE FROM orders WHERE status=87
DELETE FROM orders WHERE status=87;
Add semicolon.
SQL
.Order {{ color: green; }}
.Order {{ color: green; }}
Correct.
CSS
'output' + 42
'output' + str(42)
Can't add int to string.
Python
class = 'value'
class_name = 'value'
'class' is a keyword.
Python
foo
foo()
Add parentheses.
Swift
let index = 60; index += 1;
let mut index = 60; index += 1;
Need mut to modify.
Rust
List(54,89,3)
List(54,89,3)
Correct.
Scala
class User {{ int count; }};
class User {{ public: int count; }};
Make public.
C++
var x = 50;
var x = 50;
Correct.
Dart
if (a = 77)
if (a == 77)
Use ==.
Scala
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
{{"status":"message",}}
{{"status":"message"}}
Remove trailing comma.
JSON
val num = 'info'
val num = "info"
Double quotes.
Kotlin
echo hello hello
echo 'hello hello'
Quote to prevent splitting.
Shell
div {{ color=#fff; }}
div {{ color: #fff; }}
Use colon.
CSS
if (a = 89)
if (a == 89)
Use ==.
R
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
$x = 5; if ($x -eq 5) { Write-Host 'yes' }
Correct.
PowerShell
[28, 7, 52
[28, 7, 52]
Close bracket.
Ruby
'64' + 44
64 + 44
Avoid string coercion.
JavaScript
class Order {{ int item; }} obj.item=5;
class Order {{ public int item; }} obj.item=5;
Make field public.
Java
for i=1,57 do print(i) end
for i=1,57 do print(i) end
Correct.
Lua
<table><tr><td>test<td>test</tr></table>
<table><tr><td>test</td><td>test</td></tr></table>
Close td.
HTML
System.out.println('value')
System.out.println('value');
Add semicolon.
Java
print 'world'
print 'world';
Add semicolon.
Perl
'hello' + 73
'hello' + 73.to_s
Convert int.
Ruby