Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,149,823 members, 7,806,313 topics. Date: Tuesday, 23 April 2024 at 02:35 PM

Python 3: Exercises And Solutions. - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Python 3: Exercises And Solutions. (8851 Views)

Salesforce Rest API Example Using Python 3 / Consume Kibana Rest API Using Python 3 / How To Write A Guessing Game Program In Python 3 (2) (3) (4)

(1) (2) (3) (4) (5) (6) (Reply) (Go Down)

Python 3: Exercises And Solutions. by jayphe(m): 3:15pm On Apr 22, 2020
I welcome every Python Lords to this thread. Hmm....I don't seem to know you guys by name buh you can tell me to mention you in my next post.
Alright guys let's get this straight. This is not a tutorial thread...buh it can be of help sha. This is a place where we can ask ourselves 'Python' questions and share ideas.
Please welcome me home!

1 Like

Re: Python 3: Exercises And Solutions. by jayphe(m): 3:18pm On Apr 22, 2020


Exercise 1


Write a python program that obtains the domain name (thapar.edu) from the URL

http://www.thapar.edu/index.php/about-us/mission

3 Likes

Re: Python 3: Exercises And Solutions. by Collinsanele: 10:51am On Apr 23, 2020
website = "www...."
domain = website.split("http://www.")[1].split('/')[0]
print(domain)

1 Like

Re: Python 3: Exercises And Solutions. by jayphe(m): 3:12pm On Apr 23, 2020
Collinsanele:
website = "www...." domain = website.split("http://www.")[1].split('/')[0] print(domain)
File "<string>", line 2, in <module> IndexError: list index out of range

1 Like

Re: Python 3: Exercises And Solutions. by jayphe(m): 3:15pm On Apr 23, 2020
Collinsanele:
website = "www...." domain = website.split("http://www.")[1].split('/')[0] print(domain)
You are wrong...
Re: Python 3: Exercises And Solutions. by Collinsanele: 4:23pm On Apr 23, 2020
site ="https://www......"

domain = site.split('//')[1].split("www.")[-1]

print(domain)
Re: Python 3: Exercises And Solutions. by jayphe(m): 5:21pm On Apr 23, 2020
Collinsanele:
site ="https://www......"
domain = site.split('//')[1].split("www.")[-1]
print(domain)
you're making a mistake....
Re: Python 3: Exercises And Solutions. by jayphe(m): 5:22pm On Apr 23, 2020

This is my solution.....

#Obtain the domain (thapar.edu) name from the URL
#http://www.thapar.edu/index.php/about-us/mission

url = "http://www.thapar.edu/index.php/about-us/mission"
print ("Getting 'thapar.edu' from ",url)

list_url = url.rsplit("/",3)
print(list_url)
a = list_url[0]
str1 = str(a)
print("\n",a)
result = str1.split(".",1)
print("Finally, we now have....\n",result[1])
Re: Python 3: Exercises And Solutions. by jayphe(m): 5:30pm On Apr 23, 2020

This is another exercise.
Exercise 2

Write a Python program that accepts a string/sentence from the user. The program then changes all spaces to underscores. Finally, it prints out the final result.

Good luck guys!

1 Like

Re: Python 3: Exercises And Solutions. by Taofeekdboy(m): 8:49pm On Apr 23, 2020
jayphe:

This is another exercise.
Exercise 2

Write a Python program that accepts a string/sentence from the user. The program then changes all spaces to underscores. Finally, it prints out the final result.

Good luck guys!

user_input = str(input('write your full name))
user_input.replace(' ', ' _')
print(user_input)
Re: Python 3: Exercises And Solutions. by Collinsanele: 8:59pm On Apr 23, 2020
jayphe:

you're making a mistake....

What is the mistake
Re: Python 3: Exercises And Solutions. by kensmoney(m): 9:59pm On Apr 23, 2020
ken = "kensmoney is good"

def replaceWord(k):
sentence = str(k)
print(sentence.replace(" " , "_" )


replaceWord(ken)

#kensmoney_is_good
Re: Python 3: Exercises And Solutions. by jayphe(m): 7:53am On Apr 24, 2020
Taofeekdboy:

user_input = str(input('write your full name)) user_input.replace(' ', '_') print(user_input)
There was a space and a hypen in the 2nd argument of replace() Apart from that, you got it right! Bravo Toafeekdboy!
Re: Python 3: Exercises And Solutions. by jayphe(m): 7:56am On Apr 24, 2020
kensmoney:
ken = "kensmoney is good"
def replaceWord(k): sentence = str(k) print(sentence.replace(" " , "_" )

replaceWord(ken)
#kensmoney_is_good
kensmoney, I love your 'idea of thinking'. You got it right sha. Good job bro!
Re: Python 3: Exercises And Solutions. by jayphe(m): 7:58am On Apr 24, 2020
Collinsanele:

What is the mistake
Collinsanele, there was an IndexError stating 'list index out of range'
Re: Python 3: Exercises And Solutions. by jayphe(m): 8:05am On Apr 24, 2020

Collinsanele, Taofeekdboy, kensmoney another one is here!

Exercise 3

Write a Python program that accepts day, month and year from the user and prints the result in this form:

Today is 23/04/2020
For example,
Input:
day = 21
mnth = 07
year = 2010

Output:
Today is 21/07/2010

Good luck guys!

1 Like

Re: Python 3: Exercises And Solutions. by kensmoney(m): 11:46am On Apr 24, 2020
import datetime

date_and_time = datetime.datetime.now()
today_date =(str(date_and_time))
today_date_slice = ( today_date[:10] )
formated_date = today_date_slice.replace( "-", "/"
)
year = ( formated_date[:4] )
month_and_date = ( formated_date[5:] )

mydate= month_and_date + "/" + year
print(mydate)

#04/24/2020
Re: Python 3: Exercises And Solutions. by Zabiboy: 1:36pm On Apr 24, 2020
Op, these r short and not too tough projects...
Lets do the lengthy and real life application stuffs...
I can giv 3 diff. Projects if u guys are ok with d idea...

Note: these r projects i'v worked on/ working on

1 Like 1 Share

Re: Python 3: Exercises And Solutions. by jayphe(m): 6:54pm On Apr 24, 2020
Zabiboy:
Op, these r short and not too tough projects...
Lets do the lengthy and real life application stuffs...
I can giv 3 diff. Projects if u guys are ok with d idea...

Note: these r projects i'v worked on/ working on
Bro, I never said these were projects! They are - just as you said - simple exercises to keep 'things' busy. There is no bad if you also contribute to this. And I really would love to see one of your projects!
Re: Python 3: Exercises And Solutions. by Taofeekdboy(m): 7:16pm On Apr 24, 2020
jayphe:

Collinsanele, Taofeekdboy, kensmoney another one is here!

Exercise 3

Write a Python program that accepts day, month and year from the user and prints the result in this form:

Today is 23/04/2020
For example,
Input:
day = 21
mnth = 07
year = 2010

Output:
Today is 21/07/2010

Good luck guys!
I saw this late, I have been busy with my project and it took a lot of my time.
I will leave it here for who is interested to check..
It is a social media clone I made.
http://mysocialapp.pythonanywhere.com

Looking forward to your comments

2 Likes

Re: Python 3: Exercises And Solutions. by Collinsanele: 10:17pm On Apr 24, 2020
Taofeekdboy:

I saw this late, I have been busy with my project and it took a lot of my time.
I will leave it here for who is interested to check..
It is a social media clone I made.
http://mysocialapp.pythonanywhere.com

Looking forward to your comments

You used Django right?
It's really nice
Re: Python 3: Exercises And Solutions. by Collinsanele: 10:28pm On Apr 24, 2020
kensmoney:
import datetime

date_and_time = datetime.datetime.now()
today_date =(str(date_and_time))
today_date_slice = ( today_date[:10] )
formated_date = today_date_slice.replace( "-", "/"
)
year = ( formated_date[:4] )
month_and_date = ( formated_date[5:] )

mydate= month_and_date + "/" + year
print(mydate)

#04/24/2020

You don't need to slice.
year = datetime.datetime.now().year
month = datetime.datetime.now().month
day = datetime.datetime.now().day

1 Like

Re: Python 3: Exercises And Solutions. by Taofeekdboy(m): 10:42pm On Apr 24, 2020
Collinsanele:


You used Django right?
It's really nice
Yes, I used django with Jquery, moving it to react soon cos I wanna add more functionalities to it.
Thanks bro.

2 Likes

Re: Python 3: Exercises And Solutions. by Daddyshome: 12:08am On Apr 25, 2020
jayphe:

Collinsanele, Taofeekdboy, kensmoney another one is here!

Exercise 3

Write a Python program that accepts day, month and year from the user and prints the result in this form:

Today is 23/04/2020
For example,
Input:
day = 21
mnth = 07
year = 2010

Output:
Today is 21/07/2010

Good luck guys!
day = int(input())
month = int(input())
year = int(input())
print("Today is {:02}/{:02}/{}".format(day, month, year))
Re: Python 3: Exercises And Solutions. by kensmoney(m): 1:56am On Apr 25, 2020
[quote author=Collinsanele post=88819388]

You don't need to slice.
year = datetime.datetime.now().year
month = datetime.datetime.now().month
day = datetime.datetime.now().day

Yea but just want to do it in a diff way bro, no matter the approach,

1 Like

Re: Python 3: Exercises And Solutions. by Runningwater(m): 5:52am On Apr 25, 2020
I'm confused, the exercise said ACCEPT input from user. Isn't it like this?

def dateFun( currentDate, currentMonth, currentYear):
currentDate= int(input("Enter today's date: \n" )
currentMonth=int(input ("Enter today's month like 1 for January, 2 for February and so on: \n " )
currentYear=int(input("Enter current year: \n" )
message=currentDate,"/",currentMonth,"/",currentYear
return message

1 Like

Re: Python 3: Exercises And Solutions. by jayphe(m): 8:41am On Apr 25, 2020
Taofeekdboy:

I saw this late, I have been busy with my project and it took a lot of my time.
I will leave it here for who is interested to check..
It is a social media clone I made.
http://mysocialapp.pythonanywhere.com

Looking forward to your comments
A nice start bro....still need some fixing sha! Kudos!
Re: Python 3: Exercises And Solutions. by jayphe(m): 8:49am On Apr 25, 2020
kensmoney:
import datetime

date_and_time = datetime.datetime.now()
today_date =(str(date_and_time))
today_date_slice = ( today_date[:10] )
formated_date = today_date_slice.replace( "-", "/"
)
year = ( formated_date[:4] )
month_and_date = ( formated_date[5:] )

mydate= month_and_date + "/" + year
print(mydate)

#04/24/2020

Nice one bro...buh the question mentioned 'ACCEPT from user' and I can't find input().
Also just has Collinsanele said, you don't need to slice at all. However, you did great. Well done kensmoney
Re: Python 3: Exercises And Solutions. by jayphe(m): 8:51am On Apr 25, 2020
Runningwater:
I'm confused, the exercise said ACCEPT input from user. Isn't it like this?

def dateFun( currentDate, currentMonth, currentYear):
currentDate= int(input("Enter today's date: \n" )
currentMonth=int(input ("Enter today's month like 1 for January, 2 for February and so on: \n " )
currentYear=int(input("Enter current year: \n" )
message=currentDate,"/",currentMonth,"/",currentYear
return message

That's a wonderful approach Runningwater Kudos!
Buh u just defined the function. Anyway, I understand. That's gud!

1 Like

Re: Python 3: Exercises And Solutions. by Taofeekdboy(m): 8:54am On Apr 25, 2020
jayphe:

A nice start bro....still need some fixing sha! Kudos!
Thanks bro.
Obviously, it does!!!. Just worked on it with the aim of not using React, I worked with Django and JQuery because I wanted to master JQuery for small projects in which React might be an overkill. If time permits, I'm planning on moving it to a mobile app. Maybe it will be better there.

2 Likes

Re: Python 3: Exercises And Solutions. by jayphe(m): 9:13am On Apr 25, 2020

Collinsanele Taofeekdboy kensmoney Daddyshome Zabiboy

hmm....who else ooo? Let's get to business. Pls, let's try to read the question in details first. If the question asks for the users' input, then we should include an input statement. Zabiboy we need to talk ooo. This one wey I no see you....


Exercise 4
We are doing some 'loop and looping stuffs' today.


Write a Python program that accepts the name of a student, matric number, course code and the score he/she got in the course. E.g
Name: John Alfred
Matric. No.: MUC/CS/014/0882
Course Code: CMP 212
Score: 100

The program then print the result as:
John Alfred, MUC/CS/014/0882, CMP 212, Grade A


Using this grading table.
100-95 A
94-85 B
84-70 C
69-60 D
59-50 E
49-0 F

Corrections, advices, suggestions and opinions are highly welcome!

3 Likes

Re: Python 3: Exercises And Solutions. by jayphe(m): 9:14am On Apr 25, 2020
Taofeekdboy:

Thanks bro.
Obviously, it does!!!. Just worked on it with the aim of not using React, I worked with Django and JQuery because I wanted to master JQuery for small projects in which React might be an overkill. If time permits, I'm planning on moving it to a mobile app. Maybe it will be better there.
Wish you the best bro! Always the best!

(1) (2) (3) (4) (5) (6) (Reply)

Kehinde Adeyemi Among 12 Best Global IT Developers / Mobile Phone Apps For Nigeria / Why Is Programming Difficult?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 39
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.