繁体   English   中英

Python 跳过 while 循环

[英]Python skips while-loop

我试着做滴答作响。 我实现了一个while循环来拦截无效输入。 但是无论输入如何,程序都会继续执行程序。

def coordinates(A,B,C):
    
    while True:                                      #while-Schleife to intercept false input
        Reihe = input("Welche Reihe(Buchstabe)")
        Spalte = int(input("Welche Spalte(Zahl)"))
        
        if isinstance(Reihe,str):
            if isinstance(Spalte,int): 
                if Spalte>0 and Spalte<4:
                    break                             #input ok = breakout of loop
                else:
                    print("Zahl zu groß")
                    continue
            else:
                print("Eine Zahl eingeben")
                continue
        else:
            print("Einen Buschstaben eigeben")    
            continue


    if Reihe.upper()== "A":              #Check Place in table and if not occupied by other player
        if A[Spalte -1] != "O":
                A[Spalte-1] = 'X'
        
    elif Reihe.upper()== "B":
        if B[Spalte -1] != "O":
                B[Spalte-1] = "X"
        
    elif Reihe.upper()== "C":
        if C[Spalte -1] != "O":
                C[Spalte-1] = "X"
        
    else:
        print("Nur A,B oder C")

    print(A)
    print(B)
    print(C)


A = ["","",""]
B = ["","",""]
C = ["","",""]

coordinates(A,B,C)

我的 output:

Welche Reihe(Buchstabe)1
Welche Spalte(Zahl)1
Nur A,B oder C
['', '', '']
['', '', '']
['', '', '']
  

“Reihe”显然不是一个字符串,但程序只是逃避了循环。

给定 Reihe 的示例输入“1”,值(“1”)是一个字符串,因此测试成功。

如果您能够使用诸如 Visual Studio Code 之类的工具,则可以在调试器中单步执行您的代码并向自己证明这一点。

你可以考虑:

Reihen = ["A","B","C"]
if Reihe in Reihen:
  ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM