0% found this document useful (0 votes)
7 views

Python Challenge

A python challenge plus some comments to help

Uploaded by

robofun422
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Python Challenge

A python challenge plus some comments to help

Uploaded by

robofun422
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#A challenge program for Python

"""
In this program would be asking the user how many times they would like to
enter a name and age and not accept floats, strings, and negative integers.
After asking the user for the name and age(, the program will store it in two
different
lists and then sort it and display both of them. After that it will change the
second name to "Gomez" and add "George" to the list containing the names. After
that
It will take the seocnd age in the list containing the ages and divide it by the
first one and round it to the third decimal place. Then the program will create
a new list and combining the the list containing the ages to the list containing
the names. Then it will take the lowest value in the list containing the ages and
multiply it by two and then sorting the list containing the names and sort the
list containing the ages and then combine the list containing the names to the
list containing the ages.
"""

#Asks the user how many names will be entered


check = True
while check:
try:
times = int(input("How many names will you enter? "))
if times > 0:
print("")
check = False
else:
print("Must enter a positive integer for the number of names")
print("")
except ValueError:
print("Must enter a positive integer for the number of names")
print("")

#makes two blank lists to store the name and age.


name = []
age = []

#Asks the user for the names and ages to store in the blank lists and will not
allow them to enter a float, string, or negative number.
for i in range(times):
input_names = input("Please enter a name: ")
input_ages = input("Please enter " + input_names + "'s age: ")
try:
ages = int(input_ages)
if ages > 0:
age.append(ages)
name.append(input_names)
print("")
else:
print("Must enter positive integer for the age.")
print("")

except ValueError:
print("Must enter positive integer for the age.")
print("")
#Sort and display both lists.
name.sort()
age.sort()
print("Sorted names:")
print(name)
print("Sorted ages:")
print(age)

#Change the second name in the list to Gomez and display


print("")
print("Change second name to Gomez: ")
name.pop(1)
name.insert(1, "Gomez")
print(name)

#Add George to the list of names and display


print("")
name.append("George")
print("Add George to the list of names:")
print(name)
print("")

#Divide the second age in the list by the first age and round to 3 decimal places
and display the result
print("The second age in the list divided by the first one, rounded to 3 decimal
places:")
divison = age[1]/age[0]
divison = round(divison, 3)
print(divison)

#Create a new list by combining the Names and Ages lists and display
print("")
print ("Merged list of names and ages: ")
merged = name + age
print(merged)

#Replace the lowest number in the list with that number doubled and display the
newly sorted list
print("")
print("Replace lowest age with 2x that number and display the newly sorted list: ")
new = age[0] * 2
age[0] = new
age.sort()
name.sort()
other = age+name
print(other)

You might also like