-2

First, I would like to say I am new to coding and python in general. I just learned about importing and the random function.

Anyways, I am trying to create a text game, in python, where a letter is chosen randomly from some string/word and the user has to try and guess which letter was chosen. I think I understand how to do for loops well enough so that it continues until the correct letter is chosen, but I have no idea how to even go about randomly choosing the letter.

I would just like some help getting started. Thank you.

ibraek
  • 1
  • 1
    Did you check this [https://stackoverflow.com/questions/2823316/generate-a-random-letter-in-python][1] – Spectris Oct 28 '18 at 22:26
  • 1
    Possible duplicate of [How can I select random characters in a pythonic way?](https://stackoverflow.com/questions/14832105/how-can-i-select-random-characters-in-a-pythonic-way) – colidyre Oct 28 '18 at 22:32

3 Answers3

1

Since strings are sequences in Python, you can use random.choice to pick a random element from a list - in your case, a random letter in a string.

>>> import random
>>> c = random.choice("abcdefgh")
>>> c
'g'
>>> c = random.choice("abcdefgh")
>>> c
'a'

The >>>'s are from the REPL console (running Python by itself) and shouldn't be included if you include the code in a python file.

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
0

There is a library for randomizing things. Simply use it like this:

import random
text = "Some text"
your_variable = random.choice(text)

and you get your random letter from a sequence in that case which is saved in your_variable.

colidyre
  • 4,170
  • 12
  • 37
  • 53
0

Since you are relatively new to programming, I'd like to give you an informative example of the game to clarify some concepts.

  1. String str is implemented as sequence in Python (you can think that a string is an array of chars). So it supports indexing s = 'abc'; print(s[1]); shows b.
  2. The standard library random includes a bench of functions to carry out randomized operations. As mentioned in the other answers that the function random.choice randomly pick an element from a sequence. So it can be used to randomly pick a character from a string.
  3. You mentioned loops and you seem to be able to master them. So I hereby use another approach called recursion to continue the game until the desired answer is found.
  4. The game is written in an Object-Oriented manner. If you gonna be a developer latter in your career, it's good practice to see this.

Here is the Code:

import random

class Game:

    def __init__(self, string):
        self.string = string

    def start_game(self):
        self.answer = random.choice(string)
        self.ask_player()

    def ask_player(self):
        char = input("Just enter your guess: ")
        self.cheat(char)

        if char == self.answer:
            print("Bingo!")
            return None
        else:
            print("You missed, try again! (Or press Ctrl+C to goddamn exit!)")
            self.ask_player()

    def show_answer(self):
        print('The answer iiiis: %s \n' % self.answer)

    def cheat(self, user_input):
        if user_input == 'GG':
            self.show_answer()


if __name__ == '__main__':
    string = "This is the string from which the letters are ramdomly chosen!"
    gg = Game(string)
    gg.start_game() 

Some test runs:

Just enter your guess: 2
You missed, try again! (Or press Ctrl+C to goddamn exit!)

Just enter your guess: T
You missed, try again! (Or press Ctrl+C to goddamn exit!)

Just enter your guess: GG (Haha, I left a cheat code in order to get the right anwser!)
The answer iiiis: o 

You missed, try again! (Or press Ctrl+C to goddamn exit!)

Just enter your guess: o
Bingo!
Niko Z.
  • 342
  • 1
  • 11