How do you make python calculate numbers with variables equal to numbers?
do you mean like this?
a = 100
b = 50
c = a / b
print(a / b)
print(c)
if a / b == c:
print("they match!")
print("Let's make a story!")
name = input("\nEnter a name for an old man.: ")
age = input("Enter " + name + "'s age.: ")
cat_name = input("Enter a name for " + name + "'s cat.: ")
print("\nOnce upon a time, there was an old man called " + name + ".")
print("He was " + age + " years old and he liked to go out for a walk.")
print("When " + name + " goes out for a walk, he often walks with his cat " + cat_name + ".")
print(name + " and " + cat_name + " were good old friends from when " + name + " was in the " + {age - 10} + "'s.")
``` I want to calculate age - 10
print(name + " and " + cat_name + " were good old friends from when " + name + " was in the ", int(age) - 10, "'s.")
We have to convert age
to an int
(number) because inputs are automatically saved as a string
What resource are you using to learn python?
I watch YouTube tutorials.
Nice! If you can find a good code-with-me video you can copy along with it can be a good way to learn.
Check this out
print(f'''
Once upon a time, there was an old man called {name}.
He was {age} years old and he liked to go out for a walk.
When {name} goes out for a walk, he often walks with his cat {cat_name}.
{name} and {cat_name} were good old friends from when {name} was in the {int(age) - 10}'s.
''')
using the f
before the string will format it so you can insert your variables in curly braces.
using the triple '''
on each end lets you hit enter in the string and it will save the formatting
Why can’t it be " was in the " + int(age) - 10 + "'s."
?
That is because of data types, which you can learn about here:
The answer to your question can also be found here:
http://davidbau.com/python/slides/slide6.html
Basically, python can “add” strings to other strings, and can add numbers to other numbers, but can not add numbers to strings.