Python Forum
Please help me to ask to enter again itself when value entered is not a number.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help me to ask to enter again itself when value entered is not a number.
#1
I want the following program to ask to enter again without ending if entered value is not a integer. please help.

x=input('ENTER SUM')
try:
    p=float(x)
except:
    print('Error! Try again')
    quit()
y=input('ENTER RATE PER ANNUM')
try:
    r=float(y)
except:
    print('Error! Try again')
    quit()
z=input('ENTER TIME IN MONTH')
try:
    fz=float(z)
except:
    print('Error! Try again')
    quit()
t=(fz/12)
print('Interest=Rs',p*t*r/100,'\nAmount to pay=Rs',p+p*t*r/100)
Reply
#2
Please help us help you by using proper tags in order to understand your code.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Probably, you have noticed that you have a lot of repeating code. This screams - define a function.
So first step would be to define a function that will take prompt and maybe conversion function and will return the user input converted using this conversion function.
In order to keep asking until you get valid input - ask inside infinite loop and return/breakout of the loop when user input is valid.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
[quote='buran' pid='123035' dateline='1597216916']
Probably, you have noticed that you have a lot of repeating code. This screams - define a function.
So first step would be to define a function that will take prompt and maybe conversion function and will return the user input converted using this conversion function.
In order to keep asking until you get valid input - ask inside infinite loop and return/breakout of the loop when user input is valid.
[/quo

Thank you!
Reply
#5
def input_float(prompt):
    while True:
        try:
            return float(input(prompt))
        except:
            pass

p = input_float('ENTER SUM ')
r = input_float('ENTER RATE PER ANNUM ')
t = input_float('ENTER TIME IN MONTH ') / 12

print('Interest=Rs',p*t*r/100,'\nAmount to pay=Rs',p+p*t*r/100)
Reply
#6
EDIT: ninjad by deanhystad

As suggested by buran - one option is to create validation function and call it three times:

def validate(request):
    while True:
        answer = input(request)
        try:
            return float(answer)
        except ValueError:
            print(f'Expected float but {answer!r} was entered')

total = validate('Enter sum: ')
rate = validate('Enter rate per annum: ')
time = validate('Enter time in month: ')

# or alternatively:

total, rate, time = (validate(f'Enter {item}: ') for item in ('sum', 'rate per annum', 'time'))
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Try,Except,Else to check that user has entered either y or n (Code block pasted) RandomNameGenerator 3 2,302 Jun-29-2021, 08:21 PM
Last Post: RandomNameGenerator
  How to reuse positional arguments entered in terminal. rcmanu95 1 1,841 Jul-04-2020, 01:00 AM
Last Post: bowlofred
  how to divide one big equation entered as a string to small chunks gungurbuz 1 1,590 May-28-2020, 03:46 PM
Last Post: Larz60+
  How to check if user entered string or integer or float?? prateek3 5 11,283 Dec-21-2019, 06:24 PM
Last Post: DreamingInsanity
  I want to check for the smallest value entered by the user Shilpa 2 1,775 Aug-13-2019, 12:06 PM
Last Post: ThomasL
  Saving entered text for next usement Rigunas 1 2,310 Dec-21-2018, 10:00 AM
Last Post: buran
  Guess Random Number Why i m not able to enter input Nithya Thiyagarajan 6 8,097 Jan-07-2018, 04:26 AM
Last Post: squenson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020