Python Forum
Problem printing out first name, last name, age
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem printing out first name, last name, age
#1
I am creating an application that requests first name, last name, and age (based off of year born). I believe I have the variables all correct as they ran properly separately. When I try to print fullname and age, I'm running into issues. Currently the code is as:

firstname = input("What is your first name?")
lastname = input("What is your last name?")
fullname = firstname = " " + lastname
currentyear = 2020
birthyear = input("What year were you born? ")
ibirthyear = int(birthyear)
age = currentyear - ibirthyear
print("Welcome" + fullname, age)

When I run the application, it asks for my first name, last name, and year born. But the output eliminates the first name, prints the last name, and the correct age. What alterations are necessary for me to keep my first name in the solution? Is the final line incorrect, or did I mess something up in a previous variable?
Reply
#2
fullname = firstname = " " + lastname

is this possible in python?
Reply
#3
(Feb-07-2020, 03:34 PM)puredata Wrote: fullname = firstname = " " + lastname

is this possible in python?

in its own window, yes both of these run fine. It seems putting them in the same window is where I'm experiencing issues

firstName = input("What is your first name?")
lastName = input("What is your last name?")
fullName = firstName + " " + lastName
print("Welcome " + fullName)

currentYear = 2020
birthYear = input("What year were you born? ")
ibirthYear = int(birthYear)
age = currentYear - ibirthYear
print(age)

So it seems changing it to this works, but I'm still trying to have the final output say like "fn ln aged, age"
firstname = input("What is your first name?")
lastname = input("What is your last name?")
fullname = firstname + lastname
space = " "
fullname = firstname + space + lastname
currentyear = 2020
birthyear = input("What year were you born? ")
ibirthyear = int(birthyear)
age = currentyear - ibirthyear
print("Welcome " + fullname, + age)
Reply
#4
(Feb-07-2020, 03:37 PM)Asm0deus314 Wrote: So it seems changing it to this works, but I'm still trying to have the final output say like "fn ln aged, age"

I just checked your final code on IPython it looks like it's working, when I run
print("Welcome" + fullname, + age)
it gives me first name + last name and age with a welcome string.

I'd try to keep it shorter though. For example birthyear shouldn't need to be converted to an integer in a separate line.
birthyear = int(input("What year were you born? "))
should work. So you wouldn't need ibirthyear variable. Space variable also looks pretty unnecessary considering that you can hard code it, it will never change to something else from a space character.

Edit: this is what I was able to come up with;

from datetime import date

firstname = input("What is your first name?")
lastname = input("What is your last name?")
fullname = firstname + " " + lastname
currentyear = int(date.today().year)
birthyear = int(input("What year were you born? "))
age = currentyear - birthyear
print("Welcome " + fullname, + age)
As the next step I'd look into implementing exceptions. Currently this script allows users to input integers as their name (won't throw error) or string characters into age field (which is expecting integers and will throw error)

if firstname or lastname is not a string print ('you must input text')
if age is not an integer print ('enter numbers only')

type of stuff. I'm a newbie as well so better answers might be possible
Reply
#5
Better (more readable)
print(f"Welcome {firstname} {lastname} of a certain age {age}")
Reply
#6
I strongly recommend to follow PEP8 naming conventions (Function and variable names) i.e firstname -> first_name.

Just because it can be done:

>>> first_name, last_name, birth_year = (input(f'What is your {question}: ') for question in ('first name', 'last name', 'birth year'))
What is your first name: Guido
What is your last name: van Rossum
What is your birth year: 1956
>>> f'Welcome {first_name} {last_name} of age {2020 - int(birth_year)}'
'Welcome Guido van Rossum of age 64'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
Thanks guys, got it figured out. Pretty much teaching myself, and there are so many ways to go about things.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020