|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
temp = input('请输入您的分数:')
while not isinstance(temp,int):
temp = input('抱歉,您的输入有误,请重新输入一个0到100之间的数字:')
score = int(temp)
if score <= 100 and score >= 90:
print('A')
else:
if score < 90 and score >= 80:
print('B')
else:
if score < 80 and score >= 60:
print('C')
else:
print('D')
如上,运行之后如果输入abcd,是可以返回第二个input,但是之后输入数字,却运行不到下面的if循环,我这个架构跟小甲鱼005课后题的架构差不多,虽然他用的是isdigit函数,但是这题我觉得用isinstance的逻辑也没问题,为什么他的能跳到下一个if循环,我的就不行
下面附上小甲鱼的参考答案:
temp = input('请输入一个年份:')
while not temp.isdigit():
temp = input("抱歉,您的输入有误,请输入一个整数:")
year = int(temp)
if year/400 == int(year/400):
print(temp + ' 是闰年!')
else:
if (year/4 == int(year/4)) and (year/100 != int(year/100)):
print(temp + ' 是闰年!')
else:
print(temp + ' 不是闰年!')
首先指出你代码的不足:score = int(temp) # 这句没必要,已经判断过了int类型
而且isinstance判断也不对,input函数返回str,永远为False。
请参考我的标准代码。
=========================================
int score = int(input("Enter your score here "))
if score <0 or score >100:
print("Please enter a legal score !")
if score >=90:
print('A')
elif score >=80:
print('B')
elif score >=60:
print('C')
else:
print('D')
|
|