【问题标题】:Don't understand the true/false parts of this code. How can my inputs in this code (all integers) "be true" or "be false"?不理解这段代码的真假部分。我在此代码中的输入(所有整数)如何“为真”或“为假”?
【发布时间】:2020-03-08 22:34:18
【问题描述】:
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 感到困惑,设置是什么意思 一个等于真或假的变量?也对 while s != "": 的含义感到困惑。对不起, 我是一个完整的初学者,如果有人可以帮助我,我将永远感激不尽

【问题讨论】:

  • 这应该是什么语言?肯定不是Java,那你为什么把它标记为Java?
  • 这些都是非常基本的问题,并且有一个地方可以回答这些问题:Python Tutorial
  • 这看起来像 python,所以我冒昧地猜测你想知道带有布尔值的 if 语句的语法:stackoverflow.com/questions/38423360/…

标签: python


【解决方案1】:

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

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,这是真的,因为 1 与 2 相同。

【讨论】:

  • 这真的很有帮助,非常感谢。尽管我仍然对第 6 行和第 7 行感到困惑。您正在检查列表中的 x (如果我的不同输入)是否是第一个(是真还是假)。整数(假设我输入 3、4、47、62 等)如何是真或假?我知道 for 函数是一个循环,所以它会遍历每一个输入(lst 中的 x)——并检查它是否是“第一个”。如果我的整数是“第一”,究竟是什么意思,我在 lst 中的 x 是“真”或“假”是什么意思?
  • 很抱歉,如果这令人困惑!
  • 还是因为你在 first=true 之前设置它而自动“first”(又名 true),而一旦你设置 first=false,它就不再是这种情况了?
  • @DianaA for x in lst 就是你所说的 for 循环。它遍历下面的代码并为每个循环计数 1,直到x 达到in 之后的列表大小的值。例如,如果我说for x in range(0,5),下面的代码会执行 5 次。希望这会有所帮助。
  • @THess " 它遍历下面的代码并为每个循环计数 1,直到 x 达到 in 之后的列表大小的值。"它不是。它遍历列表对象中的每个 value,而不是 0 -> len(list_object)。 Python for 循环类似于基于迭代器的 Java for-each 循环。
【解决方案2】:
first = True                 

将布尔变量设置为True

while s != "":

检查输入是否为空。 != 表示不相等,与 == 相反,表示相等

if first:

如果first 变量是True,则运行 if 语句的代码。在这种情况下,将值设置为您的最大值并

first = False

first 设置为False,因为下一个值不再是第一个值

【讨论】:

    猜你喜欢
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多