0

I am trying to program hangman. I am quite new and am wondering something. I already figured this out

 x = "Hello World"
print(x[2])
print(x[6])
print(x[10])

The output is

l
W
d

I am wondering if i can write this:

import random
list = ["Hi","Hello","Hi wrld","Hello world"]
chosen = list(random.randint(1,4)
nr_of_letters = len(chosen)
# j is the letter the user guesses
j = input()
if j == chosen[0,nr_of_letters]:
   print("something")
else:
   print("something else")

The error info I get is that the number has to be an integer. If I try writing it differently I have 2 options.

#the first option is :
j = int(input ())
#the second option is:
if j == chosen[0,int(nr_of_letters)]:

neither of them work. Please how can I write that correctly.

1
  • Try: chosen = list[random.randint(0,3)] Commented Oct 9, 2020 at 20:21

1 Answer 1

1

So first, to choose a random word from a list:

import random
words = ["Hi","Hello","Hi wrld","Hello world"]
chosen = random.choice(words)
# Chosen will now be one of the words in the list

Since you're trying to code hangman I assume you know you'll need a while loop to allow the user to keep giving input so I'll just work with what you've given here as an example. So for checking if they entered a correct letter:

j = input()
if j in chosen:
    print("something")
else:
    print("something else")

Is that ok?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.