python 累积 while 循环不断重复我做错了什么?

python accumulating while loop keeps repeating what did i do wrong?

我不知道我做错了什么。我试过使用 break,并尝试设置变量 !=,我是在 cengage 上做的,这非常有趣。

""" LeftOrRight.py - This program calculates the total number of left-handed and right-handed students in a class. Input: L for left-handed; R for right handed; X to quit. Output: Prints the number of left-handed students and the number of right-handed students."""

rightTotal = 0 # Number of right-handed students. 
leftTotal = 0 # Number of left-handed students.

leftOrRight = input("Enter an L if you are left-handed,a R if you are right-handed or X to quit.") 
while leftOrRight != "X": 
    print (leftOrRight) 
    if leftOrRight == "L": 
        leftTotal = (leftTotal + 1) 
    elif leftOrRight == "R": 
        rightTotal = (rightTotal + 1) 
    else: 
        break

print("Number of left-handed students: " + str(leftTotal)) 
print("Number of right-handed students: " + str(rightTotal))

你的 input()while 循环之外,所以 leftOrRight 永远不会改变,永远不会到达 X 所以它不会退出循环:

leftOrRight = None
while leftOrRight != "X":
    leftOrRight = input("Enter an L if you are left-handed,a R if you are right-handed or X to quit.")
    print (leftOrRight)
    if leftOrRight == "L":
        leftTotal = (leftTotal + 1)
    elif leftOrRight == "R":
        rightTotal = (rightTotal + 1)
    else:
        break

根据您的代码,您在进入循环后不会更改 leftOrRight 的值,因此您的 while 循环的条件永远不会为假我建议进行以下编辑:

""" LeftOrRight.py - This program calculates the total number of left-handed and right-handed students in a class. Input: L for left-handed; R for right handed; X to quit. Output: Prints the number of left-handed students and the number of right-handed students."""

rightTotal = 0 # Number of right-handed students. 
leftTotal = 0 # Number of left-handed students.

leftOrRight = '' #anything random
while leftOrRight != "X":
    leftOrRight = input("Enter an L if you are left-handed,a R if you are right-handed or X to quit.") 
    print (leftOrRight) 
    if leftOrRight == "L": 
         leftTotal = (leftTotal + 1) 
    elif leftOrRight == "R": 
         rightTotal = (rightTotal + 1) 
    else: 
         break

print("Number of left-handed students: " + str(leftTotal)) 
print("Number of right-handed students: " + str(rightTotal))

这样每次执行循环都会有提示,点击X退出

您的程序刚刚获得了 ascii table 中具有最大 id 的字符。

并且只执行第一个字符串,因为 longString = max(n) 甚至不在 while 循环中。

也 max returns 最大值,所以在这种情况下它只是将文本转换为 ascii 数字。

相反,您应该使用 len(string),其中 returns 字符串的长度。

不像 max() 那样使用:

11 == max(11,10,1,2) 因为 11 是最大的字符。

n = (input("Input: ")) #get initial input

longest = 0 #define the varible which we will keep the length of the longest string in
longest_str = "" #define the varible which stores the value of the longest string.

while n: #you don't need that other stuff, while n does the same as while n != ''
    n = str(input("Input: ")) #get input
    length = len(n) #gets the length of the input
    if length > longest: #if the length of our input is larger than the last largest string
        longest = length #set the longest length to current string length
        longest_str = n #set the longest string to current string
#once the user hits enter (typing "") we exit the while loop, and this code runs afterwards.

print("Longest input was", longest_str, "at", longest, "characters long")