Python guessing game : IndexError: range object index out of range
Python guessing game : IndexError: range object index out of range
我创建了一个游戏,其中我 select 一个从 1 到 100 的数字,然后计算机会猜它。然而,当我 selecting 数字低于 6 时,我得到 IndexError。为什么会这样?
这是我的代码:
print("Helllo user, select a number from 1-100 in your mind and i will try to guess it...")
list_nums = range(1,101)
counter = 0
while True :
mid_element = list_nums[int(len(list_nums)/2)-1]
print(" Is your selected number {}".format(mid_element))
counter = counter + 1
response = str(input("is it too high or too low..? : "))
try:
if response == "too low":
list_nums = list_nums[int(len(list_nums)/2):int(len(list_nums))]
mid_element = list_nums[int(len(list_nums)/2)]
elif response == "too high":
list_nums = list_nums[1:int(len(list_nums)/2)]
mid_element = list_nums[int(len(list_nums)/2)]
elif response == "correct":
print("Perfect i finally guessed it. Your number is {}\n".format(mid_element))
break
except:
print("Invalid entry..Try again")
continue
print("\nI guessed it in {} attempts".format(counter))
换行
list_nums = list_nums[1: int(len(list_nums)/2)]
至:
list_nums = list_nums[0: int(len(list_nums)/2)]
因为,列表索引从零开始。
或者:
list_nums = list_nums[: int(len(list_nums)/2)]
因为,Python 知道列表从零开始。
我在我的系统上检查了你的代码和 运行。每次你的列表大小都在减少,最后它变成 0。那时当你的代码计算行 mid_element = list_nums[int(len(list_nums)/2)-1]
时,答案是 -1,它超出了 运行ge.
我已经跟踪代码,输出结果如下:
Helllo user, select a number from 1-100 in your mind and i will try to guess it...
Length of list_num: 100
Is your selected number 50
is it too high or too low..? : too high
Length of list_num: 49
Is your selected number 25
is it too high or too low..? : too high
Length of list_num: 23
Is your selected number 13
is it too high or too low..? : too high
Length of list_num: 10
Is your selected number 8
is it too high or too low..? : too high
Length of list_num: 4
Is your selected number 6
is it too high or too low..? : too high
Length of list_num: 1
Is your selected number 6
is it too high or too low..? : too high
Invalid entry..Try again
Length of list_num: 0
所以根据上面的答案进行修改。
我创建了一个游戏,其中我 select 一个从 1 到 100 的数字,然后计算机会猜它。然而,当我 selecting 数字低于 6 时,我得到 IndexError。为什么会这样?
这是我的代码:
print("Helllo user, select a number from 1-100 in your mind and i will try to guess it...")
list_nums = range(1,101)
counter = 0
while True :
mid_element = list_nums[int(len(list_nums)/2)-1]
print(" Is your selected number {}".format(mid_element))
counter = counter + 1
response = str(input("is it too high or too low..? : "))
try:
if response == "too low":
list_nums = list_nums[int(len(list_nums)/2):int(len(list_nums))]
mid_element = list_nums[int(len(list_nums)/2)]
elif response == "too high":
list_nums = list_nums[1:int(len(list_nums)/2)]
mid_element = list_nums[int(len(list_nums)/2)]
elif response == "correct":
print("Perfect i finally guessed it. Your number is {}\n".format(mid_element))
break
except:
print("Invalid entry..Try again")
continue
print("\nI guessed it in {} attempts".format(counter))
换行
list_nums = list_nums[1: int(len(list_nums)/2)]
至:
list_nums = list_nums[0: int(len(list_nums)/2)]
因为,列表索引从零开始。
或者:
list_nums = list_nums[: int(len(list_nums)/2)]
因为,Python 知道列表从零开始。
我在我的系统上检查了你的代码和 运行。每次你的列表大小都在减少,最后它变成 0。那时当你的代码计算行 mid_element = list_nums[int(len(list_nums)/2)-1]
时,答案是 -1,它超出了 运行ge.
我已经跟踪代码,输出结果如下:
Helllo user, select a number from 1-100 in your mind and i will try to guess it...
Length of list_num: 100
Is your selected number 50
is it too high or too low..? : too high
Length of list_num: 49
Is your selected number 25
is it too high or too low..? : too high
Length of list_num: 23
Is your selected number 13
is it too high or too low..? : too high
Length of list_num: 10
Is your selected number 8
is it too high or too low..? : too high
Length of list_num: 4
Is your selected number 6
is it too high or too low..? : too high
Length of list_num: 1
Is your selected number 6
is it too high or too low..? : too high
Invalid entry..Try again
Length of list_num: 0
所以根据上面的答案进行修改。