0

I am making a keyboard in python cause why not, but when the second def is called it prints var2 the way I want but not var1 some code

baseLetter = ("a")

letter1 = (baseLetter)
letter2 = (baseLetter)

def letter002():
  letter2 = input("")
  if letter2 == ("a"):
    letter2 = ("a")
    print("{}{}".format(letter1, letter2))
  elif letter2 == ("b"):
    letter2 = ("b")
    print("{}{}".format(letter1, letter2))
  elif letter2 == ("c"):
    letter2 = ("c")

def letter001():
  letter1 = input("")
  if letter1 == ("a"):
    letter1 = ("a")
    print("output - {}".format(letter1))
    letter002()
  elif letter1 == ("b"):
    letter1 = ("b")
    print("output - {}".format(letter1))
    letter002()
  elif letter1 == ("c"):
    letter1 = ("c")
    print("output - {}".format(letter1))
    letter002()

a small sample of code. main.py is only used to call this so it shouldn't change anything. I have tried putting all of the code on main.py but the same thing.

main.py is just letter001.letter001() and importing file letter001

I try to make it say "cb"
when I type for letter1 it gives me "c"
when I type for letter2 it gives me "ab"
I can't get rid what sets letter1 and letter2 to a certain letter or it won't work.

I am relatively new so don't really know exactly what I am doing, and I have tried to look up a solution but nothing.

001
  • 13,291
  • 5
  • 35
  • 66

1 Answers1

1

The answer to your question is really just to simplify your code.

def letter002(letter1):
    letter2 = input("")
    if letter2 == "a" or "b":
        print(f"{letter1} {letter2}")


def letter001():
    letter1 = input("")
    if letter1 == "a" or "b" or "c": # We dont need to repeat statements just use if statements
        print("output - {}".format(letter1))
        letter002(letter1)

The code above should work but it is important you understand why to avoid writing unnecessary amounts of code.

If I understand correctly - you are redefining a function for every letter. Instead we can loop our code multiple times and save the output so we can add to it again like in the function below.

def letters():
    letterbuffer = "" # Creating a variable to store the inputs
    while True:
        letter1 = input("")
        if letter1 == "a" or "b" or "c": # We dont need to repeated if  statements just use or
            print(f"output - {letter1}")
            letterbuffer += letter1
            print(letterbuffer)

Dharman
  • 30,962
  • 25
  • 85
  • 135