-1

I am writing a little .py program which gives answer depending on user's answer. Code looks like this:

name = input("Please enter your name: ")
age = input("Please tell me your age: ")
date_of_birth = 2020 - int(age)
date_of_birth = str(date_of_birth)
print("Hi! " + name + " you was born in " + date_of_birth)
answer = input("If this is true, write: \"yes\" if now=t, write: \"no\" ")

if  answer.upper() == ("YES"):
    print("In this year Canada introduced new coin")
elif answer.upper() == ("NO"):
    print("If the year of your birth is not 1989, then I need to update my knowledge and get back to you")

My question is related to below lines:

date_of_birth = 2020 - int(age)
date_of_birth = str(date_of_birth)
print("Hi! " + name + " you was born in " + date_of_birth)

As you can see, I had to change variable date_of_birth to string. Is it possible to shorten the code? I would like to avoid changing this variable type into a string. How I could write the line print(...) to shorten the code?

I am not looking for ready answer. I am kindly asking for showing me way of thinking to solve this problem. Thank you.

Gokul nath
  • 494
  • 2
  • 8
  • 17
piotrektnw
  • 120
  • 7
  • Get rid of the superfluous parentheses in the `if` condition. So `if answer.upper() == ("YES"):` should be `if answer.upper() == "YES":`. – Matthias Mar 20 '20 at 12:56
  • 1
    Does this answer your question? [How do I put a variable inside a string?](https://stackoverflow.com/questions/2960772/how-do-i-put-a-variable-inside-a-string) – mkrieger1 Mar 20 '20 at 13:11
  • Yes! Thank you for your help – piotrektnw Mar 20 '20 at 20:38

6 Answers6

0

Use f-strings

print("Hi! " + name + " you was born in " + date_of_birth)

will become

print(f"Hi! {name} you was born in {date_of_birth}")

You don't need to convert date_of_birth to a string before you use it in an f-string.

f-strings are powerful. You could even do it like this.

print(f"Hi! {name} you was born in {2020 - int(age)}")

Another thing concerning your code.

answer = input("If this is true, write: \"yes\" if now=t, write: \"no\" ")

Those escapes make this line look ugly and complicated. Enclose the string with single quotes and then there's no need to escape the double-quotes.

answer = input('If this is true, write: "yes" if not, write: "no"')
Matthias
  • 12,873
  • 6
  • 42
  • 48
0

You can use str.format. The index is the number that is in the {} tag. If you leave blank the {} tag, it will format in order.

Code:

print( "Hi! {} you were born in {}".format( name, 2020 - int(age) ) )

#is same as

print( "Hi! {1} you were born in {0}".format( 2020 - int(age), name ) )
bapap
  • 514
  • 8
  • 25
0

You can use the .format function:

print("Hi!{0} you was born in {1}".format(name, date_of_birth))

The 1 and the 0 stand for the index of the argumend in the function

-1

Yes! There is a way to make the two first lines only one line. If you don't want me to say the direct answer I'll give you a hint: You don't have to define the variable again!

An example:

graduation_date = 2020 My_String = "You were born in " + str(graduation_date - 18)

martin
  • 887
  • 7
  • 23
-1

You can seperate each variable by commas. A comma will automatically add a space.

So, doing

print("Hi!" + name + " you was born in", date_of_birth)

will work without changing date_of_birth to a string.

-1

You can try this below :

print("Hi! " + name + " you were born in", (2020 - int(age)))
Abhishek Kulkarni
  • 1,747
  • 1
  • 6
  • 8