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.
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.
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.
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.
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
Related
I am a beginner programmer, working on a project for an online course. I am trying to build a tip calculator. I want it to take input from the user for three values: Bill total, how many are splitting the bill, and the percent they would wish to tip. My conditional statement only has one if:
if meal_price >= 0.01:
example(example)
else:
example(example)
There are no elifs, only an else clause, stating to the user to enter only a numerical value. The program is designed to loop if the else clause runs, or continue if the 'if' condition is met. I would like this program to be completely user-friendly and run regardless of what is typed in. But instead of the else clause being ran when a user enters a string value, the terminal returns an error. How would I check the datatype the user enters, and run my conditional statement based off of that instead of the literal user response?
Note, I've tried:
if isinstance(meal_price, float):
Converting the user input into a string, but then the conditional statement becomes the problem
Thank you all for the help. I started my coding journey about 3 months ago and I am trying to learn as much as I can. Any feedback or criticism is GREATLY appreciated.
enter image description here
def calculation():
tip_percent = percentage / 100
tip_amount = meal_price * tip_percent
meal_and_tip = tip_amount + meal_price
total_to_return = meal_and_tip / to_split
return total_to_return
print("\nWelcome to the \"Bill Tip Calculator\"!")
print("All you need to do is enter the bill, the amount of people splitting it, and the percent you would like to tip.\n")
while True:
print("First, what was the total for the bill?")
meal_price = float(input("Bill (Numerical values only): "))
if meal_price >= 0.01:
meal_price2 = str(meal_price)
print("\nPerfect. The total is " + "$" + meal_price2 + ".")
while True:
print("\nHow many people are splitting the bill?")
to_split = int(input("People: "))
if to_split >= 1:
to_split2 = str(to_split)
print("\nAwesome, there is", "\"" + to_split2 + "\"", "person(s) paying.")
while True:
print("\nWhat percent would you like to tip?")
percentage = float(input("Percentage (Numerical values only, include decimals): "))
if percentage >= 0:
percentage2 = str(percentage)
print("\nGot it.", percentage2 + '%.')
calculation()
total = str(calculation())
#total2 = str(total)
print("\n\nEach person pays", "$" + total + ".")
exit()
else:
print("\nPlease enter only a numerical value. No decimals or special characters.")
else:
print("\nPlease respond with a numerical value greater than 0.\n")
else:
print("Please remember to enter only a numerical value.\n")
Included image snapshot in case copy & paste isn't accurate.
The user's input will be a string, so you need to check if the parse to the float was successful. You can do that with a try/except, and then loop back over asking for more input:
print("First, what was the total for the bill?")
meal_price = None
while meal_price == None:
try:
meal_price = float(input("Bill (Numerical values only):"))
except ValueError:
print("That didn't look like a number, please try again")
print(meal_price)
Adding on to #OliverRadini's answer, you use the same structure a lot for each of your inputs that could be generalized into a single function like so
def get_input(prompt, datatype):
value = input(prompt)
try:
a = datatype(value)
return a
except:
print("Input failed, please use {:s}".format(str(datatype)))
return get_input(prompt, datatype)
a = get_input("Bill total: ", float)
print(a)
Perhaps the main point of confusion is that input() will always return what the user enters as a string.
Therefore trying to check whether meal_price is something other than a string will always fail.
Only some strings can be converted into floats - if you try on an inappropriate string, an exception (specifically, a ValueError) will be raised.
So this is where you need to learn a bit about exception handling. Try opening with this block:
meal_price = None
while meal_price is None:
try:
meal_price = float(input("Bill (Numerical values only): "))
except ValueError:
print("Please remember to enter only a numerical value.\n")
This will try to execute your statement, but in the event it encounters a value error, you tell it not to raise the exception, but to instead print a message (and the loop restarts until they get it right, or a different kind of error that you haven't handled occurs).
Thank you all! After looking into your comments and making the amendments, my program works perfectly! You lot rock!!
def calculation():
tip_percent = tip / 100
tip_amount = bill * tip_percent
meal_and_tip = tip_amount + bill
total_to_return = meal_and_tip / to_split
return total_to_return
def user_input(prompt, datatype):
value = input(prompt)
try:
input_to_return = datatype(value)
return input_to_return
except ValueError:
print("Input failed, please use {:s}".format(str(datatype)))
return user_input(prompt, datatype)
print("\nWelcome to the \"Bill Tip Calculator\"!")
print("\nAll you need to do is:\n1.) Enter your bill\n2.) Enter the amount of
people (if bill is being split)\n3.) Enter the amount you would like to
tip.")
print("\n\n1.) What was the total for the bill?")
bill = user_input("Total Bill: ", float)
print("\nAwesome, the total for your meal was " + "$" + str(bill) + ".")
print("\n\n2.) How many people are splitting the bill?")
to_split = user_input("Number of People: ", int)
print("\nSo the bill is divided", str(to_split), "way(s).")
print("\n\n3.) What percent of the bill would you like to leave as a tip?
(Enter a numeral value only. No special characters.)")
tip = user_input("Tip: ", int)
print("\nYou would like to tip", str(tip) + "%! Nice!")
total = calculation()
print("\n\n\n\nYour total is " + "$" + str(total), "each! Thank you for using
the \"Bill Tip Calculator\"!")
I have been tasked with creating a python program that will ask for user inputs and calculate monthly loan repayments, this is the formula I have to work off: Formula. Doing this was not too difficult, however the tutor asked as a bonus to try to make the user inputs 'unbreakable', meaning if any value other than the expected were input it wouldn't break the program. I thought this can easily be done with Try Except, and that's what I did, however I believe my code can be written in a much more concise manner, instead of a Try Except for every input as below:
err = "Please enter a number only!!"
while True:
try:
A = int(input("How much were you loaned? "))
except ValueError:
print(err)
continue
else:
break
while True:
try:
R = float(input("At what monthly rate interest? ")) / 100
except ValueError:
print(err)
continue
else:
break
while True:
try:
N = int(input("And how many years is the loan for? "))
except ValueError:
print(err)
continue
else:
break
RA = R * A
RN = 1 - (1 + R) ** -N
P = RA / RN
print("You will pay £", P, "yearly", "or, £", P / 12, "monthly")
I feel as if perhaps the user inputs could be put into a For Loop, or perhaps all of this into one Try Except block? After thinking about it, I put all the inputs into one Try Except block, however as soon as you fail to correctly input the expected data for the user input, it goes right back to the beginning, not to the question you were on. This is not what I want. Have a look:
err = "Please enter a number only!!"
while True:
try:
A = int(input("How much were you loaned? "))
R = float(input("At what monthly rate interest? ")) / 100
N = int(input("And how many years is the loan for? "))
except ValueError:
print(err)
continue
else:
break
RA = R * A
RN = 1 - (1 + R) ** -N
P = RA / RN
print("You will pay £", P, "yearly", "or, £", P / 12, "monthly")
How do you think I can modify this code to make it more concise, shorter, and efficient without having a Try Except for every user input?
Modularise it, and use a generic function that handles input and loops until the user enters what is asked.
def get_num(prompt="Enter number: "):
"""
(str) -> num
Outputs a prompt and loops until a valid number is
entered by the user, at which point that value is
returned and the function terminates
"""
while True:
num = input(prompt)
try:
num = float(num)
return num
except:
print("Must enter a valid number")
def do_calc():
"""(None) -> None
Get user input, perform calculations and output results
"""
A = int(get_num("How much were you loaned? "))
R = get_num("At what monthly rate interest? ") / 100
N = int(get_num("And how many years is the loan for? "))
RA = R * A
RN = 1 - (1 + R) ** -N
P = RA / RN
print(f"You will pay £{round(P, 2)} yearly or £{P/12:.2f} monthly")
do_calc()
Using the f-string from Python 3 is really nice and elegant- just plug in the values or calculations inside the curly braces. As per suggestion in comment, you may wish to show to two decimal places using either the round function or the formatting option as shown.
A more pythonic way to name your variables would be
amount rather than A
rate rather than R
num_years rather than N
and so on.
Extract the repeated procedure in a function, with all the relevant inputs.
I have added some formatting to the final output to avoid too many decimals
def get_value(message, func, denominator = 1):
try_again = True
v = None
while try_again:
try:
v = func(input(message))/denominator
except ValueError:
print("Please enter a number only!!")
else:
try_again= False
return v
A = get_value("How much were you loaned? ", int)
R = get_value("At what monthly rate interest? ", float, denominator=100.0)
N = get_value("And how many years is the loan for? ", int)
RA = R * A
RN = 1 - (1 + R) ** -N
P = RA / RN
print("You will pay £ {:.2f} yearly or £ {:.2f} monthly".format(P, P / 12))
my problem is i have to calculate the the sum of digits of given number and that no is between 100 to 999 where 100 and 999 can also be include
output is coming in this pattern
if i take a=123 then out put is coming total=3,total=5 and total=6 i only want output total=6
this is the problem
there is logical error in program .Help in resolving it`
this is the complete detail of my program
i have tried it in this way
**********python**********
while(1):
a=int(input("Enter any three digit no"))
if(a<100 or a>999):
print("enter no again")
else:
s = 0
while(a>0):
k = a%10
a = a // 10
s = s + k
print("total",s)
there is no error message in the program because it has logical error in the program like i need output on giving the value of a=123
total=6 but i m getting total=3 then total=5 and in last total=6 one line of output is coming in three lines
If you need to ensure the verification of a 3 digit value and perform that validation, it may be useful to employ Regular Expressions.
import re
while True:
num = input("Enter number: ")
match = re.match(r"^\d{3}$, num)
if match:
numList = list(num)
sum = 0
for each_number in numList:
sum += int(each_number)
print("Total:", sum)
else:
print("Invalid input!")
Additionally, you can verify via exception handling, and implementing that math you had instead.
while True:
try:
num = int(input("Enter number: "))
if num in range(100, 1000):
firstDigit = num // 10
secondDigit = (num // 10) % 10
thirdDigit = num % 10
sum = firstDigit + secondDigit + thirdDigit
print("Total:", sum)
else:
print("Invalid number!")
except ValueError:
print("Invalid input!")
Method two utilizes a range() function to check, rather than the RegEx.
Indentation problem dude, remove a tab from last line.
Also, a bit of python hint/tip. Try it. :)
a=123
print(sum([int(x) for x in str(a)]))
I am completing questions from a python book when I came across this question.
Write a program which repeatedly reads numbers until the user enters "done". Once done is entered, print out total, count, and average of the numbers.
My issue here is that I do not know how to check if a user specifically entered the string 'done' while the computer is explicitly checking for numbers. Here is how I approached the problem instead.
#Avg, Sum, and count program
total = 0
count = 0
avg = 0
num = None
# Ask user to input number, if number is 0 print calculations
while (num != 0):
try:
num = float(input('(Enter \'0\' when complete.) Enter num: '))
except:
print('Error, invalid input.')
continue
count = count + 1
total = total + num
avg = total / count
print('Average: ' + str(avg) + '\nCount: ' + str(count) + '\nTotal: ' + str(total))
Instead of doing what it asked for, let the user enter 'done' to complete the program, I used an integer (0) to see if the user was done inputting numbers.
Keeping your Try-Except approach, you can simply check if the string that user inputs is done without converting to float, and break the while loop. Also, it's always better to specify the error you want to catch. ValueError in this case.
while True:
num = input('(Enter \'done\' when complete.) Enter num: ')
if num == 'done':
break
try:
num = float(num)
except ValueError:
print('Error, invalid input.')
continue
I think a better approach that would solve your problem would be as following :
input_str = input('(Enter \'0\' when complete.) Enter num: ')
if (input_str.isdigit()):
num = float(input_str)
else:
if (input_str == "done"):
done()
else:
error()
This way you control cases in which a digit was entered and the cases in which a string was entered (Not via a try/except scheme).
I'm very new to programming, just started working my way through a Python course. I've been looking through the course material and online to see if there's something I missed but can't really find anything.
My assignment is to make a chatbot that takes input and summarizes the input but also calculates the average. It should take all the input until the user writes "Done" and then terminate and print the results.
When I try to run this:
total = 0
amount = 0
average = 0
inp = input("Enter your number and press enter for each number. When you are finished write, Done:")
while inp:
inp = input("Enter your numbers and press enter for each number. When you are finished write, Done:")
amount += 1
numbers = inp
total + int(numbers)
average = total / amount
if inp == "Done":
print("the sum is {0} and the average is {1}.". format(total, average))
I get this error:
Traceback (most recent call last):
File "ex.py", line 46, in <module>
total + int(numbers)
ValueError: invalid literal for int() with base 10: 'Done'
From searching around the forums I've gathered that I need to convert str to int or something along those lines? If there are other stuff that need to be fixed, please let me know!
It seems that the problem is that when the user types "Done" then the line
int(numbers) is trying to convert "Done" into an integer which just won't work. A solution for this is to move your conditional
if inp == "Done":
print("the sum is {0} and the average is {1}.". format(total, average))
higher up, right below the "inp = " assignment. This will avoid that ValueError. Also add a break statement so it breaks out of that while loop as soon as someone types "Done"
And finally I think you are missing an = sign when adding to total variable.
I think this is what you want:
while inp:
inp = input("Enter your numbers and press enter for each number. When you are finished write, Done:")
if inp == "Done":
print("the sum is {0} and the average is {1}.". format(total, average))
break
amount += 1
numbers = inp
total += int(numbers)
average = total / amount