**编辑:***已解决!*检测输入是int还是str•Python 2.7
编程一个程序,它会告诉你,如果你足够大,可以投票。而且,当它询问你的年龄时,如果用户输入的是字母而不是数字,我希望它能说某件事,例如“请输入数字,而不是字母,重新启动”。这里是我现在所拥有的代码:
name = raw_input("What is your name?: ")
print("")
print("Hello, "+name+".\n")
print("Today we will tell you if you are old enough to vote.")
age = input("How old are you?: ")
if age >= 18:
print("You are old enough to vote, "+name+".")
elif age < 18:
print("Sorry, but you are not old enough to vote, "+name+".")
我建议你使用字符串格式化,即“用'.'' '你好,%S替换所有这些' '喂,' +名字+'。 %name'。这里有一个很好的字符串格式化教程:https://pyformat.info/ –