Python Forum

Full Version: [split] Need help solving this
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
What's wrong in this why isn't it working?
franko = input("Please enter the text you want to translate")
if franko == "A" or "a":
    print("ا")
elif franko == "B" or "b":
    print("ب")
elif franko == "C" or "c":
    print("س")
elif franko == "D" or "d":
    print("د")
elif franko == "E" or "e":
    print("ي")
elif franko == "F" or "f":
    print("ف")
elif franko == "G" or "g":
    print("ج")
elif franko == "H" or "h":
    print("ه")
elif franko == "I" or "i":
    print("ي")
elif franko == "J" or "j":
    print("ج")
elif franko == "K" or "k":
    print("ك")
elif franko == "L" or "l":
    print("ل")
elif franko == "M" or "m":
    print("م")
elif franko == "N" or "n":
    print("ن")
elif franko == "O" or "o":
    print("و")
elif franko == "P" or "p":
    print("ب")
elif franko == "Q" or "q":
    print("ك")
elif franko == "R" or "r":
    print("ر")
elif franko == "S" or "s":
    print("س")
elif franko == "T" or "t":
    print("ت")
elif franko == "U" or "u":
    print("ي")
elif franko == "V" or "v":
    print("ف")
elif franko == "W" or "w":
    print("و")
elif franko == "X" or "x":
    print("اكس")
elif franko == "Y" or "y":
    print("ي")
elif franko == "Z" or "z":
    print("ز")
elif franko == "2":
    print("ء")
elif franko == "3":
    print("ع")
elif franko == "4":
    print("ش")
elif franko == "5":
    print("خ")
elif franko == "7":
    print("ح")
elif franko == "8":
    print("غ")
else:
    print("Ma3l4 a 7oby me4 maogoda fl kamos deh")
I think that this is not good practice to hijack other people threads.

Your code is working. Just probably not as you would like. You will never get past first 'if':

>>> 1 == 1 or 'a'                                                    
True
>>> 1 == 2 or 'a'                                                    
'a'
>>> bool(1 == 2 or 'a')                                              
True
Python documentation: Truth Value Testing

Quote:Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below.

By default, an object is considered true unless its class defines either a __bool__() method that returns False or a __len__() method that returns zero, when called with the object. Here are most of the built-in objects considered false:
  • constants defined to be false: None and False.
  • zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • empty sequences and collections: '', (), [], {}, set(), range(0)

Operations and built-in functions that have a Boolean result always return 0 or False for false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and and always return one of their operands.)
To tack onto perfingo's response, there are a few ways to fix this.

  1. Add another comparison.
    franko = input("Please enter the text you want to translate")
    if franko == "A" or franko == "a":
        print("ا")
  2. Use a tuple to contain the possible inputs and compare using "in".
    franko = input("Please enter the text you want to translate")
    if franko in ("A", "a"):
        print("ا")
  3. Or my personal favorite which involves a call to str.lower() and then comparison. This can be useful when the user types more than one character to control for capitalization errors.
    franko = input("Please enter the text you want to translate").lower()
    if franko == "a":
        print("ا")
Also check https://python-forum.io/Thread-if-struct...dictionary
The third example by @stullis will come handy in this case

as alternative approach - look at str.maketrans() and str.translate() methods