How do you make python calculate numbers with variables equal to numbers?

How do you make python calculate numbers with variables equal to numbers?
0
#1

How do you make python calculate numbers with variables equal to numbers?

0 Likes

#2

do you mean like this?

a = 100
b = 50
c = a / b

print(a / b)
print(c)
if a / b == c:
    print("they match!")
0 Likes

#3
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
0 Likes

#4

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?

0 Likes

#5

I watch YouTube tutorials.

0 Likes

#6

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 :stuck_out_tongue:

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 :slight_smile:

0 Likes

#7

Why can’t it be " was in the " + int(age) - 10 + "'s."?

0 Likes

#8

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.

0 Likes

split this topic #9

2 posts were split to a new topic: Split a list into even and odd numbers

0 Likes