Click here to Skip to main content
15,923,789 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Python
#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
Posted
Updated 8-Jan-17 2:48am
v2
Comments
Member 12939713 8-Jan-17 8:19am    
how do I fix it then
[no name] 8-Jan-17 8:24am    
"I have tried everything", clearly you have not. You haven't tried asking a question or describing a problem with the code you just dumped here.
Member 12939713 8-Jan-17 8:25am    
ok well can u help then
[no name] 8-Jan-17 10:13am    
I can try. Do you have a question or a problem?

1 solution

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):
Python
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:
Python
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])
 
Share this answer
 
v2
Comments
Thomas Daniels 8-Jan-17 8:34am    
that happens in your for loop
Member 12939713 8-Jan-17 8:36am    
please can u just show me how to do it as I can't I have been trying all morning and I am getting so stressed and upset about this
Thomas Daniels 8-Jan-17 8:38am    
But what do I have to show you? You only have to replace your while loop with my while loop, that works.
Member 12939713 8-Jan-17 8:39am    
can u just send whole code finished as I am so confused
Thomas Daniels 8-Jan-17 8:42am    
See my updated answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900