Python Practice

in #programming6 years ago (edited)

Hey it's 2019 for a week now!


My New Year Resolutions are to restart meditating every morning, learn and make stuff (nothing new really).

A friend wrote that his resolution is to be sweeter in 2019. I had immediately thought that I should stop eating so much chocolate chip ice-cream... That reminds me there is still some left... :3

This diary has started to collect dust and it's time to restart on it!

The holiday season seemed to last a fun forever, with Andikki, V and K showing up from Finland (what?) and Frankfurt. It was one of the best holidays I can remember :)

While I was having fun and slacking off, Microsoft changed their courses on edX and now you lose access after one month! Plus they don't let you do the tests anymore unless you pay...way to go Microsoft! I applaud you!!! It's surprising they were free and open for so long. So I'll audit it while it's still open and concentrate on other sources :D there is plenty of info available elsewhere.

1-4.3 Intro Python Practice


Conditionals


You will be able to

  • code more than two choices using elif
  • gather numeric input using type casting
  • perform subtraction, multiplication and division operations in code
So yeah I've reached the practice module for the Conditionals! Yay, it's head-scratching time :D

After I solve some of the practice exercises I try to find out if the course stuff has offered an example of how to solve it. Sometimes there is no solution, and other students offer their solutions instead. Those students usually are familiar at least with one more language and use stuff not taught in the course yet xD It always makes me wonder why the course gives out problems which clearly require more than was taught. I can imagine people giving up before finishing the course because of that.



This is one of such exercises. The legendary pet conversation D:

# [ ] complete pet conversation
pet = 0
about_pet = input("Tell me something about your pet: ")
if "dog" in about_pet:
    pet += 1
    print("Ah, a dog!")
if "cat" in about_pet:
    pet += 1
    print("Nice, I have a cat too")
if pet > 1:
    print("Cool, you have many pets!")
print("Thanks! Was nice meeting you! Later!")
This is the solution I came up with, although as far as I remember the course didn't mention the += part (I know it from the Codeacademy course)

+= is an assignment operator that is used to add the following numeric value to the variable:

x = 2

x += 5 is a way to express x = x + 5
in case a string is following the += then it concatenates the strings represented in the variable.

shopping_list = toothpaste

shopping_list += toothbrush
that would make

shopping_list = toothpaste, toothbrush
Many students were asking how to check if 1 or more pets are mentioned. While the staff course didn't provide any solutions, I found an interesting one, which makes me curious if it's overcomplicating things.
# another solution
num_pets = 0
about_pet = input("Type something about your pet ")

if 'dog' in about_pet:
print("Ah, what a cute dog ")
num_pets +=1
if 'cat' in about_pet:
num_pets +=1
elif 'bird' in about_pet:
num_pets +=1
if 'cat' in about_pet:
print("Ah, I hate cats ")
num_pets +=1
if 'dog' in about_pet:
num_pets +=1
elif 'bird' in about_pet:
num_pets +=1
if 'bird' in about_pet:
print("Ah, a bird ")
num_pets +=1
if 'cat' in about_pet:
num_pets +=1
elif 'dog' in about_pet:
num_pets +=1
if num_pets > 1:
print("You have a lot of pets! ")
print("Thank you for your story")

I've tested this solution too but it doesn't seem to do anything, so why add extra conditions inside the main one if they don't do anything?

Any explanation?

~ mitsu


Posted from my blog with SteemPress : https://anmitsu.club/2019/01/08/python-practice/

Sort:  

Congratulations @anmitsu! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You published more than 10 posts. Your next target is to reach 20 posts.
You received more than 250 upvotes. Your next target is to reach 500 upvotes.

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Do not miss the last post from @steemitboard:

SteemWhales has officially moved to SteemitBoard Ranking
SteemitBoard - Witness Update

Support SteemitBoard's project! Vote for its witness and get one more award!