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

时间:2018-02-15 14:53:27

标签: python-3.x while-loop accumulator

我无法弄清楚我做错了什么。我尝试过使用休息,并尝试设置变量!=,我在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))

3 个答案:

答案 0 :(得分:0)

您的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

答案 1 :(得分:0)

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

""" 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退出

答案 2 :(得分:0)

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

在while循环中甚至没有将第一个字符串用作longString = max(n)

max也返回最大值,因此在这种情况下,它只是将文本转换为ascii数字。

相反,您应该使用len(string)来返回字符串的长度。

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")