打印"抱歉,我不明白你的意见......"每次。
如果我删除输入的异常部分只是整数,它就可以正常工作。
以下是代码:
num= input('Please think of a number between 0 and 100!')
a=int(num)
if type(a)=="int":
low=0
high=100
mid= (low+high)/2
while round(mid) != a:
print("Is your secret number", round(mid),"?")
x=input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly.")
if x=="h":
high=round(mid)
elif x=="l":
low=round(mid)
elif x=="c":
break
else:
print("Sorry, I did not understand your input.")
mid=(low+high)/2
if round(mid) ==a:
print("Is your secret number", round(mid),"?")
y=input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly.")
print("Game over. Your secret number was:", round(mid))
else:
print("Sorry, I did not understand your input...")
答案 0 :(得分:3)
由于type(a) == 'int'
部分而无法正常工作。请改用type(a) == int
或isinstance(a, int)
。话虽如此,该语句没有任何意义,因为int(num)
预先在异常无效的情况下引发异常。
# First make sure if the number is even a valid integer before casting.
if num.isdigit():
a = int(num)
...
else:
print("Sorry, I did not understand your input")
答案 1 :(得分:2)
它没有超过第一个if语句
>>>type(1)=='int'
False
您可以使用isinstance
(尽管在您的代码中,此时它可能不是整数)
if isinstance(a, int):