![](/img/trans.png)
[英]How can I use use a regex to match characters that aren't included in certain words?
[英]How to check if certain characters aren't in a string?
import os
class Converter(object):
def dec_to_any(self, a, b):
self.values = {0: "0", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9", 10: "A", 11: "B", 12: "C", 13: "D", 14: "E", 15: "F", 16: "G", 17: "H", 18: "I", 19: "J", 20: "K", 21: "L", 22: "M", 23: "N", 24: "O", 25: "P", 26: "Q", 27: "R", 28: "S", 29: "T", 30: "U", 31: "V"}
self.universal = []
self.temp_uni = []
while a != 0:
if a < b:
self.universal.append(self.values.get(a, "unavailable"))
a -= a
else:
self.universal.append(self.values.get((a % b), "unavailable"))
a //= b
for i in range(len(self.universal) + 1):
self.temp_uni.append(self.universal[-i])
del self.temp_uni[0]
self.universal = ""
for i in self.temp_uni:
self.universal += str(i)
return self.universal
def any_to_dec(self, a, b):
a = int(a, b) #problem part 3 start and end
return a
def display(self):
print("Hello and welcome to the base converter\nHere is the menu:\n\t0 - close\n\t1 - convert\n\n")
def main(self):
while True:
try:
self.display()
self.option = int(input("Please select one: "))
while self.option > 1 or self.option < 0:
os.system("cls")
self.display()
self.option = int(input("Please select one: "))
os.system("cls")
break
except:
os.system("cls")
pass
if self.option == 1:
while True:
try:
self.base1 = int(input("Please enter a base to convert from (2-32): "))
while self.base1 < 2 or self.base1 > 33:
os.system("cls")
self.base1 = int(input("Please enter a base to convert from (2-32): "))
break
except:
os.system("cls")
pass
while True:
try:
self.number = str(input("Please enter a number to convert: ")).upper()
break
except:
os.system("cls")
pass
while True: #problem part 1 start
try:
self.base2 = int(input("Please enter a base to convert to (2-36): "))
while self.base2 < 2 or self.base2 > 37:
os.system("cls")
self.base2 = int(input("Please enter a base to convert to (2-36): "))
break
except:
os.system("cls")
pass #problem part 1 end
os.system("cls")
self.return_val = self.any_to_dec(self.number, self.base1) #problem part 2 start and end
self.return_val = self.dec_to_any(self.return_val, self.base2)
print(self.number, "in base", self.base1, "would be",
self.return_val, "in base", self.base2)
start = Converter()
start.main()
這是代碼,僅供參考。 我正在制作一個基本轉換器,用戶在其中選擇一個要與之進行轉換的基本並輸入數字,轉換器吐出轉換后的數字。 我的困境是,用戶可以輸入例如2的基數,並且超出輸入的范圍,最終會導致錯誤,我嘗試通過添加if語句來檢查他們所處的基數,並通過while循環來表示直到繼續,以防止發生錯誤沒有引起字符的錯誤。 但這很煩人。 有更自動化的方法嗎? 喜歡:
base_2 = ["0", "1"]
如果存在任何不在base_2
:
some prevention code....
擴展我的評論,您可以從string
模塊中獲取數字和字母。 下面是基數為1-36的不區分大小寫的文字。
from string import digits, ascii_lowercase
characters = digits + ascii_lowercase
def is_valid(literal, base):
valid_digits = characters[:base]
return set(valid_digits).issuperset(literal.lower())
is_valid('01010101', 2)
# True
is_valid('100101a', 2)
# False
is_valid('DeadBeef', 16)
# True
is_valid('123z', 16)
# False
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.