首页 > 解决方案 > 不理解这段代码的真假部分。我在此代码中的输入(所有整数)如何“为真”或“为假”?

问题描述

print("Please enter integers (then press enter key twice to show you're done):")
s = input()                  #whatever you're inputting after the print
first = True                 #What does this mean???
while s != "":               #What does this mean???
    lst = s.split()          #split all your inputs into a list
    for x in lst:
        if first:                            #If its in ur lst?
            maxV = int(x)           #then the max value will be that input as an integer
            first = False                #What does this mean?
        else:
            if maxV < int(x):
                maxV = int(x)
    s= input()
print(maxV)

我对这段代码中的 first=True 和 first=False 感到困惑,将变量设置为 true 或 false 是什么意思?也对 while s != "": 的含义感到困惑。对不起,我是一个完整的初学者,如果有人可以帮助我,将永远感激不尽

标签: python

解决方案


我真的不知道这是什么编程语言,但通过基本知识我可以告诉你这些东西的含义。我希望它有帮助:

print("Please enter integers (then press enter key twice to show you're done):")
s = input()                  #Here s becomes your input
first = True                 #Here you set first as a boolean which can have the state true or false. In this example it gets the value True assigned
while s != "":               #While repeats a certain process and in this example it keeps this process going while s isn't empty
    lst = s.split()          #splits all your inputs into a list <- you got that right
    for x in lst:
        if first:                        #It checks if first is true. If it is true it keeps going with the code right after the if
            maxV = int(x)                #then the max value will be that input as an integer
            first = False                #this sets a new value to first. which is false in this case
        else:
            if maxV < int(x):
                maxV = int(x)
    s= input()
print(maxV)

另外你说你不明白!=. !=是一样==的,但恰恰相反。意思是不平等。因此,如果你说这样的话1 == 1是真的,因为 1 等于 1。如果你说1 != 2这是真的,因为 12 不同。


推荐阅读