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
  How to convert while loop to for loop in my code? tatahuft 4 774 Dec-21-2024, 07:59 AM
Last Post: snippsat
  QThread Signal is emitted immediately at start of Code Caliban86 3 1,609 Sep-26-2024, 02:02 PM
Last Post: deanhystad
  I think I need to delete input data because returning to start fails thelad 2 1,052 Sep-24-2024, 10:12 AM
Last Post: thelad
  ValueError: could not broadcast input array from shape makingwithheld 1 2,150 Jul-06-2024, 03:02 PM
Last Post: paul18fr
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 2,443 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
Photo Python code: While loop with if statement HAMOUDA 1 1,319 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,916 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  Start Putty into Python Form Schlazen 5 8,409 Dec-13-2022, 06:28 AM
Last Post: divya130
  Code won't break While loop or go back to the input? MrKnd94 2 1,892 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  Python Idle won't start totalmachine 9 5,647 Oct-16-2022, 05:57 PM
Last Post: totalmachine

Forum Jump:

User Panel Messages

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