簡體   English   中英

如何將我的if語句轉換為while或for循環?

[英]How to turn my if statement into a while or for loop?

因此,我要制作的程序是用戶可以輸入要正確繼續的路徑。 我以為我會做出一個簡單的提示來告訴用戶它是否是有效路徑。

這是我的提示代碼:

if Path.find("Global") == -1:
    continue
else:
    print "Not a valid path."

當然,我不能在其中使用continue,但我只是不知道如何使此提示成為循環。 這樣的想法是,如果路徑包含單詞“ Global”,則程序將繼續執行另一項操作;如果不包含單詞,它將告訴用戶一條消息,並告知程序停止(中斷)。

def get_path():
    Path = raw_input("Please select the correct path: ")
    if Path.find("Global") == -1:
        # "Tell the user a message and tell the program to stop".
        print('{0} is not a correct path.'.format(Path))
        return
    # "the program continues with another action."
    print('{0} is a correct path.'.format(Path))

除非輸入匹配條件,否則可以中斷循環,如下所示:

while True:
    if 'Global' in raw_input('Enter a valid path: '):
        continue
    else:
        print 'Not a valid path.'
        break

嘗試這個:

Path = raw_input("Please enter a valid Path")
while Path.find("Global") == -1:
  print "This is not a valid Path."
  Path = raw_input("Please enter a valid Path")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM