-
Notifications
You must be signed in to change notification settings - Fork 1
Don't merge until you understand what is going on here. #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,23 @@ | ||
| def letters_count(): | ||
| print('Please write your text') | ||
| input_text = input() | ||
| count_lower = 0 | ||
| def count_letters(string): | ||
| count_upper = 0 | ||
| count_lower = 0 | ||
|
|
||
| for i in input_text: | ||
| for i in string: | ||
| if (i.islower()): | ||
| count_lower = count_lower + 1 | ||
| elif (i.isupper()): | ||
| count_upper = count_upper + 1 | ||
| print('There are {} uppercase letters'.format(count_upper)) | ||
| print('There are {} lowercase letters'.format.(count_lower)) | ||
|
|
||
| letters_count() | ||
| return (count_upper, count_lower) | ||
|
|
||
| def main(): | ||
| print('Please write your text') | ||
| input_text = input() | ||
|
|
||
| results = count_letters(input_text) | ||
|
|
||
| print('There are {} uppercase letters'.format(results[0])) | ||
| print('There are {} lowercase letters'.format(results[1])) | ||
|
|
||
| if __name__ == "__main__": | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. am citit acum despre de ce se foloseste name
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uite bucata asta am explicat în comentariu mai sus. Utilitatea este că la import în alt fișier poate nu ai nevoie să fie executat automat main sau altă logică aferentă. Asta dă posibilitatea să folosești același cod în 2 moduri: ca program executabil; și ca logică reutilizabilă prin import. Spre exemplu ai o aplicație pentru conversia de valută. Scrii în consolă ceva de gen |
||
| main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from capitals import count_letters | ||
|
|
||
| assert count_letters('nfWEHI') == (4, 2), 'should be 4 uppercase letter and 2 lower case' | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assert scrie ca e un instrument pentru testare si verifica conditia care urmeaza. daca totul e True, nu se intampla nimic, daca e False apare o eroare si pot sa presupun ca apare si textul care l-ai pus intre ghilimele. capitals e un modul? sau e denumirea fisierului din care se importa functia count_letters pe care tot tu ai definit-o in fisierul cela?
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cum se porneste test_capitals.py? trebuie sa fie un alt fisier care uneste ambele capitals.py si test_capitals.py?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup, assert aruncă excepție când nu se îndeplinește condiția. capitals în cazul dat e fișier, python tratează ca modul. test-capitals.py pornești manual, adica $python test_capitals.py. Dar sunt instrumente de automatizare de gen pytest. Acolo și testele se scriu altfel, nu cu aserturi, și se pornesc ele toate automat, după numele test_*, și raportul e arătat, nu doar excepție când nu e corect. |
||
| assert count_letters('WOIJgeiegrh') == (4, 7), 'should be 4 uppercase letter and 7 lower case' | ||
| assert count_letters('gh89FJKB') != (0, 0), 'should be wrong' | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deci in main se cheama count_letters - foarte logic! :D :D I like this structure more
main e un fel de aranjare frumoasa a core-ului programului, un fel de cluster pentru toate functiile din acest fisier?
si la sfarsit se cheama doar main ca asa e mai comod/frumos?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
main e punctul de intrare în program, de unde el începe execuția. În mod obișnuit python execută toate instrucțiunile care nu-s în funcții la rând. Dar în cazul dat noi nu avem instrucțiuni care nu-s în funcții, doar
if __name__ == "__main__": main(). Și ceea ce face bucata asta doar verifică dacă fișierul a fost chemat pentru interpretare și nu pentru import.Adica în mod normal când ai instrucțiuni înafara funcțiilor și faci
$python script.pysauimport scriptîn alt fișier rezultatul va fi același, ele vor fi executate. Dar dacă vrei la import să nu fie executate atunci trebuie să înțelegi cum e apelat fișierul și bucata cu if name anume asta face, dacă e interpretat și nu importat el va executa main, dacă nu ap nu.