python中如果要多次输入文本,关于文本游戏:文本游戏 – 如果语句基于输入文本 – Python...
所以,我用python制作了一個基于文本的益智游戲,用于我的編程類(我們被迫使用python),所以我希望程序能夠檢測用戶是否輸入了"猶豫"或"行走"之類的內容,而不是讓用戶說1或2之類的簡單內容。
目前我已經確定了用戶輸入中字符的數量,但是這使得他們幾乎可以輸入任何東西。
#Choice Number1
def introchoice():
print("Do you 'Hesitate? or do you 'Walk forward")
def Hesitate():
print()
print("You hesistate, startled by the sudden illumination of the room. Focusing on the old man who has his back turned to you. He gestures for you to come closer.
''Come in, Come in, don't be frightened. I'm but a frail old man'' he says.")
print()
#
def Walk():
print()
print("DEFAULT")
print()
#Currently Determines Input
InputVar = 5
#User Input
Choice = str(input())
#Checks length
if len(Choice) >= InputVar:
Hesitate()
else:
if len(Choice) <= InputVar:
Walk()
else:
print("Invalid Input")
#Intro Choice Def end
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#Clean Up
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
我如何才能更改它,這樣,如果輸入不是walk或猶豫,它就不會接受輸入(在當前代碼中,walk還沒有包含在代碼中)。我希望它是這樣的;
if input ="Hesitate"
print("You Hesitate")
else:
if input ="Walk"
print("You walk forward")
else:
print("Invalid Input")
我不知道如何在Python中正確地完成這項工作。我真的到處找過。
這是使用控制臺嗎?
while True:
input = input("What do you do")
if input =="Choice1":
print("Consequence1")
break
if input =="Choice2":
print("Consequence2")
break
if input =="Choice3":
print("Consequence3")
break
這不是最好的方式去做這件事,但它很容易達到我插入你的要求。
事實上,答案在于你如何做這個循環(huán)。
在這個簡單的方法中,它會一直運行,直到在得到有效的輸入之后中斷,一個更有效的解決方案可以通過使用make shift switch語句和一個字典找到,這里將討論一個例子。或者,您可以在循環(huán)語句中使用更具體的條件。
我試過你的建議,但一直返回語法錯誤
@gamewylder,抱歉,它的格式不是完全可以直接復制的,我添加了":",現(xiàn)在應該可以解決這個問題了。
謝謝,這次成功了。
基于另一個stackoverflow線程,我傾向于相信您會想要這樣的東西:
if input =="Hesitate":
print("You Hesitate")
else:
if input =="Walk":
print("You walk forward")
else:
print("Invalid Input")
我假設你已經得到了輸入值。另外,如果給出這個答案,您需要強制資本化到預定的形式(第一個字母的資本都保持在較低的水平)。這條線可能有幫助。
最后,回顧分配與平等之間的區(qū)別可能會有所幫助。在幾種常見的編程語言中,您將使用"="為變量賦值。在大多數相同語言中,您將使用"=="來確定變量或常量的相等性。
目前我的輸入設置為:choice=str(input())這能工作嗎?你放的好像是我需要的。
@Gamewylder從一個python教程中,我想你會想要一些沿著以下幾行的東西:choice=input("你做什么?")此外,99中場的回答顯示了一個很好的方法來接受輸入。在許多場景中,最佳實踐實際上是在等待輸入時執(zhí)行一個循環(huán),從而阻塞程序(如果計劃這樣做的話)。
對于已接受的答案,將所有提示文本放在input()函數中通常是不好的做法。另外,不要使用len()功能,這是不好的做法。你應該做的是:
def introchoice():
#don't bother using three print statements every time.
def printb(texttoprint):
print()
print(texttoprint)
print()
def Hesitate():
printb('You hesistate, startled by the sudden illumination of the room. Focusing on the old man who has his back turned to you. He gestures for you to come closer.
''Come in, Come in, don't be frightened. I'm but a frail old man'' he says.')
def Walk():
printb('Default')
input=null
print('Do you Hesitate or Walk')
input=input('')
#Python3 is case sensitive, and you want to make sure to accept all cases
while input.upper()!='HESITATE' and input.upper()!='WALK':
print('Invalid input')
input=input('')
此代碼將按您的要求執(zhí)行。在確定輸入值時不要檢查輸入的長度,應該循環(huán)直到得到有效的值。
您希望獲取用戶輸入,然后使用while循環(huán)重新定義用戶輸入變量,直到獲得有效的輸入。所以像這樣:
var = raw_input("Enter Text:")
while (var !="X" and var !="Y"):
var = raw_input("Pick a different option:")
edit:raw_input()已更改為input()python 3.x
總結
以上是生活随笔為你收集整理的python中如果要多次输入文本,关于文本游戏:文本游戏 – 如果语句基于输入文本 – Python...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java map输出中括号,从地图检索数
- 下一篇: 失业补助金什么时候到账