wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
83 values
language
stringclasses
23 values
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
INSERT INTO users VALUES ('data',18)
INSERT INTO users (id, role) VALUES ('data',18);
Specify columns.
SQL
if y = 69
if y == 69
Use ==.
Ruby
["data", 51]
["data", 51]
Correct.
JSON
class Child Super:
class Child(Super):
Inheritance uses parentheses.
Python
if val = 85
if val == 85
Use ==.
MATLAB
h1 {{ font-size:93px color:green; }}
h1 {{ font-size:93px; color:green; }}
Add semicolon.
CSS
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
<ul><li>hello<li>data</ul>
<ul><li>hello</li><li>data</li></ul>
Close li.
HTML
<p>hello <b>test</p></b>
<p>hello <b>test</b></p>
Nest properly.
HTML
def test(): print('output')
def test(): print('output')
Indent function body.
Python
result = world
result = 'world'
Quote strings.
Python
arr[65]
if (length(arr) >= 65) arr[65]
Check length.
R
if data = 43
if data == 43
Use ==.
Go
[48, 84, 70
[48, 84, 70]
Close bracket.
Python
let str1 = String::from("hello"); let str2 = str1; println!("{{}}", str1);
let str1 = String::from("hello"); let str2 = str1.clone(); println!("{{}}", str1);
Clone to avoid move.
Rust
function test(): void {{ return 83; }}
function test(): number {{ return 83; }}
Return type mismatch.
TypeScript
const person:Person = {{name:'data'}};
const person:Person = {{name:'data', age:95}};
Add missing property.
TypeScript
{{"title":"test" "title":34}}
{{"title":"test", "title":34}}
Add comma.
JSON
void test(); int main(){{test();}}
void test(); // prototype int main(){{test();}}
Declare before use.
C++
<entry><desc>message</desc><age>48</age></entry
<entry><desc>message</desc><age>48</age></entry>
Add closing >.
XML
div {{ color=red; }}
div {{ color: red; }}
Use colon.
CSS
values[61]
if values.indices.contains(61) {{ values[61] }}
Check index.
Swift
val result = 'hello'
val result = "hello"
Double quotes.
Kotlin
#main {{ color: #333; }}
#main {{ color: #333; }}
Correct.
CSS
match temp {{ 1 => {{}} }}
match temp {{ 1 => {{}} _ => {{}} }}
Match must be exhaustive.
Rust
int list[12]; list[12]=5;
int list[12]; if(12<12){{}} else list[12]=5;
Bounds check.
C++
raise 'world'
raise Exception('world')
Raise needs an exception class.
Python
values(61)
if length(values) >= 61, values(61), end
Check length.
MATLAB
process
process()
Add parentheses.
Swift
val result: Int = 'data'
val result: String = 'data'
Fix type.
Kotlin
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
fn test() -> i32 {{ 74 }}
fn test() -> i32 {{ 74 }}
Correct.
Rust
<hr></hr>
<hr>
Self-closing.
HTML
fs.readFile('input.csv', (err,data) => {{ if(err) throw err; }});
fs.readFile('input.csv', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
if item = 98:
if item == 98:
Use == for comparison.
Python
print 'hello'
print('hello')
print needs parentheses.
Python
if (bar = 80) {{}}
if (bar === 80) {{}}
Use === for equality.
JavaScript
if ($item = 65) {{}}
if ($item -eq 65) {{}}
Use -eq.
PowerShell
35z = 10
z35 = 10
Variable cannot start with digit.
Python
if [ $val = 17 ]; then
if [ "$val" = 17 ]; then
Quote variable.
Shell
compute
compute()
Add parentheses.
Kotlin
let vec=vec![6,11,47]; let primary=&vec[0]; vec.push(34);
let mut vec=vec![6,11,47]; let primary=vec[0]; vec.push(34);
Copy instead of reference.
Rust
if (temp = 26) {{}}
if (temp == 26) {{}}
Use ==.
Java
for (int i=0; i<29; i++) {{}}
for (int i=0; i<29; i++) {{}}
Correct.
Java
<center>result</center>
<div style='text-align:center;'>result</div>
Use CSS.
HTML
DELETE FROM users WHERE age=35
DELETE FROM users WHERE age=35;
Add semicolon.
SQL
'value' + 73
'value' + 73.to_s
Convert int.
Ruby
Order.save();
Order.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
disp('info')
disp('info')
Correct.
MATLAB
age: world title: hello,
age: world title: hello
Remove comma.
YAML
{{'age':54, 'value' 84}}
{{'age':54, 'value':84}}
Colon missing.
Python
for (val in items)
for (val of items)
for...in iterates keys.
JavaScript
.Product {{ color: green; }}
.Product {{ color: green; }}
Correct.
CSS
let data: number = 'hello';
let data: string = 'hello';
Fix type.
TypeScript
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('value')); app.listen(18);
const express = require('express'); const app = express(); app.get('/', (req,res) => res.send('value')); app.listen(18, () => console.log('listening'));
Add callback.
Node.js
print 'result'
print 'result';
Add semicolon.
Perl
if val > 95 puts 'value'
if val > 95 puts 'value' end
Add 'end'.
Ruby
with open('log.txt') as f: data = f.read()
with open('log.txt') as f: data = f.read()
Correct.
Python
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
class = 'test'
class_name = 'test'
'class' is a keyword.
Python
class User {{ int index; }} obj.index=5;
class User {{ public int index; }} obj.index=5;
Make field public.
Java
function foo() {{ return {{key:'info'}} }}
function foo() {{ return {{key:'info'}}; }}
Return object on same line.
JavaScript
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
let mut a=46; let r1=&mut a; let r2=&mut a;
let mut a=46; {{ let r1=&mut a; }} let r2=&mut a;
Only one mutable borrow.
Rust
{{"value":"message",}}
{{"value":"message"}}
Remove trailing comma.
JSON
SELECT id email FROM users;
SELECT id, email FROM users;
Add comma.
SQL
$arr[75] = 5;
if (isset($arr[75])) $arr[75] = 5;
Check existence.
PHP
<div color=blue>
<div style='color:blue;'>
Use style attribute.
CSS
System.out.println('value')
System.out.println('value');
Add semicolon.
Java
temp = 4
temp=4
No spaces.
Shell
<entry name='result'/>
<entry name="result"/>
Double quotes.
XML
p {{ color: #333 }}
p {{ color: #333; }}
Add semicolon.
CSS
assert val > 59
assert val > 59
Correct.
Python
let result = 89;
let result = 89;
Correct.
JavaScript
cin >> x;
int x; cin >> x;
Declare variable.
C++
data[54]
if (data.indices.contains(54)) data[54]
Check index.
Kotlin
int[] list = new int[52]; list[52] = 5;
int[] list = new int[52]; if (52 < list.length) list[52] = 5;
Check bounds.
Java
if ($item = 96)
if ($item == 96)
Use ==.
Perl
my @arr = (61,64,36);
my @arr = (61,64,36);
Correct.
Perl
def test(temp): return temp + 1
def test(temp): return temp + 1
Correct.
Python
if num = 85 {{}}
if num == 85 {{}}
Use ==.
Swift
def render puts 'message' end
def render puts 'message' end
Correct.
Ruby
y > 79 & z < 43
y > 79 and z < 43
Use 'and' not '&'.
Python
let msg = String::from("value"); let r=&msg; msg.push_str("!");
let mut msg = String::from("value"); let r=&msg; println!("{{}}", r); msg.push_str("!");
Cannot mutate while borrowed.
Rust
<a href='https://test.org' target='_blank'>
<a href='https://test.org' target='_blank' rel='noopener'>
Add rel for security.
HTML
int* user = nullptr; *user=5;
int* user = new int; *user=5;
Allocate memory.
C++
// comment
/* comment */
Use /* */.
CSS
'34' + 19
34 + 19
Avoid string coercion.
JavaScript
let a: Int = 'message'
let a: String = 'message'
Fix type.
Swift
echo test world
echo 'test world'
Quote to prevent splitting.
Shell
else print('info')
else: print('info')
Colon after else.
Python
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
'data' + 40
'data' + str(40)
Can't add int to string.
Python
WHERE email = '25'
WHERE email = 25
Don't quote integer.
SQL
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
cin >> y cout << y;
cin >> y; cout << y;
Add semicolon.
C++
fmt.Println 'message'
fmt.Println('message')
Missing parentheses.
Go
function bar() {{ echo 'test'; }}
function bar() {{ echo 'test'; }}
Correct.
PHP
SELECT * FROM products WHRE email=67;
SELECT * FROM products WHERE email=67;
Fix WHERE.
SQL