Python Forum
Python- Help with try: input() except ValueError: Loop code to start of sequence
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python- Help with try: input() except ValueError: Loop code to start of sequence
#1
Good Evening,
I have been learning python for about a week now and decided to try and write a financial calculator program (however painstaking due to my complete lack of knowledge. I am having trouble with user input and try:. I would like to restart the program back to user input when the user inputs a str or float rather than an int. I can get the program to print "please use a real number" if they input something other than an int, but I don't know how to get it to restart.

I know my code is probably dreadfully inefficient, and I am still working the calculation at the end for compound interest. (Help appreciated there too!)

Respectfully yours,
Aldi
try:
    age = int(input("What is your age?"))
    retirement_age = int(input("At what age would you like to retire?"))
except ValueError:
    print("Please use a whole number")
years_to_save = int(retirement_age-age)
print(f'You have {years_to_save} years to save.')
try:
    mnth_svng_no_intrest = int(input("How much money will you save per month?"))
except ValueError:
    print("Use a real number, rounded to the tenth")
life_svng_no_intrest = int(years_to_save*mnth_svng_no_intrest)*12
print(f'That is great! If you do NOTHING but save that money, you will have ${life_svng_no_intrest} at retirement.')
interst_at_ten = (mnth_svng_no_intrest(1*.1)**1)
Reply
#2
this should give you the idea on how to loop a code when error occured
while True:
    try:
        variable = int(input())
        break
    except:
        print("try again")
swallow osama bin laden
Reply
#3
Actually, the except clause should be qualified.
if you don't know what type of exception, you can find out as follows:
following will intentionally create an exception:
>>> def get_numbase():
...     numbase = 0
...     try:
...         numbase = int(input('Enter numbase: '))
...         return numbase
...     except:
...         print("Unexpected error:", sys.exc_info()[0])
...
>>> get_numbase()
Enter numbase: how about this
Unexpected error: <class 'ValueError'>
So this will show what type of exception is being thrown.

You can then correct your code as follows (yo can remove the import sys):
>>> def get_numbase():
...     numbase = 0
...     while True:
...         try:
...             numbase = int(input('Enter number base: '))
...             return numbase
...         except ValueError:
...             print('Please enter numeric number base')
...
>>> get_numbase()
Enter number base: How about this
Please enter numeric number base
Enter number base: 10
10
>>>
Now, if some exception other than ValueError occurs, you will still get a traceback, but
Value Error will be properly captured and delt with
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,053 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
Photo Python code: While loop with if statement HAMOUDA 1 572 Sep-18-2023, 11:18 AM
Last Post: deanhystad
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 1,031 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  Start Putty into Python Form Schlazen 5 5,457 Dec-13-2022, 06:28 AM
Last Post: divya130
  Code won't break While loop or go back to the input? MrKnd94 2 946 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  Python Idle won't start totalmachine 9 3,487 Oct-16-2022, 05:57 PM
Last Post: totalmachine
  Help Switching between keyboard/Mic input in my code Extra 1 1,084 Aug-28-2022, 10:16 PM
Last Post: deanhystad
  ValueError: Found input variables with inconsistent numbers of samples saoko 0 2,470 Jun-16-2022, 06:59 PM
Last Post: saoko
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,475 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  readline.parse_and_bind() does not work in start-up script of Python interpreter zzzhhh 0 1,520 Jan-18-2022, 11:05 AM
Last Post: zzzhhh

Forum Jump:

User Panel Messages

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