|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
如何判断一个输入是否为整数
while i:
temp=input("input a number:")
if not temp.isdigit:
print("\t输入类型错误")
else:
num=int(temp)
if 0<num<100:
print("正确")
i-=1
else:
i-=1
print("错误,还有"+str(i)+"次机会")
if i==0:
print("遗憾,没猜中!")
输入字母或者小数就报错
input a number:i
Traceback (most recent call last):
File "F:/python/lesson1/hello.py", line 8, in <module>
num=int(temp)
ValueError: invalid literal for int() with base 10: 'i'
>>>
==================== RESTART: F:/python/lesson1/hello.py ====================
input a number:5.5
Traceback (most recent call last):
File "F:/python/lesson1/hello.py", line 8, in <module>
num=int(temp)
ValueError: invalid literal for int() with base 10: '5.5'
本帖最后由 新手·ing 于 2017-4-15 16:14 编辑
你已经写了
num=int(temp)
int会把字符串转变成整数
5.5是小数,l是字符串
当然都不行
- while i:
- temp=input("input a number:")
-
- if not temp.isdigit():
- print("\t输入类型错误")
- else:
- num=int(temp)
- if 0<num<100:
- print("正确")
- i-=1
-
- else:
- i-=1
- print("错误,还有"+str(i)+"次机会")
- if i==0:
- print("遗憾,没猜中!")
复制代码
|
-
-
|