0

I wrote a program to output some simple numbers and strings to the user after reading them from the user.

In order to print and use math operators, I had to convert the datatypes multiple times in order to print it the way I want it.

Here's my code:

print("What is your name?")
name = input()

print("What is your age?")
age = input()

print(type(age))
print("Check for type")

print("We will now add your " + age + " years to 50.")
age = int(age)
print(type(age))
print("Check for type")

finalAge = 50 + age
finalAge = str(finalAge)
age = str(age)
print("In 50 years " + name + " will be " + finalAge + " years old.")

Here's the output:

What is your name?
Gavin
What is your age?
23
<class 'str'>
Check for beginning
We will now add your 23 years to 50.
<class 'int'>
Check for end
In 50 years Gavin will be 73 years old.

The bottom line is that I am looking for a better way to not have to convert types multiple times before the program finishes. Open to any suggestions!

ababuji
  • 1,683
  • 2
  • 14
  • 39
MasterG
  • 11
  • 5
  • 1
    Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – ababuji Aug 08 '18 at 22:29
  • Use string formatting instead of concatenation: `print("{}".format(50))` – Jonathon Reinhart Aug 08 '18 at 22:30
  • @JonathonReinhart Do you have any example code to how that would look? – MasterG Aug 08 '18 at 22:32
  • But why you gave `str` value to age and after that you gave an `int` value to it? Isn't it wrong? – Azhy Aug 08 '18 at 22:34
  • @Azhy It started as a string, then I changed it to an Integer to add it, and then changed it back to a String. Looks Like I could have formulated my approach a little better. – MasterG Aug 08 '18 at 22:36

2 Answers2

0

You cannot add int value to str value or something like that although the str value has a number in it, because their variable types are different but you can create functions to easier your projects or use fewer codes for same thing, for example you can use something like:-

def sum(a, b):
    return str(int(a)+int(b));

This is a function which returns the sum of two number values as string and you can create a lot of custom functions as how do you want, you can make your code easier and more enjoyful by using Functions.

Azhy
  • 704
  • 3
  • 16
-1

It would be better to not do the reassignment and only cast when you need to.

print("What is your age?")
age = input()

print("We will now add your " + age + " years to 50.")

finalAge = 50 + int(age)
print("In 50 years " + name + " will be " + str(finalAge) + " years old.")

Additionally, it is good practice to assign the data type based on what it fits. age would be best suited for an int:

print("What is your age?")
age = int(input())

print("We will now add your " + str(age) + " years to 50.")

finalAge = 50 + age
print("In 50 years " + name + " will be " + str(finalAge) + " years old.")
Easton Bornemeier
  • 1,918
  • 8
  • 22
  • Why the downvotes? He asked for a method to not have to convert types many times in his program, I believe this answers. There are obviously better but more advanced methods he could be using, but that is not what the question asked – Easton Bornemeier Aug 08 '18 at 22:33
  • Bornmeier, I agree with you. What other advanced methods would you have suggested? I'm eager to learn this! So, any topic that I can look into I would appreciate. – MasterG Aug 08 '18 at 22:39