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
答案 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)