Python Forum
Need some help with simple coding! - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Need some help with simple coding! (/thread-36087.html)



Need some help with simple coding! - Jarno12 - Jan-15-2022

I needed to create a code to get users age and name and then have the output (input examples John, 33) be Hello John! You were born 1989. current output is Hello John ! You were born in 1989 .

Here is the code I made so far

# Create an integer variable of the current year.
current_year = int(2022)
# Prompt the user for their name and save to string variable.
my_Name = input("What is your name?\n")
# Prompt the user for their age and save to int variable.
my_age = int(input("How old are you?\n"))


# Format users info to show "Hello ______!, you were born in______." Subtracted current_year from my_age to get year born.
print( 'Hello' , my_Name, '!' , 'You were born in ' , current_year - my_age, '.')
Any clue how to remove just those two spaces?


RE: Need some help with simple coding! - Yoriz - Jan-15-2022

Use a f string
print(f"Hello {my_Name}! You were born in {current_year - my_age}.")
f-string, string format, and string expressions


RE: Need some help with simple coding! - Jarno12 - Jan-15-2022

(Jan-15-2022, 05:17 PM)Yoriz Wrote: Use a f string
print(f"Hello {my_Name}! You were born in {current_year - my_age}.")
f-string, string format, and string expressions

This worked perfectly thank you so much!


RE: Need some help with simple coding! - Gribouillis - Jan-15-2022

I can't believe 33 years old people were born in 1989.. Cry

By the way, the program will not give correct results next year. Python can give you the current year
import datetime
current_year = datetime.date.today().year
With this the program will work for many years.


RE: Need some help with simple coding! - Jarno12 - Jan-15-2022

(Jan-15-2022, 05:35 PM)Gribouillis Wrote: I can't believe 33 years old people were born in 1989.. Cry

By the way, the program will not give correct results next year. Python can give you the current year
import datetime
current_year = datetime.date.today().year
With this the program will work for many years.

Haha I guess time goes by faster when we're having fun! I updated the code with that in there I appreciate the help!!!


RE: Need some help with simple coding! - ndc85430 - Jan-15-2022

Note that in line 2, the call to int is unnecessary - the value 2022 is already an integer (what other type could it be really?).