Skip to content

Instantly share code, notes, and snippets.

code = 1004001
value = 15.50
msg = "This action is not allowed"
print(f"Error {code:10d}") # code will occupy 10 characters
## 'Error 1004001'
print(f"Error {code:#X}") # code will be shown as hexa, uppercase, with 0X prefix
## 'Error 0XF51E1'
print(f"Value: {value:.4f}") # value will have 4 digits after decimal point
## 'Value: 15.5000'
>>> f"Error {code}: {msg}"
'Error 1004001: This action is not allowed'
>>> f"Error {code:#x}: {msg}"
'Error 0xf51e1: This action is not allowed'
f"Error {code}: {msg}"
## 'Error 1004001: This action is not allowed'
"Error {}: {}".format(code, msg)
## 'Error 1004001: This action is not allowed'
>>> "Error %d: %s" % (code, msg)
'Error 1004001: This action is not allowed'
msg = "This action is not allowed"
details = "Please, talk to your admin"
code = 1004001
try:
print('ok')
except ValueError:
print('value error')
except:
print('other error')
else:
print('everything went fine')
try:
print('some danger code')
except ValueError:
print('a value error occurred')
except:
print('other error happened')
import sys
correct_password = 'supersecret!'
for i in range(3):
password = input('Please, type your password:')
if password == correct_password:
break
else:
print('Sorry! You entered the wrong password many times!')
import sys
correct_password = 'supersecret!'
tries = 3
while tries > 0:
password = input('Please, type your password:')
if password == correct_password:
break