如何采用不同的输入类型并对每种类型做不同的事情?蟒蛇 3.5 [英] How to take different input types and do something different with each? Python 3.5
本文介绍了如何采用不同的输入类型并对每种类型做不同的事情?蟒蛇 3.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我正在尝试制作一个供个人使用的 Reddit 机器人,作为一个学习项目,但我在为输入添加错误异常时遇到了问题.
So I am trying to make a Reddit bot for personal use, as a learning project, and I am having trouble adding error exceptions for inputs.
这里是完整的源代码:http://pastebin.com/DYiun1ux
这里唯一有问题的部分是
The parts that are solely in question here are
while True:
if type(thing_limit) == int:
print("That is a number, thanks.")
break
elif type(thing_limit) == float:
print("You need a whole number.")
sys.exit()
elif type(thing_limit) == str:
print("You need a whole number.")
sys.exit()
else:
print("That is a number, thanks.")
break
我不确定如何确保输入的用户名有效.谢谢!
I'm not sure how I would go about making sure that the username that I put in is valid or not. Thanks!
推荐答案
input
总是返回一个字符串.您最好的选择是尝试将结果转换为整数.
input
always returns a string. Your best choice is trying to convert the result to an integer.
try:
thing_limit = int(thing_limit)
except ValueError:
print("You need a whole number.")
sys.exit()
else:
print("That is a number, thanks.")
这篇关于如何采用不同的输入类型并对每种类型做不同的事情?蟒蛇 3.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文