I want to only allow a user to input a string followed by an integer and separated using a comma and if they do not comply I would like to let them know that they have inputted incorrectly and to let them try again. I am having trouble finding a way to do this, I can make the input require a comma but I do not know how to specify a string and int on each side.
name_age = []
user_input = []
name_age2 = []
ages = []
while user_input != "":
try:
user_input = input("Type 'Name,Age': ")
name_age.append(user_input)
print(name_age)
if "," not in user_input:
raise ValueError
except ValueError:
print("Incorrect try again")
else:
name_age.remove("")
for item in name_age:
name_age2.append(item.split(','))
for pair in name_age2:
for val in pair:
if val.isdigit():
ages.append(int(val))
print(name_age2)
print(ages)
print("Names & Ages: ", name_age2)
print("Total of ages: ", sum(ages))
print("Average of ages: ", sum(ages) / len(ages))
print("Total number of names: ", len(name_age2))
print("End")