Python Forum
I want to check if the input is str or is int & if it's str repeat the loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I want to check if the input is str or is int & if it's str repeat the loop
#2
Check out this link to read about exception handling. In a nutshell, you can use try to attempt code that may cause an exception, and except to execute code only if an exception is encountered.

The example below will exit the while loop if both input variables are successfully converted to integers, but will print the try again message if a ValueError exception occurs.

while True:
    rows_input = input("Enter Rows: " )
    cols_input = input("Enter Cols: " )

    try:
        rows = int(rows_input)
        cols = int(cols_input)
        break

    except ValueError:
        print("you have to Enter a Number not string... [Try again]")
HLD202 likes this post
Reply


Messages In This Thread
RE: I want to check if the input is str or is int & if it's str repeat the loop - by GOTO10 - Nov-23-2020, 02:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 445 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  How to automate loop test check on Network device jpc230 1 600 Oct-09-2023, 09:54 PM
Last Post: Larz60+
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,104 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,384 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Code won't break While loop or go back to the input? MrKnd94 2 985 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  Repeat request by else stsxbel 2 1,201 Jul-30-2022, 03:34 PM
Last Post: stsxbel
  get out of while loop and stop repeat Frankduc 11 3,070 Apr-26-2022, 10:09 PM
Last Post: deanhystad
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,519 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Avoid multiple repeat in indent Frankduc 8 2,936 Jan-18-2022, 05:46 PM
Last Post: Frankduc
Exclamation question about input, while loop, then print jamie_01 5 2,715 Sep-30-2021, 12:46 PM
Last Post: Underscore

Forum Jump:

User Panel Messages

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