python中如果要多次输入文本,关于文本游戏:文本游戏 – 如果语句基于输入文本 – Python...
所以,我用python制作了一個(gè)基于文本的益智游戲,用于我的編程類(我們被迫使用python),所以我希望程序能夠檢測(cè)用戶是否輸入了"猶豫"或"行走"之類的內(nèi)容,而不是讓用戶說(shuō)1或2之類的簡(jiǎn)單內(nèi)容。
目前我已經(jīng)確定了用戶輸入中字符的數(shù)量,但是這使得他們幾乎可以輸入任何東西。
#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或猶豫,它就不會(huì)接受輸入(在當(dāng)前代碼中,walk還沒(méi)有包含在代碼中)。我希望它是這樣的;
if input ="Hesitate"
print("You Hesitate")
else:
if input ="Walk"
print("You walk forward")
else:
print("Invalid Input")
我不知道如何在Python中正確地完成這項(xiàng)工作。我真的到處找過(guò)。
這是使用控制臺(tái)嗎?
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
這不是最好的方式去做這件事,但它很容易達(dá)到我插入你的要求。
事實(shí)上,答案在于你如何做這個(gè)循環(huán)。
在這個(gè)簡(jiǎn)單的方法中,它會(huì)一直運(yùn)行,直到在得到有效的輸入之后中斷,一個(gè)更有效的解決方案可以通過(guò)使用make shift switch語(yǔ)句和一個(gè)字典找到,這里將討論一個(gè)例子。或者,您可以在循環(huán)語(yǔ)句中使用更具體的條件。
我試過(guò)你的建議,但一直返回語(yǔ)法錯(cuò)誤
@gamewylder,抱歉,它的格式不是完全可以直接復(fù)制的,我添加了":",現(xiàn)在應(yīng)該可以解決這個(gè)問(wèn)題了。
謝謝,這次成功了。
基于另一個(gè)stackoverflow線程,我傾向于相信您會(huì)想要這樣的東西:
if input =="Hesitate":
print("You Hesitate")
else:
if input =="Walk":
print("You walk forward")
else:
print("Invalid Input")
我假設(shè)你已經(jīng)得到了輸入值。另外,如果給出這個(gè)答案,您需要強(qiáng)制資本化到預(yù)定的形式(第一個(gè)字母的資本都保持在較低的水平)。這條線可能有幫助。
最后,回顧分配與平等之間的區(qū)別可能會(huì)有所幫助。在幾種常見(jiàn)的編程語(yǔ)言中,您將使用"="為變量賦值。在大多數(shù)相同語(yǔ)言中,您將使用"=="來(lái)確定變量或常量的相等性。
目前我的輸入設(shè)置為:choice=str(input())這能工作嗎?你放的好像是我需要的。
@Gamewylder從一個(gè)python教程中,我想你會(huì)想要一些沿著以下幾行的東西:choice=input("你做什么?")此外,99中場(chǎng)的回答顯示了一個(gè)很好的方法來(lái)接受輸入。在許多場(chǎng)景中,最佳實(shí)踐實(shí)際上是在等待輸入時(shí)執(zhí)行一個(gè)循環(huán),從而阻塞程序(如果計(jì)劃這樣做的話)。
對(duì)于已接受的答案,將所有提示文本放在input()函數(shù)中通常是不好的做法。另外,不要使用len()功能,這是不好的做法。你應(yīng)該做的是:
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í)行。在確定輸入值時(shí)不要檢查輸入的長(zhǎng)度,應(yīng)該循環(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
總結(jié)
以上是生活随笔為你收集整理的python中如果要多次输入文本,关于文本游戏:文本游戏 – 如果语句基于输入文本 – Python...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java map输出中括号,从地图检索数
- 下一篇: 失业补助金什么时候到账