当用户输入高于 7 时,我可以告诉我的乌龟什么都不做吗?

Can i tell my turtle to NOT do anything when the user input is higher than 7?

我在python turtle 中编写了connect 4 的代码,并列出了所有列。有 7 列,但如果您输入“8”,游戏将以错误结束。我可以以某种方式改变它,它不会给你一个错误,而是什么都不做,你可以继续玩吗?因此,例如,如果您错误地输入了 8 或 9,它不会完全停止游戏吗?这是我的代码:

import turtle

pen = turtle.Turtle()
screen = turtle.Screen()
screen.setup(900,900)

screen.title("Vier op een rij")
screen.tracer(0,0)

fontText = ("Arial", 16)

pen.ht()
pen.penup()
pen.speed(0)

def board():
    pen.color("blue")
    pen.fillcolor("blue")
    pen.goto(-350,-400)
    pen.pendown()
    pen.begin_fill()
    pen.goto(350,-400)
    pen.goto(350,200)
    pen.goto(-350,200)
    pen.goto(-350,-400)
    pen.end_fill()
    pen.penup()

def circles(x,y):
    pen.goto(x,y)
    pen.color("white")
    pen.fillcolor("white")
    pen.begin_fill()
    for i in range(7):
        pen.pendown()
        pen.circle(25)
        pen.penup()
        pen.forward(100)
    pen.end_fill()
    pen.penup()
    
def number(x,y,text):
    pen.color("black")
    pen.goto(x,y)
    pen.write(text, font=fontText, align="center")

def drawCircles():
    circles(-300, -370)
    circles(-300,-270)
    circles(-300,-170)
    circles(-300,-70)
    circles(-300,30)
    circles(-300,130)

def numbers():
    number(-300,220,"Kolom 1")
    number(-200,220,"Kolom 2")
    number(-100,220,"Kolom 3")
    number(0,220,"Kolom 4")
    number(100,220,"Kolom 5")
    number(200,220,"Kolom 6")
    number(300,220,"Kolom 7")


numbers()           
board()
drawCircles()
screen.update()

def Circle(x,y,color):
    pen.penup()
    pen.color(color)
    pen.fillcolor(color)
    pen.goto(x,y)
    pen.pendown()
    pen.begin_fill()
    pen.circle(25)
    pen.end_fill()
    pen.penup()
    
y = -370
kolomGevuld = [0,0,0,0,0,0,0]
rijGevuld = [y,y,y,y,y,y,y]
space = 100

while True:
    red = int(input("In welke kolom wil je een rode schijf plaatsen? "))
    kolomGevuld[red-1] += 1
    if kolomGevuld[red-1] < 7:
        Circle(-300 + (red-1) * space, rijGevuld[red-1],"red")
    rijGevuld[red-1] += space
    screen.update()
    
    yellow = int(input("In welke kolom wil je een gele schijf plaatsen? "))
    kolomGevuld[yellow-1] += 1
    if kolomGevuld[yellow-1] < 7:
        Circle(-300 + (yellow-1) * space, rijGevuld[yellow-1],"yellow")
    rijGevuld[yellow-1] += space
    screen.update()
    

turtle.done()

检查数组索引是否在获取输入后立即超出范围,如果索引超出范围,则继续 while 循环的下一次迭代。我已经评论了我添加到您的代码中的代码行

while True:
    red = int(input("In welke kolom wil je een rode schijf plaatsen? "))
    # check if red is out of range or not and if yes, continue
    if red < 1 or red > 7:
        continue
    kolomGevuld[red-1] += 1
    if kolomGevuld[red-1] < 7:
        Circle(-300 + (red-1) * space, rijGevuld[red-1], "red")
    rijGevuld[red-1] += space
    screen.update()

    yellow = int(input("In welke kolom wil je een gele schijf plaatsen? "))
    # check if yellow is out of range or not and if yes, continue
    if yellow < 1 or yellow > 7:
        continue
    kolomGevuld[yellow-1] += 1
    if kolomGevuld[yellow-1] < 7:
        Circle(-300 + (yellow-1) * space, rijGevuld[yellow-1], "yellow")
    rijGevuld[yellow-1] += space
    screen.update()