如何通过Python上的RANGE ERROR列出LIST INDEX [英] How do I get past LIST INDEX OUR of RANGE ERROR on Python
本文介绍了如何通过Python上的RANGE ERROR列出LIST INDEX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
# 导入时间
import random
import time
freehours = []
nameslist = []
hoursbookedlist = []
学生 范围内( 7 ):
name = input( 输入学生姓名)
hoursbooked = int(输入( 输入预订的小时数对于学生))
(hoursbooked< 10):
打印( 请重新输入预订的小时数:)
hoursbooked = int(输入( 请输入已为stundent1预订的小时数))
如果 hoursbooked> 10 和 hoursbooked< 15 :
freehours.append( 1 )
elif int(hoursbooked> = 15):
freehours.append( 2 )
nameslist.append(name)
hoursbookedlist.append(hoursbooked)
print ( student \t hoursbooked \t freehours)
for i 范围内( 7 ):
print (nameslist [i], \t, \t,hoursbookedlist [i], \t, \\ t,freehours [i])
我尝试过:
我已经尝试过一切
解决方案
问题是你的而$ c如果 hoursbooked
大于或等于10,则$ c> -loop永远不会输入。
另请注意你的条件是 hoursbooked< 10
,但你的if语句不能很好地处理输入 10
。你需要 elif hoursbooked> = 15
而不是 elif int(hoursbooked> = 15)
:后者转换boolean为整数,并且您不需要转换,因为hoursbooked已经是一个整数。
如果输入不是整数,您的代码将抛出ValueError。我建议阅读: 8。错误和例外 - Python 3.6.0文档 [ ^ ]
要解决这些问题,你可以这样做来替换而
-loop(以下代码没有捕获潜在的ValueErrors;我会留给你的):
而 < span class =code-keyword> True :
hoursbooked = int(输入( 输入为学生预订的小时数(至少10)))
如果 hoursbooked> = 10 和 hoursbooked< 15 :
freehours.append( 1 )
break
elif hoursbooked> = 15 :
freehours.append( 2 )
break
A 而True
循环将无限期地继续,如果它从未被告知停止。如果您的程序输入 if
或 elif
块,我们知道输入是正确的。所以在这个块中,我们放了一个 break
语句,这是从循环中逃脱的信号。如果未输入if或elif块,则输入小于10并且循环内的代码再次执行。
完整代码为:
import random
import 时间
freehours = []
nameslist = []
hoursbookedlist = []
for student 在范围内( 7 ):
name = input( 输入学生姓名)
True :
hoursbooked = int(输入( 输入为学生预订的小时数(至少10小时)))
如果小时书> = 10 和小时书< 15 :
freehours.append( 1 )
break
elif hoursbooked> = 15 :
freehours.append( 2 )
break
nameslist.append(name )
hoursbookedlist.append(hoursbooked)
print ( student \t hoursbooked \t freehours)
print (名单)
print (freehours)
print (hoursbookedlist)
for i 在范围内( 7 ):
< span class =code-keyword> print (nameslist [i],< span class =code-string> \t, \t,hoursbookedlist [i], \ t , \t,freehours [i])
#Imports Time
import random
import time
freehours = []
nameslist = []
hoursbookedlist = []
for student in range(7):
name = input("Enter students name ")
hoursbooked = int(input("Enter number of hours booked for the students "))
while (hoursbooked <10):
print("Please re-enter number of hours booked: ")
hoursbooked = int(input("Please enter the number of hours that have been booked for stundent1 "))
if hoursbooked > 10 and hoursbooked < 15:
freehours.append(1)
elif int(hoursbooked >=15):
freehours.append(2)
nameslist.append(name)
hoursbookedlist.append(hoursbooked)
print ("student \t hoursbooked \t freehours ")
for i in range(7):
print(nameslist[i],"\t","\t",hoursbookedlist[i],"\t","\t",freehours[i])
What I have tried:
I have tried everything
解决方案
The problem is that yourwhile
-loop never gets entered ifhoursbooked
is greater than or equal to 10.
Also note that your while condition ishoursbooked < 10
, but your if-statements do not deal well with the input10
. And you needelif hoursbooked >= 15
rather thanelif int(hoursbooked >= 15)
: the latter converts the boolean to an integer, and you don't need the conversion because hoursbooked is an integer already.
Your code will throw a ValueError if the inputs are not integers. I recommend reading this: 8. Errors and Exceptions — Python 3.6.0 documentation[^]
To fix the issues, you can do this to replace thewhile
-loop (the following code does not catch potential ValueErrors; I'll leave that for you):
while True: hoursbooked = int(input("Enter number of hours booked for the students (at least 10) ")) if hoursbooked >= 10 and hoursbooked < 15: freehours.append(1) break elif hoursbooked >= 15: freehours.append(2) break
Awhile True
loop would go on infinitely if it's never told to stop. If your program enters theif
orelif
block, we know that the input is correct. So in this block, we put abreak
statement, which is the signal to escape from the loop. If the if or elif blocks aren't entered, the input is lower than 10 and the code inside the loop gets executed again.
The full code would be:
import random import time freehours = [] nameslist = [] hoursbookedlist = [] for student in range(7): name = input("Enter students name ") while True: hoursbooked = int(input("Enter number of hours booked for the students (at least 10) ")) if hoursbooked >= 10 and hoursbooked < 15: freehours.append(1) break elif hoursbooked >= 15: freehours.append(2) break nameslist.append(name) hoursbookedlist.append(hoursbooked) print ("student \t hoursbooked \t freehours ") print(nameslist) print(freehours) print(hoursbookedlist) for i in range(7): print(nameslist[i],"\t","\t",hoursbookedlist[i],"\t","\t",freehours[i])
这篇关于如何通过Python上的RANGE ERROR列出LIST INDEX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文