#Mastery07 – Use of comments in Python #TC101 @PablO_CVi
Comments are tracks that the programer can add to the code, this won`t change anything in the program, this is just for helping the person that is going to use the code. For adding a comment in Python you just have to put a hashtag like this: #here goes your comment. This is how you can add comments to your code in python. Here you can see that this does not modify the program.
Here is the code i used for this program: https:/
I used this link to help me do this mastery: https:/
It is also useful to read chapter 3 of the book “Think Python, How to Think Like a Computer Scientist”,
You can see an example of this in my #WSQ08. Here’s the code showing the function that i created
def osuma (num1,num2): #first you have to define your function, in this case it’s osuma with num1 and num 2 as parameters
suma=num1+num2 #This is what the function does… this line must be idented so the function can work properly
return suma #the return statement returns with a value from a function… this line must be idented so the function can work properly
num1 = int(input(“Give me a number: “))
num2 = int(input (“Give me another number: “))
su=osuma(num1,num2) #Here i called osuma function i defined in order for the variable to take the function’s value
print (“The sum of your numbers is”, su)
]]>
I used this link to help me do this mastery: https:/
It is also useful to read chapter 3 of the book “Think Python, How to Think Like a Computer Scientist”,
You can see an example of this in my #WSQ08. Here’s the code showing where i called the function that i defined:
def osuma (num1,num2): #function osuma
suma=num1+num2
return suma
def oresta (num1,num2): #function oresta
resta=num1-num2
return resta
def omulti (num1,num2): #function omulti
multi=num1*num2
return multi
def odiv (num1,num2): #function odiv
div=num1/num2
return div
def orem(num1,num2): #function orem
rem= num1%num2
return rem
num1 = int(input(“Give me a number: “))
num2 = int(input (“Give me another number: “))
#Here i called every function i defined in order for the variable to take the functions value
su=osuma(num1,num2) #Here i called osuma function i defined in order for the variable to take the function’s value
res=oresta(num1,num2) #Here i called oresta function i defined in order for the variable to take the function’s value
mult=omulti(num1,num2) #Here i called omulti function i defined in order for the variable to take the function’s value
di=odiv(num1,num2) #Here i called odiv function i defined in order for the variable to take the function’s value
re=orem(num1,num2) #Here i called orem function i defined in order for the variable to take the function’s value
print (“The sum of your numbers is”, su)
print (“The difference of your numbers is”, res)
print (“The product of your numbers is”, mult)
print (“The division of your numbers is”, int(di))
print(“The remainder of the division of your numbers is”, int(re))
]]>
#333333; line-height: 1.5em !important;”>I learned how to use the while function in python thanks to this WSQ. Its easier to do a “while” than a lot of “ifs”, thats why i made my code with a single while function.
#333333; line-height: 1.5em !important;”>I used this links that showed me how to get a random number and how to use the while function in python.
#333333; line-height: 1.5em !important;”>#4c93cb; text-decoration: none;” href=”http://stackoverflow.com/questions/5555712/generate-a-random-number”>http:/
#333333; line-height: 1.5em !important;”>#4c93cb; text-decoration: none;” href=”http://www.tutorialspoint.com/python/python_loops.htm”>http:/
#333333; line-height: 1.5em !important;”>Here’s my code:
#333333; line-height: 1.5em !important;”>#8cbf99; text-decoration: none; font-family: ‘Helvetica Neue’, Helvetica, Arial, sans-serif; padding: 3px; font-size: 1em; background-repeat: repeat-x;” href=”http://gilrg18.withknown.com/tag/Gilberto” rel=”tag”>#Gilberto Rogel García a01630171
#333333; line-height: 1.5em !important;”>import random
#333333; line-height: 1.5em !important;”>value= random.randint(1,100)
#333333; line-height: 1.5em !important;”>tries=0
#333333; line-height: 1.5em !important;”>num= int(input(“Guess a number from 1 to 100:”))
#333333; line-height: 1.5em !important;”>while num != value: ###the while funtion starts here, i put “!=” so when the number is different from value the while function will take place.
#333333; line-height: 1.5em !important;”>if num>value :
#333333; line-height: 1.5em !important;”>print (num, “is too high”)
#333333; line-height: 1.5em !important;”>num= int(input(“Try again:”))
#333333; line-height: 1.5em !important;”>tries=tries+1
#333333; line-height: 1.5em !important;”>elif num<value :
#333333; line-height: 1.5em !important;”>print (num, “is too low”)
#333333; line-height: 1.5em !important;”>num= int(input(“Try again:”))
#333333; line-height: 1.5em !important;”>tries= tries+1
#333333; line-height: 1.5em !important;”>tries= tries +1 ###here is where the while function ends, when the number is equal to value.
#333333; line-height: 1.5em !important;”>print(“Congratulations!, The right number is”,value)
#333333; line-height: 1.5em !important;”>print(“It took you” ,tries, “tries to win”)
#333333; line-height: 1.5em !important;”>#8cbf99; text-decoration: none; font-family: ‘Helvetica Neue’, Helvetica, Arial, sans-serif; padding: 3px; font-size: 1em; background-repeat: repeat-x;” href=”http://gilrg18.withknown.com/tag/TC1014″ rel=”tag”>#TC1014 #8cbf99; text-decoration: none; font-family: ‘Helvetica Neue’, Helvetica, Arial, sans-serif; padding: 3px; font-size: 1em; background-repeat: repeat-x;” href=”http://gilrg18.withknown.com/tag/WSQ06″ rel=”tag”>#WSQ06
]]>#333333; line-height: 1.5em !important;”>I learned how to use the while function in python thanks to this WSQ. Its easier to do a “while” than a lot of “ifs”, thats why i made my code with a single while function.
#333333; line-height: 1.5em !important;”>I used this links that showed me how to get a random number and how to use the while function in python.
#333333; line-height: 1.5em !important;”>#4c93cb; text-decoration: none;” href=”http://stackoverflow.com/questions/5555712/generate-a-random-number”>http:/
#333333; line-height: 1.5em !important;”>#4c93cb; text-decoration: none;” href=”http://www.tutorialspoint.com/python/python_loops.htm”>http:/
#333333; line-height: 1.5em !important;”>Here’s my code:
#333333; line-height: 1.5em !important;”>#8cbf99; text-decoration: none; font-family: ‘Helvetica Neue’, Helvetica, Arial, sans-serif; padding: 3px; font-size: 1em; background-repeat: repeat-x;” href=”http://gilrg18.withknown.com/tag/Gilberto” rel=”tag”>#Gilberto Rogel García a01630171
#333333; line-height: 1.5em !important;”>import random
#333333; line-height: 1.5em !important;”>value= random.randint(1,100)
#333333; line-height: 1.5em !important;”>tries=0
#333333; line-height: 1.5em !important;”>num= int(input(“Guess a number from 1 to 100:”))
#333333; line-height: 1.5em !important;”>while num != value: ###the while funtion starts here, i put “!=” so when the number is different from value the while function will take place.
#333333; line-height: 1.5em !important;”>if num>value :
#333333; line-height: 1.5em !important;”>print (num, “is too high”)
#333333; line-height: 1.5em !important;”>num= int(input(“Try again:”))
#333333; line-height: 1.5em !important;”>tries=tries+1
#333333; line-height: 1.5em !important;”>elif num<value :
#333333; line-height: 1.5em !important;”>print (num, “is too low”)
#333333; line-height: 1.5em !important;”>num= int(input(“Try again:”))
#333333; line-height: 1.5em !important;”>tries= tries+1
#333333; line-height: 1.5em !important;”>tries= tries +1 ###here is where the while function ends, when the number is equal to value.
#333333; line-height: 1.5em !important;”>print(“Congratulations!, The right number is”,value)
#333333; line-height: 1.5em !important;”>print(“It took you” ,tries, “tries to win”)
#333333; line-height: 1.5em !important;”>#8cbf99; text-decoration: none; font-family: ‘Helvetica Neue’, Helvetica, Arial, sans-serif; padding: 3px; font-size: 1em; background-repeat: repeat-x;” href=”http://gilrg18.withknown.com/tag/TC1014″ rel=”tag”>#TC1014 #8cbf99; text-decoration: none; font-family: ‘Helvetica Neue’, Helvetica, Arial, sans-serif; padding: 3px; font-size: 1em; background-repeat: repeat-x;” href=”http://gilrg18.withknown.com/tag/WSQ06″ rel=”tag”>#WSQ06
]]>