我可以使用for循环获得相同的结果吗?

时间:2018-11-16 12:37:36

标签: python while-loop boolean

这段代码可以正常工作,但是我想知道是否可以使用for循环获得相同的功能?

import pymongo
from pymongo import MongoClient
from gridfs import GridFS
from bson import objectid
import gridfs
from motor.motor_asyncio import AsyncIOMotorGridFSBucket

client = MongoClient("localhost", 27017)
client = MongoClient().sampleDB2
fs = AsyncIOMotorGridFSBucket(client)
print(fs)
file_id = fs.upload_from_stream("2018-10-12.pdf", "data I want to store!")
file = open('2018-10-12.pdf', 'wb+')
fs.download_to_stream(file_id, file)
file.seek(0)
contents = file.read()
#print(contents)

1 个答案:

答案 0 :(得分:0)

对于此代码,您不能将输入用作变量,所以我将其重写为:

   choice = input("") #This will get a string that the user types.        

   if "stay" in choice: #This will execute the code below if the user has typed something with stay in it
        print dead("You were struck by lightning !")

    elif "wait" in choice and not(storm): #this will execute the code below if the user has typed something with the word wait in it and there is no storm(storm=False)
        print "The storm has stpped !"
        storm_halted=True

    elif "wait" in input and storm: #this will execute the code below if the user has typed something with the word wait in it and there is a storm (storm=True)
        print dead("You are lazy")
    elif "room" in input and storm : #this will execute the code below if the user has typed something with the word room in it and there is a storm (storm=True)
        gold ()
    else: #This code will execute if none of the above happen
        print dead("Type something")

至于storm_halted = True,对此的英文是:风暴已停止。尽管我认为您是无意中将not(风暴)和风暴颠倒了-从等效意义上判断。

希望这可以帮助您理解。确实没有太多其他方法可以实现这一目标,并且绝对没有更简单的方法可以实现这一目标。