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
#1
I want to check if The input is 'str' or it's 'int' and if it's 'str' repeat the loop...
but i don't know how to check if the input was for example: dds

while True:
    rows_input = input("Enter Rows: " )
    cols_input = input("Enter Cols: " )
    
    if int(rows_input) and int(cols_input): #here i'm trying to check it but i get error
        # i want to check if it's possible to convert the str to int then close the loop but i can't
        break
    else:
        print("you have to Enter a Number not string... [Try again]")
# Here are inputs
>>> Enter Rows: dds
>>> Enter Cols: 2
Error:
Traceback (most recent call last): File "Matrix_Calculator.py", line 5, in <module> if int(rows_input) and int(cols_input): #here i'm trying to check it but i get error ValueError: invalid literal for int() with base 10: 'dds'
but if the inputs be int, there's no problem and no error and the loop ends
>>> Enter Rows: 2
>>> Enter Cols: 2
Error:
No Error
Reply


Messages In This Thread
I want to check if the input is str or is int & if it's str repeat the loop - by HLD202 - Nov-23-2020, 02:31 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 422 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  How to automate loop test check on Network device jpc230 1 592 Oct-09-2023, 09:54 PM
Last Post: Larz60+
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,077 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,358 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Code won't break While loop or go back to the input? MrKnd94 2 967 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  Repeat request by else stsxbel 2 1,187 Jul-30-2022, 03:34 PM
Last Post: stsxbel
  get out of while loop and stop repeat Frankduc 11 3,012 Apr-26-2022, 10:09 PM
Last Post: deanhystad
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,500 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Avoid multiple repeat in indent Frankduc 8 2,908 Jan-18-2022, 05:46 PM
Last Post: Frankduc
Exclamation question about input, while loop, then print jamie_01 5 2,688 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