Skip to content

How to write effective code

ExWiCo edited this page Apr 30, 2023 · 3 revisions

Here are some examples:

1

TURN THIS:
print("There was a man named " + character_name + ".")
INTO THIS:
print(f"There was a man named {character_name}.")

2

TURN THIS:
inp = input()
if inp == "a":
print(1)
elif inp == "A":
print(1)
INTO THIS:
inp = input()
if inp == "a" or inp == "A": #1
print(1)

3

TURN THIS: a, A = 1, 1 INTO THIS: a = A = 1

4

TURN THIS: i = i + 1 INTO THIS: i += 1

5

TURN THIS:
message_pt1 = "Veritas"
message_pt2 = "means \"truth\"."
print(f"\n{message_pt1} {message_pt2}")
INTO THIS:
message = ["Veritas", "means \"truth\"."]
print(f"\n{message[0]} {message[1]}")

6

TURN THIS:
accepted_1 = "cage"
accepted_2 = "dragon"
accepted_3 = "key"
guess = input("Give me a password: ")

if guess == accepted_1 or guess == accepted_2 or guess == accepted_3:
print("Hi")
INTO THIS:
accepted_passwords = ["cage", "dragon", "key"]
guess = input("Give me a password: ")

if guess in accepted_passwords:
print("Hi")

Clone this wiki locally