Exercise 1: Loops

 1. Create a program that asks the user for 8 names of people and store them in a list. When all the names have been given, pick a random one and print it.

I think this is going to be a very nice challenge. Good luck,  and when you finish, come back here to see the solution. 

Okay let's delve into the solution of this problem.

Code:

import random

people = []

for x in range(0,8):
    person = input("Type the name of the person: ")
    people.append(person)

index = random.randint(0,7)

random_person = people[index]

print("Picking a random person: ", random_person)

#Let's see if this is working

Output:

>>> 
 RESTART: C:/Users/ameny/OneDrive/Desktop/Python.course/Exercise 1 on loops.py 
Type the name of the person: John
Type the name of the person: Paul 
Type the name of the person: Kofi
Type the name of the person: William
Type the name of the person: Kwame
Type the name of the person: KING
Type the name of the person: Hank
Type the name of the person: Walt
Picking a random person:  KING
>>> 

And it worked perfectly. You have the solution for excercise number 1. Let me know if you have further questions.



No comments:

Post a Comment

Python Programming Tutorial 15

 Error Handling Anytime we work with user input, we should make sure not only the data is valid and within the range we want, like what we d...