-2

HOW DO I SHUFFLE THE INDEX LIST? with this code and the variable list

#turns string to list
list=[]
#gets the sentence to count and what to count and deletes punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
sentence=input("Enter sentence to count: ")
sentence=sentence.lower()

no_punct = ""
for char in sentence:
   if char not in punctuations:
      no_punct = no_punct + char

list=no_punct.split()

#this shows whether it is there or not
index=[index+1 for index,list in enumerate(list)]
print("the indices are ",index)

1 Answers1

0

The following code worked for me. You had trouble with random.shuffle, but I got it working here. Remember that it does not return anything, so you have to use it first, and then print after shuffling.

import random

#turns string to list
list=[]
#gets the sentence to count and what to count and deletes punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
sentence=input("Enter sentence to count: ")
sentence=sentence.lower()

no_punct = ""
for char in sentence:
   if char not in punctuations:
      no_punct = no_punct + char

list=no_punct.split()

#this shows whether it is there or not
index=[index+1 for index,list in enumerate(list)]
print("the indices are ",index)

print(index)
random.shuffle(index)
print(index)

Edit: This probably would make it a duplicate question, as mentioned in the comments.

coralvanda
  • 6,431
  • 2
  • 15
  • 25