Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Adding string after int

Code
age = 22
daysalive = (age * 365)
print("Hello, you have been alive for" , daysalive +" days""")



How would I format the print so it does not give errors?:



Traceback (most recent call last):
File "C:\Users\17275\Desktop\Python\daysAlive.py", line 3, in <module>
print("Hello, you have been alive for" , daysalive +" days""")
TypeError: unsupported operand type(s) for +: 'int' and 'str'


Thanks :)
age = 22
daysalive = (age 365)
print("Hello, you have been alive for " + str(daysalive)+ " days")

or

age = 22
daysalive = (age
365)
print("Hello, you have been alive for " , daysalive , " days")

just replace + with ,
(Aug 31, 2021 08:37 AM)the craavce Wrote:  age = 22
daysalive = (age 365)
print("Hello, you have been alive for " + str(daysalive)+ " days")

or

age = 22
daysalive = (age
365)
print("Hello, you have been alive for " , daysalive , " days")

just replace + with ,

Appreciate it :)
(Aug 31, 2021 08:37 AM)the craavce Wrote:  age = 22
daysalive = (age 365)
print("Hello, you have been alive for " + str(daysalive)+ " days")

or

age = 22
daysalive = (age
365)
print("Hello, you have been alive for " , daysalive , " days")

just replace + with ,

better would be


PHP Code

<?php 
age 
= 22
daysalive 
= (age * 365)
print(
f"Hello, you have been alive for {daysalive} days") 
¸„»°'´¸„»°'´Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est ¸„»°'´`'°«„¸`'°«„¸¸„»°'´
(Sep 6, 2021 01:47 AM)vorticalbox Wrote:  better would be


PHP Code

<?php 
age 
= 22
daysalive 
= (age * 365)
print(
f"Hello, you have been alive for {daysalive} days") 

Even better would be

Code
from datetime import date

birthday = date(1998, 12, 31)
delta = date.today() - birthday
print(f"Hello, you have been alive for {delta.days} days")
[Image: 3943c24fb6058003233768a9747351d2.png]
(Sep 7, 2021 12:08 AM)L E G I Ø N Wrote:  Even better would be

Code
from datetime import date

birthday = date(1998, 12, 31)
delta = date.today() - birthday
print(f"Hello, you have been alive for {delta.days} days")

This isn't just better its the only correct answer per pep 498.
As the saying goes ‘There’s more than one way to skin a cat’ 🙂
[Image: Web-Logo-copy.gif]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by MaskinApr 21, 2021 01:52 AM 5 321 Apr 21, 2021 01:52 AM
Last Post: Vector Dynamics
[Python] by madeha elsayedxxxxMay 17, 2020 10:01 PM 1 140 May 17, 2020 10:01 PM
Last Post: District
[C] by MordiciaNov 15, 2019 07:21 PM 3 210 Nov 15, 2019 07:21 PM
Last Post: Mordicia