[Tutor] Lists
Vincent Balmori
vincentbalmori at yahoo.com
Fri Jun 10 10:01:43 CEST 2011
I'm stuck on two problems from the Absolute Beginners book. The first is simple.
I am trying to print all the words in the list in random order without repeats,
but it always shows None for some reason.
#Program prints list of words in random order with no repeats
import random
#List of words
list = ["first", "second", "third", "fourth","fifth"]
#Randomize word order
order = random.shuffle(list)
#Print words in random order
print(order)
input("\n\nPress the Enter to exit")
The other question is: "Improve the program by adding a choice that lets the
user enter a name and get back a grandfather. You should still only use one list
of son-father pairs. Make sure to include several generations in list so a match
can be found." The only thing I have done is adding choice 5 with its basic elif
block and adding another name in each of the three sequences in the list, but I
am completely stuck otherwise.
#User can enter name of male and name of his father will show
#Allow user to add, replace, and delete son-father pairs.
#Also have the user enter a name to get back a grandfather
#Setting values
sondad = {"Vincent": "Ramon": "Lolo","Jesus": "God": "Unknown", "Simba":
"Mufasa": "Lion"}
choice = None
while choice != "0":
print(
"""
Who's Your Daddy?!
0 - Quit
1 - Look Up Pair
2 - Add a Pair
3 - Replace a Pair
4 - Delete a Pair
5 - Look up a Grandfather
"""
)
choice = input("Choice: ")
print()
# exit
if choice == "0":
print("Good-bye.")
# look up pair
elif choice == "1":
choice = input("Which pair do you want to look up?: ")
if choice in sondad:
dad = sondad[choice]
print("\n", choice,"is son to:", dad)
else:
print("\nSorry, I don't know", choice)
# add a father
elif choice == "2":
choice = input("Who is the son you want to add?: ")
if choice not in sondad:
dad = input("\nWho's the father?: ")
sondad[choice] = dad
print("\n", dad, "has been added.")
else:
print("\nThat pair already exists! Try replacing them.")
# replace a father
elif choice == "3":
choice = input("Which son do you want to replace a father?: ")
if choice in sondad:
dad = input("Who's the father?: ")
sondad[choice] = dad
print("\n", choice, "has been replaced.")
else:
print("\nThey don't exist! Try adding them.")
# delete a pair
elif choice == "4":
choice = input("Which pair do you want to delete?: ")
if choice in sondad:
del sondad[choice]
print("\nOkay, I deleted", choice)
else:
print("\nI can't do that!", choice, "don't exist.")
# look up grandfather:
elif choice == "5":
choice = input("Who's grandfather are you looking up?: ")
if choice in sondad:
dad = sondad[choice]
print("\n", choice,"is grandson to:", dad)
else:
print("\nSorry, I don't know", choice)
# some unknown choice
else:
print("\nSorry, but", choice, "isn't a valid choice.")
input("\n\nPress the enter key to exit.")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110610/a28a5084/attachment-0001.html>
More information about the Tutor
mailing list