Open utterances-bot opened 3 years ago
Hello everyone! Today we will be combining knowledge from DAY 1, DAY 2 and DAY 3. This time we will be coding using version control and unit testing.
The tests will help you to make sure that the code you write complies with certain standards and specifications. Each exercise has a test and the level of difficulty increases as you progress with the exercise.
In the second exercise I am getting the following error even though I have a hello()
function defined in gizmo.py
. What am I missing?
AttributeError Traceback (most recent call last)
<ipython-input-21-5eb3a3862f66> in <module>
1 import gizmo
----> 2 gizmo.hello("a", "b")
AttributeError: module 'gizmo' has no attribute 'hello'
@cforgaci hello should be a function within the gizmo.py
can you share your current code at the moment in a comment like so?:
def function(argument):
return "Something"
Another way to let us look at your code is by pushing it to your GitHub repository and sending us a link to see the current status ;)
Hi, I wrote some code in the py, but I don't understand how to run the file with the import command like described in exercise 1. My code:
def hello(name,country='Finland'):
print('Hello'+ name + ', how are things in ' + country)
@jurra, this is the link to the file: https://github.com/cforgaci/gizmo/blob/1fa3fc8a21cce9069fac0d84e3e7992ecba87706/gizmo.py
@jurra, it works if I type the function to the python console, but it does not if I import gizmo.py
as a module with import gizmo
@MertenNefs we are still in office hours, maybe is best to address the issue there. Your code looks good so far ;)
@MertenNefs the first thing I see from your function is that you are not returning a value, instead you could write:
def hello(name,country='Finland'):
return 'Hello'+ name + ', how are things in ' + country
For this particular exercise of the gizmo challenge I see that the spec or test accepts a print statement. Here is another example of how the string could be templated.
def hello(name, country='Finland'):
print("Hello %s, how are things in %s?" % (name, country))
@cforgaci I read this comment but I was not able to understand it in detail:
@jurra, it works if I type the function to the python console, but it does not if I import gizmo.py as a module with import gizmo
You can use the python interpreter to test the module like so:
>>> import gizmo
>>> gizmo.hello("Name", "Country")
For running the test and making sure it passes you can run this command in the terminal:
python -m pytest .tests/test_gizmo.py
@jurra, I figured it out. For some reason, importing the function as a module did not work although the code was correct, but after I restarted the python console, it did. Now it passed the test too!
Nice troubleshooting Claudiu! Glad you figured it out.
def spell(name):
spelled = ''
for l in name:
if (l == name[-1]):
# works: spelled = spelled + l
spelled += l # better
else:
spelled += l +'.'
print(spelled)
This one worked for exercise 3. At first, the letters got printed in seperate lines underneath each other, as I was printing them seperately in the loop. It took me a while to find out how to print the whole string at once;) And I got a tip of += to add stuff to an existing variable.
oh no, it didn't work for Frankfurter, because there is another r in there...
@MertenNefs the challenge continues ;) nice work!
def spell(name):
spelled = ''
for l in name:
spelled += l + '.' # spelled + l + '.'
print(spelled[:-1])
phew...
this also works:
def spell(word):
for letter in list(word)[:-1]:
print(letter, ".", sep="", end="")
print(list(word)[-1])
Thanks again everyone for participating in the workshop.
We assure you that if you are able to solve the challenges of Day 3 you will have made a great learning achievement.
The last exercise requires you to build coding muscle, develop the basic craft, attitude, and patience. This is essential to address more domain-specific workflows, tasks and research questions using programming.
@aecryan, @jurra, thanks again for a very insightful workshop! Just to let you know, I submitted my first pull request with the Python challenge, all tests passed! :tada:
@cforgaci Congratulations Claudiu! You should feel very proud of yourself!!! 🥳
Hi @aecryan and @jurra, I had to wait for the weekend to find time to get it done, but it worked. 14 checks in the pull request here as well! Thank you, and congratulations @cforgaci for the quick solve.
Congrats @MertenNefs :) I hope you enjoyed it, I also enjoyed doing that challenge
def hello(name, country="Finland"): print(f"Hello {name},how are things in {country}?")
why there is a "f" after print( ?
@cforgaci, if "list" is taken out, the output is still correct, so what is the reason to have the commend of "list" in the cell?
the f is part of the F-string functionality in python, there is a link below the exercise
f stands for format in this context
On Mon, 22 Mar 2021, 10:21 communisker, @.***> wrote:
exercise 2 Create a function
def hello(name, country="Finland"): print(f"Hello {name},how are things in {country}?")
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/the-magnificents/04-02-2021-Carpentry-for-HGIS/issues/78#issuecomment-803904340, or unsubscribe https://github.com/notifications/unsubscribe-auth/AENYNULMJKBBF46B3VN655DTE4D2TANCNFSM4ZKD3OFQ .
EXERCISES DAY 3 — Python essentials for GIS learners
https://the-magnificents.github.io/04-02-2021-Carpentry-for-HGIS/03_Day_3_Git/exercise/00_README.html