2

I'm working on this simple task where I need to use 2 while loops. The first while loop checks if the number of hours is less than 0 if it is then loop should keep asking the user. Here's my code:

hours = float(input('Enter the hours worked this week: '))

count = 0
while (0 > hours):
    print ('Enter the hours worked this week: ')
    count = count + 1

pay = float(input('Enter the hourly pay rate: '))
while (0 > pay):
    print ('Enter the hourly pay rate: ')
    count = count + 1

total_pay = hours * pay

print('Total pay: $', format(total_pay, ',.2f'))
1
  • Please use the correct formatting. Your code will not execute like that. Commented Feb 11, 2016 at 3:21

3 Answers 3

5

break is what you're looking for.

x = 100
while(True):
    if x <= 0:
        break
    x -= 1
print x # => 0

As for your example, there is nothing that would seem to cause a break to occur. For example:

hours = float(input('Enter the hours worked this week: '))

count = 0

while (0 > hours):
    print ('Enter the hours worked this week: ')
    count = count + 1

You are not editing the hours variable at all. This would merely continue to print out "Enter the hours worked this week: " and increment count ad infinitum. We would need to know what the goal is to provide any more help.

Sign up to request clarification or add additional context in comments.

5 Comments

Why not while (x > 0)?
Otherwise the break would not be demonstrated
The point is to show OP what break actually does. The while(True) is an infinite loop without a break statement.
I just wanted to make sure that no one actually uses break like this.
It doesn't solve OP's problem, which is they're not changing the values used in the condition.
4

You exit a loop by either using break or making the condition false.

In your case, you take input from the user, and if hours < 0, you print the prompt and update the count, but you don't update hours.

while (0 > hours):
    print ('Enter the hours worked this week: ')
    count = count + 1

should be:

while (0 > hours):
    hours = float(input('Enter the hours worked this week: '))
    count = count + 1

Similarly for pay:

while (0 > pay):
    pay = float(input('Enter the hourly pay rate: '))
    count = count + 1

5 Comments

@progx I'm not going to actually do your homework for you. Look at your solution and look at mine. It should be very clear where you need to make the change. You are substituting one line in your code for one line in my code for each while loop you've used.
I did what you suggested but it fails after I type 2 negative numbers for the hours worked. I'm using python 3.4 by the way
are you casting the input to a float? If so, what is the error you are seeing?
yup I wasn't casting the input to a float, but I solved the issue. Thanks by the way!
@ munk I'll mark your answer as the correct for this question!. Thank you!
1

Well, the other answer shows you how to break out of a while loop, but you're also not assigning to the pay and hours variables. You can use the built-in input function to get what the user supplied into into your program

hours = float(input('Enter the hours worked this week: '))

count = 0
while (0 > hours):
    hours = input('Enter the hours worked this week: ')
    count = count + 1

pay = float(input('Enter the hourly pay rate: '))
while (0 > pay):
    pay =  input('Enter the hourly pay rate: ')
    count = count + 1

total_pay = hours * pay

print('Total pay: $', format(total_pay, ',.2f'))

7 Comments

@ Garret your code fails after I enter 2 negative numbers.
@ Garret if I do this: Enter the hours worked this week: -30 and then this Enter the hours worked this week: -12 . I get an error
What error do you get? On my machine, it keeps asking me until I pass in a positive value
this one: TypeError: unorderable types: int() > str()
@GarrettR you're changing the type of hours to a string, then trying to compare it to a float.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.