Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Variables with numbers
#1
Hi Guys
I am an absolute beginner in python coding, I have this basic error and was wondering whats wrong
character_age = "50" this works fine
character_age = 50 I have been told you don't need " around numbers but it gives me an error
I'm using python 3 & pycharm the latest version
Quote
#2
show your code in python tags and exact error you get, in error tags
Quote
#3
character_name = "Mark"
character_age = 50
print("There was a man called " + character_name + ", ")
print("he was " + character_age + " years old. ")
character_name = "Pete"
print("He really liked the name " + character_name + ", ")
print("but didn't like being " + character_age + ". ")
error message
Error:
C:\Users\steve\PycharmProjects\variables\venv\Scripts\python.exe C:/Users/steve/PycharmProjects/variables/variables.py There was a man called Mark, Traceback (most recent call last): File "C:/Users/steve/PycharmProjects/variables/variables.py", line 9, in <module> print("he was " + character_age + " years old. ") TypeError: can only concatenate str (not "int") to str
buran wrote Nov-11-2018, 07:48 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Also, don't include line numbers when post code
Quote
#4
It's better not to use concatenation. If you do, you need to cast all variables to str.
Better use string formatting
print("he was {} years old.".format(character_age))
or in 3.6+ use f-strings
print(f"he was {character_age} years old.")
Quote
#5
Thank you,
As I said I am a complete novice at this coding.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 304 May-09-2019, 12:19 PM
Last Post: Pleiades

Forum Jump:


Users browsing this thread: 1 Guest(s)