簡體   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