Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to put a string in a specific index position in python

I’m trying to put the string ‘^’ within a specific index position in my code depending on an awnser, this is my code.

mylist = ['', 'O', '']
from random import shuffle

def shuffle_list(mylist):
    shuffle(mylist)
    return mylist


def player_guess():
    guess = ''
    while guess not in ['0', '1', '2']:
        guess = input("Pick a number from 0, 1 or 2: ")

    return int(guess)

myindex = player_guess()

def check_guess(mylist, guess):
    if mylist[guess] == 'O':
        print("Correct")
        print(mylist)
    else:
        print('Wrong guess!')
        correct_pos = {mylist.index('O')}
        print(f"Your guess was at position {guess} the correct position was {correct_pos} \n {mylist} \n")
 

# INITAL LIST
mylist = ['', 'O', '']
# SHUFFLE LIST
mixedup_list = shuffle_list(mylist)
# USER GUESS
guess = player_guess()
# CHECK GUESS
check_guess(mixedup_list, guess)

I don’t want my output to be this [”,”,’O’] which is the typical output after you have gotten the question wrong I want it to look like [”,”,’O’]
with the ‘^’ symbol right under the ‘O’ on a new line, how would I do something like this and what do I need to add?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This will print out where the O is in the list

def check_guess(mylist, guess):
    if mylist[guess] == 'O':
        print("Correct")
        print(mylist)
    else:
        print('Wrong guess!')
        correct_pos = {mylist.index('O')}
        print(f"Your guess was at position {guess} the correct position was {correct_pos} \n {mylist}")
        carrot_string = ' '*(3+mylist.index('O')*4) + '^'
        print(carrot_string)

the first element starts at position 3 and increases by 4 depending on where its located in the list

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading