LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-22-2019, 08:11 AM   #1
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Rep: Reputation: 43
[PYTHON] OR operator works like AND


Im having a problem with "OR" operator in "if" condition.
At the moment it works like AND.

quit() should be executed if any of the inputs is not numerical/intiger.
Meanwhile, it works ONLY if both inputs are non-numerical/intiger.

I tried to replace | with || and or. No success

Can anyone help me out here? What im doing wrong?

Code:
#!/usr/bin/python3
import sys

a=input("one: ")
b=input("two: ")

if( a.isdigit() | b.isdigit() ):
    print("User input is Number ")
else:
    print("One of provided inputs is not a number")
    quit()

print (a)
print (b)

Last edited by czezz; 11-22-2019 at 08:39 AM.
 
Old 11-22-2019, 08:36 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
That's not the "or" operator. The "or" operator is or.
Code:
if a.isdigit() or b.isdigit():
 
Old 11-22-2019, 08:39 AM   #3
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
Hi Dugan, thanks for reply. Unfortunately, as I mentioned in my 1st post - "or" does not work neither.
 
Old 11-22-2019, 08:49 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Test with and, not or.
 
1 members found this post helpful.
Old 11-22-2019, 08:50 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,749

Rep: Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928
Isn't that how or works?
1 or 1 = 1
1 or 0 = 1
0 or 1 = 1
0 or 0 = 0

You need to check each variable separately or use an and...

Last edited by michaelk; 11-22-2019 at 08:53 AM.
 
1 members found this post helpful.
Old 11-22-2019, 08:51 AM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Code:
#!/usr/bin/python3
a=input("one: ")
b=input("two: ")

if a.isdigit() and b.isdigit():
    print("User input is Number ")
else:
    print("One of provided inputs is not a number")
    quit()

print (a)
print (b)

Last edited by dugan; 11-22-2019 at 08:53 AM.
 
Old 11-22-2019, 09:00 AM   #7
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
works!

Last edited by czezz; 11-22-2019 at 09:14 AM.
 
1 members found this post helpful.
Old 12-05-2019, 12:12 AM   #8
FlinchX
Member
 
Registered: Nov 2017
Distribution: Slackware Linux
Posts: 666

Rep: Reputation: Disabled
OP, do you understand the difference between a digit and a number? If I wanted to check if a string represents a valid number, I'd rather try to convert it to a number and catch the exception if it fails.
 
Old 12-09-2019, 03:50 AM   #9
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
number = 1 or more digits.
digit = 0 - 9
However, function isdigit() seems to be working for both.
 
Old 12-09-2019, 08:35 AM   #10
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
@FlinchX: He's doing it correctly.
 
Old 12-09-2019, 12:31 PM   #11
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554
For the reference of anyone else confused:

In Python:
string.isdecimal() tests whether string contains only ASCII 0-9.
string.isdigit() tests whether string contains only ASCII 0-9 or superscript numbers.
string.isnumeric() tests whether every character has the Unicode "numeric" property.

Examples:
Code:
myvar = '1' -> returns true for isdecimal, isdigit, isnumeric
myvar = '1²' -> returns true for isdigit, isnumeric (false for isdecimal)
myvar = '½' -> returns true for isnumeric (false for isdecimal, isdigit)
myvar = '-1' -> returns false for all three
myvar = '1.0' -> returns false for all three
myvar = '1e10' -> returns false for all three

It's not clear from the OP's "...inputs are non-numerical/intiger" whether isdigit is correct, whether isdecimal might be preferred, or whether a different check would be better.

Last edited by boughtonp; 12-09-2019 at 12:33 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: IBM's Red Hat Deal, NuoDB Operator Now Has Red Hat OpenShift Operator Certification, Krita 4.2.0 Alpha Released, Elive 3.0 Update, UBp LXer Syndicated Linux News 0 05-09-2019 06:21 PM
[SOLVED] C++ Operator Overloading Within an Already Overloaded Operator mirlin510 Programming 8 04-17-2011 12:02 PM
[SOLVED] Python equivalent of C ternary operator. pr_deltoid Programming 6 06-12-2010 04:31 PM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
I wuld like to use linux but im a cad operator so things like auto cad needs to work Juvencio Linux - General 7 06-15-2007 10:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration