Python Forum
How do I print "string involved" if one or more of my variables are strings?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I print "string involved" if one or more of my variables are strings?
#1
The below has no synthetical or static semantic errors in it, but does not run as expected. Namely, I want my shell to print "string involved" if the type of varA or varB is string; instead it prints nothing. Could anyone help me understand what is wrong with my code and how to fix it?

varA = 1
varB = "A"

if (type(varA) and type(varB)) is (int or float):
    if varA > varB:
        print("bigger")
    if varA == varB:
        print("equal")
    if varA < varB:
        print("smaller")
elif (type(varA) or type(varB)) is str:
    print("string involved")
Reply
#2
well, that's one hell of a script..
if (type(varA) and type(varB)) is (int or float):
is same as
if str is int: # and that evaluates to False
and

elif (type(varA) or type(varB)) is str:
is same as
elif int is str: # again evaluates to False
for better understanding read
https://python-forum.io/Thread-Multiple-...or-keyword

that said, checking types in Python is not common practice. Read duck typing
finally, varA is int and varB is str, comparison betwen different types could yield unexpected results
>>> 1>'A'
False
>>> 1=='A'
False
>>> 1<'A'
True
>>>
Reply
#3
Usually input is taken as strings. So usually both variables would be a string. In that case you could utilize string methods such as str.isdigit()

Otherwise you should use the isinstance built in rather than type..
if isinstance(varA, (int, float)) and isinstance(varB, (int, float)):
    if varA > varB:
        print("bigger")
    elif varA == varB:
        print("equal")
    elif varA < varB:
        print("smaller")
elif isinstance(varA, str) or isinstance(varB, str):
    print('string involved')
However in python you should ask for forgiveness rather than permission. You can do any variation of try/except here, but here is a version. I kept the checking for string, because you can still make those comparisons with strings, otherwise the if condition wouldn't even be there.
if isinstance(varA, str) or isinstance(varB, str):
    print('string involved')
else:
    try:
        if varA > varB:
            print("bigger")
        elif varA == varB:
            print("equal")
        elif varA < varB:
            print("smaller")
    except TypeError:
        pass
Recommended Tutorials:
Reply
#4
(Oct-02-2017, 02:44 PM)Shellburn Wrote: The below has no synthetical or static semantic errors in it, but does not run as expected.


You don't know what a semantic error is, then.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 699 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Variables in a SCPI string thomaswfirth 3 861 May-22-2023, 05:04 PM
Last Post: thomaswfirth
  How to print variables in function? samuelbachorik 3 851 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  Remove a space between a string and variable in print sie 5 1,706 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Can you print a string variable to printer hammer 2 1,894 Apr-30-2022, 11:48 PM
Last Post: hammer
  Splitting strings in list of strings jesse68 3 1,704 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Search multiple CSV files for a string or strings cubangt 7 7,842 Feb-23-2022, 12:53 AM
Last Post: Pedroski55
  Referencing string names in df to outside variables illmattic 1 1,328 Nov-16-2021, 12:47 PM
Last Post: jefsummers
  Print first day of the week as string in date format MyerzzD 2 1,980 Sep-29-2021, 06:43 AM
Last Post: MyerzzD
  print function output wrong with strings. mposwal 5 3,046 Feb-12-2021, 09:04 AM
Last Post: DPaul

Forum Jump:

User Panel Messages

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