I am having a lot of trouble understanding this problem. I need to enter a string in the format of 2X 4Y (e.g number letter, can be two letters such as HE, number letter
Then return the value as x24y. The values need to be distinct and need to match the dictionary symbol and be positive integers.
#enter a string in form xA yB zC
#x, y, z are postive integers
#A,B,C are distinct symbols of elements (2H 1O 1C)
symbol = {"H":1,"He":4, "Li":7, "Be":9, "B":11, "C":12, "N":14, "O":16, "F":19, "Ne":20}
user_input= input('Enter a string in the format of xA yB zC; NO SPACES')#2C 4Y
final_input = user_input.split(' ') # Makes the input into a list format// #Prints a LIST
# - [2c, 4b]
#unique_input = {*final_input}
print(final_input)
def stoi_coefficent(final_input): # this will only work when letter inputs are 'H', 'O' rather than 'HE'
output=[]
for i in final_input:
output = i[1::2]
output =i[0: :2]
print(output)
return output
def Convert(output):
output_dct = {output[i]: output[i 1] for i in range(0, len(output), 2)}
print(output_dct)
return output_dct
def check_if_in_symbol(output):
active = True
while active:
for i in output:
if i not in symbol or i not in numbers:
print('enter another symbol or number')
else:
active=False
continue
a = (stoi_coefficent(final_input))
print(output)
print(Convert(output))
b = (check_if_in_symbol(a))
I have tried various other ideas but I think I need to make the input into a dictionary. But how do I make a dictionary when the split function makes a list in the format of [2x,4y] and how would I use a comparative operator when the numbers and letters are one list item?
CodePudding user response:
You can split the input as you do (user_input.split(' ')
), then apply this to each element in the list to split numbers from letters.
Product code looks like abcd2343, how to split by letters and numbers?
You then have the symbols and numbers which you can check for validity separately.
CodePudding user response:
I'm not sure I fully understand what you're trying to do. I'm guessing that you're trying to convert an element ("He") into an int (4) and compute the final expression.
If so, you can try this (just replace "2He 5Li" with input()
):
import re
SYMBOLS = {"H":1,"He":4, "Li":7, "Be":9, "B":11, "C":12, "N":14, "O":16, "F":19, "Ne":20}
def split(split_input):
combo_strings = []
for combo in split_input:
split_str = re.split(r"(\D )", combo.strip())
if len(split_str) < 2:
print(f"Could not parse input: {combo}")
exit()
combo_strings.append(split_str[:2])
return combo_strings
def convert(combo_strings):
int_pairs = []
for pair in combo_strings:
coefficient = pair[0]
possible_symbol = pair[1].capitalize()
if possible_symbol not in SYMBOLS.keys() or not coefficient.isdigit() or int(coefficient) < 1:
print(f'Not a valid symbol: {coefficient possible_symbol}. Enter another symbol')
exit()
symbol_coeff = SYMBOLS[possible_symbol]
int_pairs.append((int(coefficient), int(symbol_coeff)))
return int_pairs
def compute(converted_ints):
final_result = 0
for ints in converted_ints:
final_result = ints[0] * ints[1]
return final_result
user_input = "2He 5Li"
split_input = user_input.split(' ')
combo_strings = split(split_input)
converted_ints = convert(combo_strings)
final_result = compute(converted_ints)
#final_result = 43