Created
March 12, 2020 15:30
-
-
Save Bundi-py/6704b19288d09b9efdc9a0a2660bf17f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Napiši program koji čita fajl sa informacijama o hemijskim elementima i skladišti ih u odgovarajući strukturu podataka. | |
# Potom prima input od korisnika. Ako je input integer, , onda program ispisuje simbol i naziv elementa sa tim brojem | |
# protona. Ako korisnik unese naziv ili simbol elementa, program ispisuje njegov broj protona. Takođe, program ispisuje | |
# porucu o grešci ako ne postoji elemenat koji bi odgovarao unetom nazivu, simbolu ili broju protona. Program nastavlja | |
# da učitava input sve dok nije uneta prazna linija. | |
# verzija 1: | |
tabela = [] | |
for line in open('elements.txt'): | |
line = line.strip().split(',') | |
tabela.append({'simbol': line[1], 'ime': line[2], 'broj protona': line[0]}) | |
while True: | |
user_input = input( | |
"Unesite ime, simbol ili broj protona (ili Enter za izlaz iz programa): ") | |
# Ako je user_input prazan string, prekidam izvršavanje while petlje | |
if user_input == '': | |
print('Izlazim.') | |
break | |
founded = False | |
for elements in tabela: | |
if not founded: | |
for element in elements.values(): | |
# Ako element value odgovara inputu, dodeljujem founded promenljivoj | |
# vrednost True kako bih prekinuo dalje izvršavanje prve for petlje | |
if element == user_input.title(): | |
founded = True | |
# Ukoliko je input integer, ispisujem simbol, ime i broj | |
# protona. U suprotnom, ispisujem samo broj protona | |
if element.isdigit(): | |
print(f"Simbol: {elements['simbol']} \ | |
\nIme: {elements['ime']} \ | |
\nBroj protona: {elements['broj protona']}\n") | |
else: | |
if element in elements['simbol']: | |
print(f"Ime: {elements['ime']} \ | |
\nBroj protona: {elements['broj protona']}\n") | |
elif element in elements['ime']: | |
print(f"Simbol: {elements['simbol']} \ | |
\nBroj protona: {elements['broj protona']}\n") | |
else: | |
break | |
if not founded: | |
print(f"Element {user_input} nije pronađen! Pokušajte ponovo.\n") | |
****************************************************************** | |
# verzija 2: | |
ime = {} | |
simbol = {} | |
proton = {} | |
for line in open('elements.txt'): | |
line = line.strip().split(',') | |
ime[line[2]] = [line[1], int(line[0])] | |
simbol[line[1]] = [line[2], int(line[0])] | |
proton[int(line[0])] = [line[2], line[1]] | |
user_input = input("Unesite ime, simbol ili broj protona (ili Enter za izlaz iz programa): ") | |
if user_input == '': | |
print('Izlazim.') | |
elif user_input.isdigit(): | |
print('Ime elementa je', proton[int(user_input)][0], '\ni njegov simbol je', proton[int(user_input)][1]) | |
elif user_input.title() in simbol.keys(): | |
print('Ime elementa je', simbol[user_input.title()][0], '\ni on ima', simbol[user_input.title()][1], 'protona') | |
elif user_input.title() in ime.keys(): | |
print('Simbol elementa je', ime[user_input.title()][0], '\ni on ima', ime[user_input.title()][1], 'protona') | |
else: | |
print('Uneta vrednost se ne nalazi u tabeli prirodnih elemenata.') | |
****************************************************************** | |
# verzija 3: | |
def get_test_data(): | |
test_data = [ [1,'H','Hydrogen'], [2,'He','Helium'], [3,'Li','Lithium'], | |
[4,'Be','Beryllium'], [5,'B','Boron'], [6,'C','Carbon']] | |
return test_data | |
def find_in_matrix( table, element): | |
element = element.lower() | |
for row in table: | |
for col in row: | |
if element == str(col).lower(): | |
return row | |
return [ element ] | |
def output( row ): | |
print() | |
if len( row ) == 3: | |
print('Atomic number: ', row[0]) | |
print('Symbol : ', row[1]) | |
print('Name : ', row[2]) | |
else: | |
print('Matching element not found: ', row[0]) | |
def main_loop(): | |
pt=get_test_data() | |
while True: | |
print() | |
print('Enter atomic number, chemical symbol or element name') | |
print('to get the rest of the data, or an empty line for end:') | |
search = input() | |
if len(search)==0: | |
break | |
result = find_in_matrix(pt,search) | |
output(result) | |
print() | |
print('Bye!') | |
main_loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment