![](/img/trans.png)
[英]How to check if a character is in a specific position in a string in Python?
[英]How to put a string in a specific index position in python
我試圖根據 awnser 將字符串 '^' 放在我的代碼中的特定索引 position 中,這是我的代碼。
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)
我不希望我的 output 是這個 ['','','O'] 這是典型的 output 在你弄錯問題后我希望它看起來像 ['','','O']在新行的“O”下方帶有“^”符號,我將如何做這樣的事情,我需要添加什么?
這將打印出 O 在列表中的位置
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)
第一個元素從 position 3 開始,並根據其在列表中的位置增加 4
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.