int to str on Python

时间:2016-09-07 19:33:03

标签: python

name = input("Please enter your name: ")
age = input("Please enter your age: ")
print ("Hello, " + name + ", you are " + age * 7 + " Years old in Dog years.").

这是我的代码,这就是我得到的。

Please enter your name: paris
Please enter your age: 13
Hello, paris, you are 13131313131313 Years old in Dog years.

我想要13 x 7而不是13 7次。有人可以帮帮我吗? THX

2 个答案:

答案 0 :(得分:2)

在python中你可以执行此操作str * 7,你可以看到它会重复7次。

您希望将年龄转换为int int(age)或将您的输入转换为仅接受整数age = int(input("enter age")),如评论中所述。

为了将来参考,您可以使用type(<variable>)

测试变量的类型

编辑...

name = input("Please enter your name: ")
age = int(input("Please enter your age: "))
print ("Hello, " + name + ", you are " , age * 7 , " Years old in Dog years.")

答案 1 :(得分:1)

将字符串与整数相乘会重复多次。在进行计算之前,您必须将age转换为数字:

print("Hello, {}, you are {} Years old in Dog years.".format(name, int(age) * 7)