-->

python accumulating while loop keeps repeating wha

2020-05-03 10:32发布

问题:

I can't figure out what I am doing wrong. I have tried using a break, and tried setting what the variable !=, I am doing this on cengage and it is very finnicky.

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

回答1:

your input() is outside the while loop, so leftOrRight never changes, never get to X so it will not exit the loop:

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


回答2:

According to your code you are not changing the value for leftOrRight after entering the loop so the condition for your while loop is never false I would suggest the following edit:

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

so that you get a prompt every time the loop is executed and you can click X to exit



回答3:

Your program just got the character that had the largest id in the ascii table.

And only doing the first string as the longString = max(n) wasn't even in the while loop.

also max returns the largest value, so in this case it was just converting the text into an ascii number.

instead you should use len(string) which returns the length of the string.

Unlike max() which is used like:

11 == max(11,10,1,2) as 11 is the largest character.

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