The problem is that your
while
-loop never gets entered if
hoursbooked
is greater than or equal to 10.
Also note that your while condition is
hoursbooked < 10
, but your if-statements do not deal well with the input
10
. And you need
elif hoursbooked >= 15
rather than
elif 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 the
while
-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
A
while True
loop would go on infinitely if it's never told to stop. If your program enters the
if
or
elif
block, we know that the input is correct. So in this block, we put a
break
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])