首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在不显式转换输入的情况下将其作为整数读取?

如何在不显式转换输入的情况下将其作为整数读取?
EN

Stack Overflow用户
提问于 2018-08-09 06:26:27
回答 1查看 124关注 0票数 0

我写了一个程序,在从用户那里读取一些简单的数字和字符串后,将它们输出给用户。

为了打印和使用数学运算符,我必须多次转换数据类型,以便以我想要的方式打印它。

下面是我的代码:

代码语言:javascript
复制
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.")

下面是输出:

代码语言:javascript
复制
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.

底线是,我正在寻找一种更好的方法,而不必在程序结束之前多次转换类型。欢迎任何建议!

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51756512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档