我写了一个程序,在从用户那里读取一些简单的数字和字符串后,将它们输出给用户。
为了打印和使用数学运算符,我必须多次转换数据类型,以便以我想要的方式打印它。
下面是我的代码:
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.")
下面是输出:
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.
底线是,我正在寻找一种更好的方法,而不必在程序结束之前多次转换类型。欢迎任何建议!
https://stackoverflow.com/questions/51756512
复制相似问题