python累积while循环不断重复我做错了什么吗? [英] python accumulating while loop keeps repeating what did i do wrong?

查看:114
本文介绍了python累积while循环不断重复我做错了什么吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道自己在做什么错。我试过使用中断,并尝试设置变量!=,我在进行接合时会这样做,而且非常精巧。

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


推荐答案

您的 input() while 循环之外,因此 leftOrRight 永不更改,永不进入 X ,因此它不会退出循环:

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

这篇关于python累积while循环不断重复我做错了什么吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆