【问题标题】:python accumulating while loop keeps repeating what did i do wrong?python累积while循环不断重复我做错了什么?
【发布时间】:2018-07-26 09:01:44
【问题描述】:

我不知道我做错了什么。我尝试过使用中断,并尝试设置变量!=,我在 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))

【问题讨论】:

  • Erica.. 请以正确的格式发布您的脚本代码;从每行代码的四个空格开始。代码在视图窗格中变为灰色。目前的信息根本没有多大意义。不是图片!!
  • 我尝试以图片格式发布。
  • 不鼓励使用图片代码。人们无法复制/粘贴并尝试重现您的问题。

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


【解决方案1】:

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

并且仅将第一个字符串作为longString = max(n) 甚至不在while循环中。

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

【讨论】:

    【解决方案2】:

    根据您的代码,您在进入循环后不会更改 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退出

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-21
        • 1970-01-01
        • 2014-03-24
        • 2021-02-15
        • 1970-01-01
        • 2011-11-23
        • 2012-10-01
        • 2017-07-29
        相关资源
        最近更新 更多