1

Python 2.7. I am new to Python and I am stuck with while True loop. Simple program to calculate salary. When 'standard' entered as a letter, it catches the error and jumps again to the line 'Enter your rate'. I want it to be repeated only where the error was captured and not starting to input all info all over again. Can someone help please?

while True:
 try:
 rate = float(raw_input("Enter your rate: "))
 error = float(rate)
 standard = float(raw_input("Enter total standard hours 100%: "))
 error = float(standart) 
 except:
 print 'Not a number'
 continue 
 else:
 sum = standard * rate 
 print sum

Thank you in advance.

eyllanesc
246k19 gold badges205 silver badges282 bronze badges
asked Jun 30, 2018 at 10:58
1
  • 1
    add break in the end of else, after print sum Commented Jun 30, 2018 at 11:04

4 Answers 4

2
while True:
 try:
 rate = float(raw_input("Enter your rate: "))
 standard = float(raw_input("Enter total standard hours 100%: "))
 except ValueError:
 print 'Not a number' 
 else:
 sum = standard * rate 
 print sum
 break

You need to add a break at the end. Also you dont need to write error = float(..) when you are already trying to typecaste it in the input step.

Also, there is a typo in the line error = float(standart) . This will cause it to give exceptions forever.

Another good practice is to specify the type of error you expect ( ValueError ). This would help prevent things like typos.

answered Jun 30, 2018 at 11:05
Sign up to request clarification or add additional context in comments.

Comments

0

Try splitting standard and rate out of the same loop, like so.

def get_input(text):
 while 1:
 try:
 value = float(raw_input(text))
 break
 except:
 print 'Not a Number'
 return value
rate = get_input("Enter your rate: ")
standard = get_input("Enter total standard hours 100%: ")
sum = standard * rate
print(sum)

This way only the value that failed will be re-queried.

answered Jun 30, 2018 at 11:11

2 Comments

It does make sense and it did work. Thank you. Can you please only explain why did you put '1' after 'while'?
I'm glad it worked. In python 1 will evaluate as True, and 0 as false. So while 1 is equivalent to while True.
0

In your code continue instruction is not needed, as else instruction is executed only if no exception has been thrown.

But to exit the loop in the "positive" case, add break instruction after print sum.

And one more remark: Change standart to standard.

answered Jun 30, 2018 at 11:32

Comments

-1
while True:
try:
 rate = float(raw_input("Enter your rate: "))
 standard = float(raw_input("Enter total standard hours 100%: "))
except ValueError:
 print( 'Not a number' )
else:
 sum = standard * rate 
 print(sum)
 break
answered Jun 30, 2018 at 11:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.