简体   繁体   中英

How do you make a “game” that takes a random element from the list and ask for a guess input on which index that element is on?

If I for example have the class:

class A:
    def _init_(self, letter, number, index)
        self.letter = letter
        self.number = number
        self.index = index

I have this list(could be much longer), example:

list = [['D', 123], ['B', 133], ['C',229], ['A', 189], ['F', 630], ['K', 278], ['V', 780], ['O', 819], ['L', 200], ['N', 982], ['M', 562], ['P', 628], ['T', 299]]

(The numbers in the list should not matter in these questions, they have to be there for now. Only the letters are important.)

Menu:

def menu():
    print("a. Number game")
    print("b. Letter game")
    choice = input("what's your choice?: ")
    return choice

It should have the functions that are called when you choose that option:

def number_game(): 
    pass

def letter_game():
    pass

If you choose the option "a" I want the program to choose (slump) a random letter from the list and make the user guess on which index of the list they are. For example question: "In which place is the letter A?", the answer should be an index number(place) the user thinks A is on. If the answer is right the program should go on with the next question asking about a random letter again and so on until the user decides to quit.

If you choose the option "b" I want the program to do the exact opposite. The program should ask "which letter is on the second place?" and the input should be a letter. How could you make this work? Thankful for any help!

For option a, you could pick a random letter from the list and then check if the value your user picks is equal to the target value.

For option b, you could get input from the user and use an argmax function to determine if the user input matches the target index.

You could try this (the gamelist variable in this case is the list, because you shouldn't use list as your variable. It's a Python keyword. Also, in this case, I'm assuming you still have the useless numbers next to the letters.):

import random

gamelist = [['D', 123], ['B', 133], ['C',229], ['A', 189], ['F', 630], ['K', 278], ['V', 780], ['O', 819], ['L', 200], ['N', 982], ['M', 562], ['P', 628], ['T', 299]]

def number_game():

  lindex = random.randint(0, len(gamelist)) #Getting a random index of the list
  print("Where is the letter " + gamelist[lindex][0] + "?") #[0] is for the letter instead of the number
  choice = input(">")
  if choice == str(lindex):
      print("You got it right!")
      #Do whatever you would want to happen after they get it right. return will end the function.
      return
  else:
      print("Oops, that's wrong! It was at " + str(lindex))
      #This is, you guessed it, when they get it wrong.
      return

def letter_game():

  lindex = random.randint(0, len(gamelist)) #Getting a random index of the list
  print("What letter is at index " + str(lindex) + "?")
  choice = input(">").upper() #upper is so they can do lowercase
  if choice == gamelist[lindex][0]:
      print("You got it right!")
      #Do whatever you would want to happen after they get it right. return will end the function.
      return
  else:
      print("Oops, that's wrong! it was " + gamelist[lindex][0])
      #This is, you guessed it, when they get it wrong.
      return

#The code below would be for checking which option the user chose and acting upon it

option = "b" #You should set this up to ask for user input later

if option == "a":
    number_game()
elif option == "b":
    letter_game()
else:
    print("Invalid option")

Try it on repl.it: https://replit.com/@PythonPower312/LittleGame#main.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM