Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lashtabegav/second/task1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Використовуючи регулярні вирази, для поданого
нижче тексту замініть кожне число на його шіснадцяткове представлення.
Виведіть результат.
"""
import re

def read(q):
x=input(q)
while not re.match(r'(?:[0]\.\d+)|(?:[1-9]\d*(?:\.\d+)?)', x):
x= input(q)
return float(x)

def convert_base(num, to_base=10, from_base=10):
if isinsance(num, str):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct isinsance (...) to isinstance(...)

n = int(num, from_base)
else:
n=int(num)
alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
if n < to_base:
return alphabet[n]
else:
return convert_base(n // to_base, to_base) + alphabet[n % to_base]

number = read('Введіть число: ')
convert_base(number, to_base=16, from_base=10)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Print your result: print (convert_base(...))