0

this is my code:

ranking_dict = {}
current_ranking = open("ladder.txt")
a=0
for line in current_ranking:
    x = line.rstrip("\n")
    a+=1
    ranking_dict[int(a)]=x

## query: check if the player is in the ladder
def checkplayer():
    username = input("Please input in your name in the format J Tan or JY Tan, where J/JY are the initial(s) of your first name, and Tan is your last name: ")
    if username in ranking_dict.values():
        print("You are currently in the ladder")
    else:
        print("You are currently not in the ladder, please register")
    username = None
    return

## query: list ranking of players
def query_ranking():
    for key in ranking_dict:
        print(key, " : ", ranking_dict[key])
    return

## list of queries
def userqueries():
    print ("Please enter in your query:")
    print ("Challenge, Check Player, Check Rankings, etc")
    query = input(":")
    if query == str("Check Player") or str("check player") or str("checkplayer"):
        checkplayer()
    elif query == str("Check Rankings") or str("check rankings") or str("checkrankings"):
        query_ranking()
    else:
        print ("That was an invalid query")
    return

userqueries()

Currently what I'm trying to do is call a different function based on different user inputs. But when I type in anything it immediately calls the first function checkplayer(). Did I screw up the input recognizing part of the code?

1 Answers1

1

To check for multiple equality conditions, you need to repeat the equality comparison for each option:

if query == "Check Player" or query == "check player" or query == "checkplayer":
   # ...

Other notes:

  1. you don't need to wrap a string literal in the str() function, as they are already strings.
  2. To express the checks more succinctly, you could check that the query is in a set of valid queries:
check_player_queries = set(["Check Player", "check player", "checkplayer"])
if query in check_player_queries:
    checkplayer()
  1. If your query validation logic accepts case-insentive and whitespace-insensitive commands, you could simplify the check by normalising the input query through transforming into lowercase and removing the whitespace:
def normalise(s):
  return s.lower().replace(" ", "")

query = normalise(input(":"))
if query == "checkplayer":
    checkplayer()
elif query == "checkrankings":
    query_ranking()
Anson Miu
  • 1,171
  • 7
  • 6