Python Forum
Need some help with simple coding!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need some help with simple coding!
#1
Information 
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?
Yoriz write Jan-15-2022, 05:11 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Use a f string
print(f"Hello {my_Name}! You were born in {current_year - my_age}.")
f-string, string format, and string expressions
Jarno12 likes this post
Reply
#3
(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!
Reply
#4
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.
Jarno12 likes this post
Reply
#5
(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!!!
Reply
#6
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?).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple Freshman Python Coding Help tomatoe 3 2,277 Apr-30-2020, 10:23 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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