Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<pre>''' can anyone please explain me these two line of codes and how do they work''' '''letter = random.choice[position] is this a list now? and display[position] = letter can anyone please explain me how is these two code working in details please i didnt understad how display which is a list now is also a position is this position a list now?


What I have tried:

import random

random_list = ["Apples", "Oranges", "Pea", "Cherries"]

random_choice = random.choice(random_list)
display = []
answer = print("The word is",random_choice)
new_word = len(random_choice)

guess = input("Please enter random characters : \n")

for i in  range(0, new_word):
    display += "_"
print(display)

for position in range(0, new_word):
    letter = random_choice[position]
    if letter == guess:
        display[position] = letter

print(display)
Posted
Updated 10-Oct-21 10:07am

1 solution

position is an index into the list: Python - Lists[^]
Which explains both lines, if you read the link ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900