日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python中如果要多次输入文本,关于文本游戏:文本游戏 – 如果语句基于输入文本 – Python...

發(fā)布時(shí)間:2025/4/5 python 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中如果要多次输入文本,关于文本游戏:文本游戏 – 如果语句基于输入文本 – Python... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

所以,我用python制作了一個(gè)基于文本的益智游戲,用于我的編程類(我們被迫使用python),所以我希望程序能夠檢測(cè)用戶是否輸入了"猶豫"或"行走"之類的內(nèi)容,而不是讓用戶說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還沒有包含在代碼中)。我希望它是這樣的;

if input ="Hesitate"

print("You Hesitate")

else:

if input ="Walk"

print("You walk forward")

else:

print("Invalid Input")

我不知道如何在Python中正確地完成這項(xiàng)工作。我真的到處找過。

這是使用控制臺(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è)更有效的解決方案可以通過使用make shift switch語句和一個(gè)字典找到,這里將討論一個(gè)例子?;蛘?#xff0c;您可以在循環(huán)語句中使用更具體的條件。

我試過你的建議,但一直返回語法錯(cuò)誤

@gamewylder,抱歉,它的格式不是完全可以直接復(fù)制的,我添加了":",現(xiàn)在應(yīng)該可以解決這個(gè)問題了。

謝謝,這次成功了。

基于另一個(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ì)有所幫助。在幾種常見的編程語言中,您將使用"="為變量賦值。在大多數(shù)相同語言中,您將使用"=="來確定變量或常量的相等性。

目前我的輸入設(shè)置為:choice=str(input())這能工作嗎?你放的好像是我需要的。

@Gamewylder從一個(gè)python教程中,我想你會(huì)想要一些沿著以下幾行的東西:choice=input("你做什么?")此外,99中場(chǎng)的回答顯示了一個(gè)很好的方法來接受輸入。在許多場(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ǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。