全部。我對 Python 有點陌生。我最近參加了一門課程并學習了一些教程。我正在嘗試自己探索并制作“一些東西”。這是一個基于文本的 RPG。我正在嘗試從我已經創建的預先存在的串列中讀取用戶輸入,以防他們以不同的方式鍵入選項。
right_Choices = ["right", "Right", "RIGHT"]
left_Choices = ["left", "Left", "LEFT"]
稍后在代碼中,我問用戶他們想“向左還是向右”走哪個方向?在他們輸入之后,我試圖讓代碼理解任何可能的輸入,即如果他們先輸入“左”,然后再輸入“左”或“左”,它仍然會繼續理解。
# Check user input for left or right
left_or_right = input("You wake up dazed in a random alley... Unsure of where you are, which way will you go? Left or Right? ")
#Left option
if left_or_right == left_Choices:
ans = input("You stumble into a saloon nearly empty but some faces are there. Feel like having a drink at the bar? Or sitting at an empty table? (bar/table)? ")
if ans == "bar":
print("You sat at the bar and the bartender slides a glass across On the house partner! Nice! A free drink to help out. ( 10HP)")
health =10
elif ans == "table":
print("You sit at an empty table and a group on men approach and seat themselves So tough guy where's our 450Gold!?")
else:
print("You were too drunk to walk, fell down and passed out...")
我希望我的解釋至少有點可以理解,因為我對此很陌生,我不知道如何措辭這個問題。
X)
uj5u.com熱心網友回復:
可以通過不同的方式實作這一目標。我假設你想要一個while回圈。
一種方法是對串列使用 if/elif。
代碼:
right_Choices = ["right", "Right", "RIGHT"]
left_Choices = ["left", "Left", "LEFT"]
while True:
user_choice = input('choose direction: ')
if user_choice in right_Choices:
print('right')
break
elif user_choice in left_Choices:
print('left')
break
else:
print('choice not valid! Please choose again')
continue
輸出:
choose direction: no
choice not valid! Please choose again
choose direction: left
left
[Program finished]
您可以在匹配之前將輸入轉換為小寫或大寫并跳過串列。
像這樣:
while True:
user_choice = input('choose direction: ')
if user_choice.lower() == "right":
print('right')
break
elif user_choice.lower() == "left":
print('left')
break
else:
print('choice not valid please choose agaim')
continue
輸出:
choose direction: LEFT
left
[Program finished]
或者制作一個全部小寫的方向串列,并檢查 user_choice.lower() 是否在串列中。
代碼:
directions = ['left', 'right', 'forward', 'back']
while True:
user_choice = input('choose direction: ')
if user_choice.lower() in directions:
print(user_choice.lower())
break
else:
print('choice not valid please choose agaim')
continue
輸出:
choose direction: To space
choice not valid! Please choose again
choose direction: RIght
right
[Program finished]
uj5u.com熱心網友回復:
你應該讓你的測驗集理論化。例如:
如果在 Set_of_Left_Turns 中選擇或在 Set_of_Right_Turns 中選擇: print('first branch') else: print('second branch')
此外,如果您將選擇變數限制為僅大寫答案,然后使用choice.upper() 將已映射為大寫的輸入與一組大寫選項進行比較,則會簡化問題。
更一般地,使用想法(例如奇異值分解),您可以使用演算法將文本短語映射到幾個關鍵名詞,然后根據足夠的統計資料而不是原始文本做出決定。即你可以使用降維的想法。
更一般地說,模糊邏輯的相關之處在于您可以“去模糊化”輸入,計算這個結果的函式,然后模糊化輸出。這個概念也與深度學習/神經網路中自動編碼器的作業有關。這些都是主成分分析中的降維原理。
我建議在您的 RPG 中將一組操作設為靜態(或明確地給出),以便您的用戶知道候選選擇是什么。看看例如 Zork 或 Ultima 5 以獲得靈感。
最后一個想法,研究一下 Python 中 switch 陳述句的可能模擬:
case/switch 陳述句的 Python 等效項是什么?
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/480487.html
