I am a beginner programmer (or so I think) at this programming stuff so my knowledge of terminology usage is rather subpar. So with that said. I'm doin this in Python, (yea yea, it should be easy cause it's Python but it's not easy for me). I want to know, is there an easier way to return a string that is legitimately a string and not represented as an integer when using the input function? Cause when I ask the user to type their name and they type a number instead (for whatever reason...must be lupus), the numeric input is still accepted as an string value.
I want to accept string (actual words) and not numeric(integer or float), so when a numeric value is entered, it'll print a message asking to try again, then return the user to the input function to enter the correct value (simple stuff, right???). I had also added entry for age, height(haven't done anything for these two yet) and gender (gender options are just male and female).
As for the gender portion, I created a function to enter either male or female and associated both gender by returning the value of their respective gender sign. Excuse my grammar. Anyhoo, penny for your thoughts? P.S. May be a lot of unnecessary clutter in my code so excuse the hot mess. I am welcome to criticism.
the code goes as follows:
i = 'Name'
j = 'Age'
k = 'Height'
l = 'Sex'
def anno_name(input_str: str ) -> str:
if input_str.isalpha():
return
#print(input_str)
else:
print("Entry invalid. Name can neither be numeric nor left empty. Please try again.\n")
return anno_name(input("Enter your name: "))
aname = input(f'{i}\nEnter your name: \n')
anno_name(aname)
print(input(f'\n{j}\nEnter age: \n'))
print(input(f'\n{k}\nEnter height: \n'))
def sym_g():
gen = input(f'\n{l}\nEnter sex: \n')
if gen == 'female':
return 'female \f'
elif gen == 'male':
return ('male \v')
else:
if gen != 'female' or 'male':
print("Entry invalid. Please try again.\n")
return sym_g()#('Invalid value. Please try again...')
sym_gen = sym_g()
print(sym_gen)
CodePudding user response:
With what you are trying to do, you will have to evaluate every input which I it's a good thing to create functions for each and every input and return the input if it pass the evaluation. That will give you the flexibility to handle every error individually. Example:
def anno_name(input_str: str ) -> str:
if input_str.replace(" ", "").isalpha():
return " ".join(input_str.split(" ")).title()
else:
print("Entry invalid. Name can neither be numeric nor left empty. Please try again.\n")
return anno_name(input("Enter your name: "))
def get_age(input_age: int ) -> int:
try:
input_age = int(input_age)
except ValueError:
pass
if type(input_age) is int:
return input_age
else:
print("Entry invalid. Accepts only integers. Please try again.\n")
return get_age(input("Enter Age: "))
def get_height(input_height: float ) -> float:
try:
input_height = float(input_height)
except ValueError:
pass
if type(input_height) is float:
return input_height
else:
print("Entry invalid. Accepts either integers or float. Please try again.\n")
return get_height(input("Enter height: "))
def get_sex(input_sex: str ) -> str:
input_sex = input_sex.lower()
if input_sex == "male" or input_sex == "female":
return input_sex.title()
else:
print("Entry invalid. Enter (male) or (female).\n")
return get_sex(input("Enter sex: "))
def get_personal_details() -> None:
name = anno_name(input("Name\nEnter your name: "))
print(name, "\n")
age = get_age(input("\vAge\nEnter Age: "))
print(age, "\n")
height = get_height(input("\vHeight\nEnter Height: "))
print(height, "\n")
sex = get_sex(input("\vSex\nEnter Sex: "))
print(sex, "\n")
print("\vPersonal Details:")
print(f"Name: {name}\nAge: {age}\nHeight: {height}\nSex: {sex}")
get_personal_details()
output:
Name
Enter your name: james kookoo
James Kookoo
Age
Enter Age: 67
67
Height
Enter Height: 6732
6732.0
Sex
Enter Sex: male
Male
Personal Details:
Name: James Kookoo
Age: 67
Height: 6732.0
Sex: Male
CodePudding user response:
Ok, that looks like about it. This does work. But I was wondering is there a way to return the gender sign along side the gender itself where they are concatenated. In my original code I had associated the gender with the sign. Your code is clean and very efficient. I just having a some challenges and want to seek some pointers. So what I did was changed a few things in the gender function.
def get_sex(input_sex):
input_sex = input_sex.lower()
if input_sex == "male":
return input_sex.title() (' \v')
elif input_sex == "female":
return input_sex.title() (' \f')
else:
print("Entry invalid. Enter (male) or (female).\n")
return get_sex(input("Enter sex: "))'
That way I keep the the gender concatenated with the respective gender sign (escape sequences). My change may be not as clean as what was corrected to be. I am open to anymore ideas to make it least wordy. Perhaps using a lambda. Which I do not have the slightest of clues to squeeze all of this block into one or two or three lines of code.
CodePudding user response:
What kind of signs are you referring to? Some signs might not show when you use them, depending on your python version. You can use something like ♂
and ♀
and you can also use lambda
if you wish to. The choice is yours.
I will suggest you concatenate using f-string
. Something like:
def get_sex(input_sex):
input_sex = input_sex.lower()
user_gender = lambda x : f"{input_sex.title()} ♂" if (x == "male") else f"{input_sex.title()} ♀"
if input_sex == "male" or input_sex == "female":
return user_gender(input_sex)
else:
print("Entry invalid. Enter (male) or (female).\n")
return get_sex(input("Enter sex: "))
output:
Sex
Enter Sex: female
Female ♀