Input while循环的结构

Input while循环的结构,input,while-loop,Input,While Loop,我试图在Python中使用while循环,在用户输入中有反斜杠字符时提供错误消息。该代码在输入分数并请求第二次输入时提供错误消息。当第二个输入的长度与原始输入的长度不同时,就会出现问题,我不知道如何解决这个问题,因为我对Python的知识有限。感谢您的帮助 size = getInput('Size(in): ') charcount = len(size) for i in range(0,charcount): if size[i] == '/': while size[i] == '

我试图在Python中使用while循环,在用户输入中有反斜杠字符时提供错误消息。该代码在输入分数并请求第二次输入时提供错误消息。当第二个输入的长度与原始输入的长度不同时,就会出现问题,我不知道如何解决这个问题,因为我对Python的知识有限。感谢您的帮助

size = getInput('Size(in): ')
charcount = len(size)
for i in range(0,charcount):
 if size[i] == '/':
  while size[i] == '/':
   getWarningReply('Please enter size as a decimal', 'OKAY')
   size = getInput('Size(in): ')
 elif size[i] == '.':
#Convert size input from string to list, then back to string because strings are immutable whereas lists are not
  sizechars = list(size)
  sizechars[i] = 'P'
  size = "".join(sizechars)

如果新的
size
的长度比原始长度
charcount
短,则很容易超出范围,因此这不是一个很好的方法

我绝对不是Python大师,但更好的方法是将整个过程包装在while循环中,而不是将while循环嵌套在for循环中:

not_decimal = True

while not_decimal:
    found_slash = False
    size = getInput('Size(in): ')
    charcount = len(size)

    for i in range(0, charcount):
        if size[i] == '/':
            print 'Please enter size as a decimal.'
            found_slash = True
            break
        elif size[i] == '.':
            #Convert size input from string to list, then back to string because strings are immutable whereas lists are not
            sizechars = list(size)
            sizechars[i] = 'P'
            size = "".join(sizechars)

    if not found_slash:
        not_decimal = False

这是什么语言?“语言”并没有告诉我们太多……并不是说得太苛刻,但“FOR”、“IF”和“WHILE”的混合似乎很奇怪,我认为逻辑是第一个问题,不管给定语言的WHILE语法是什么。我建议您回到开头,从那里开始,重新编写逻辑对不起,这是Python。是的,我知道它很混乱,但我已经有几年没有使用Python了,我正在努力复习它。我遇到的问题是,当用户提供第二个输入时,如果它的大小不同,就会出现错误。它成功了!非常感谢。大家都知道,我对语句/循环结构非常生疏。我真的很感激。如果可以的话,我会投赞成票,但上面说我需要15个声誉lol。