turtlemodule的重复次数不会超过两次,并且不会运行。Python3.6

2024-10-02 12:32:20 发布

您现在位置:Python中文网/ 问答频道 /正文

import turtle as t
import random as r
import time,os
global turtlecount

turtlelist=["sam","matt","aaa","bbb"]
turtlelist2=["sam","matt","song","thunder"]         #거북이 이름
turtlecolor=["red","blue","green","black"]    #거북이 색깔
goal=[]#들어온 거북이추가할 리스트
abc=[1000]

class turtlegame: #거북이 클래스
    def __init__(self):
        self.batting() #배팅, 거북이선택
        self.fencing() #경기장
        self.tutlesetting() # 거북이 출발선
        self.turtle_move() #거북이 움직임
        self.account()

        self.bye() #경기 종료
        input()

    def batting(self):
        self.wallet=abc[0]
        self.money = 0
        print(self.wallet)
        print(turtlelist2)
        global playerturtle
        playerturtle=input("Choose a turtle to go batting.")
        self.money=int(input("betting money :"))
        while self.money > self.wallet:
            print("have no money in your wallet")
            self.money=int(input("betting money"))

    def fencing(self): #경기장설정
        wn=t.Screen()
        wn.bgcolor("White") #터틀모듈 화면 설정
        t.tracer(2)
        fence=t.Turtle()
        goalline=t.Turtle()
        fence.penup()
        goalline.penup()
        fence.setposition(-300,-100)
        goalline.setposition(280,200)
        fence.pendown()
        goalline.pendown()
        fence.pensize(3)
        goalline.pensize(3)
        for i in range(2):
            fence.forward(600)
            fence.left(90)
            fence.forward(300)
            fence.left(90)
            goalline.rt(90)
            goalline.fd(300)
            fence.ht()
            goalline.ht()
    def tutlesetting(self): #거북이 처음 셋팅
            for i in range(turtlecount):
                turtlelist[i]=t.Turtle()
                turtlelist[i].shape("turtle")
                turtlelist[i].color(turtlecolor[i])
                turtlelist[i].penup()
                turtlelist[i].setposition(-280,140-(i*60))



    def turtle_move(self):
        raceend=0
        while raceend!=1:
            for i in range(turtlecount): #골인 시 리스트 인덱스 리스트 2에서 받은 후 변환 후 값을 리턴
                if turtlelist[i].xcor()>270:
                    raceend+=1
                    turtlelist[i]=turtlelist2[i]
                    time.sleep(1)
                    return goal.append(turtlelist[i])

                #울타리 닿이면 돌아가도록 설정
                if turtlelist[i].xcor() < -295:
                    turtlelist[i].rt(180)
                    turtlelist[i].fd(15)
                if turtlelist[i].ycor()<-95:
                    turtlelist[i].rt(180)
                    turtlelist[i].fd(15)
                if turtlelist[i].ycor()>195:
                    turtlelist[i].rt(180)
                    turtlelist[i].fd(15)
                #거북이 움직임
                turtlelist[i].setheading(0)
                if r.randint(1,2) % 2==0:
                    turtlelist[r.randint(0,int(turtlecount)-1)].rt(r.randint(20,110))
                else:
                    turtlelist[r.randint(0,int(turtlecount)-1)].lt(r.randint(20,110))
                turtlelist[i].fd(r.randint(10,20))
    def account(self):
        if goal[0]==playerturtle:
            self.wallet+=(self.money*turtlecount)
            print("%s turtle win %s get money."%(goal[0],(self.money*turtlecount)))
            print(self.wallet)
            abc[0]=self.wallet
        else:
            print("%s turtle win. to lose moeny"%(goal[0]))
            self.wallet -= self.money
            print(self.wallet)
            abc[0]=self.wallet
            if self.wallet<0:
                print("have no money in your wallet.")

    def bye(self): #승리 거북이 텍스트로 알린 뒤 터틀 모듈 나오기
        tt=t.Turtle()
        tt.ht()
        t.clearscreen()
        t.reset()
        t.write("★%s turtle win .★"%(goal[0]),False,"center",("",20))
        time.sleep(2)
        t.bye()

        print(goal)



while True:

    print(" ")
    print("                    Turtlerace\n")
    print("""===========================================================
                    1.start


                    2. exit
===========================================================""")

    start=int(input("Button : "))
    os.system("cls")
    if start == 1 :
        turtlecount=int(input("how many?(2 ~ 4)")) #처음 거북이 수 결정

        if turtlecount <=1 or turtlecount> 4:
            print("please 1~4")
            continue # 5
        turtlegame() #class
        print(goal[0])
        os.system("pause")
        os.system("cls")
        turtlelist=["sam","matt","aaa","bbb"]
        turtlelist2=["sam","matt","song","thunder"]
        money=0
        start=0
        goal=[]
        continue
    elif start==2:
        exit()

执行上述代码时,将执行第一个“while true”类

但是第二个turtlemodul没有运行

我想知道为什么会发生错误


Tags: selfinputifdefintwalletprintrandint
1条回答
网友
1楼 · 发布于 2024-10-02 12:32:20

我修正了代码,使一件事更加清晰,现在它可以工作了

import turtle as t
import random as r
import time,os
global turtlecount

turtlelist=["sam","matt","aaa","bbb"]
turtlelist2=["sam","matt","song","thunder"]         #거북이 이름
turtlecolor=["red","blue","green","black"]    #거북이 색깔
goal=[]#들어온 거북이추가할 리스트
abc=[1000]

class turtlegame: #거북이 클래스
    def __init__(self):
        self.batting() #배팅, 거북이선택
        self.fencing() #경기장
        self.tutlesetting() # 거북이 출발선
        self.turtle_move() #거북이 움직임
        self.account()

        self.bye() #경기 종료
        input('[press enter to go back to main menu]')

    def batting(self):
        self.wallet=abc[0]
        self.money = 0
        print(self.wallet)
        print(turtlelist2)
        global playerturtle
        playerturtle=input("Choose a turtle to go batting.")
        self.money=int(input("betting money :"))
        while self.money > self.wallet:
            print("have no money in your wallet")
            self.money=int(input("betting money"))

    def fencing(self): #경기장설정
        wn=t.Screen()
        wn.bgcolor("White") #터틀모듈 화면 설정
        t.tracer(2)
        fence=t.Turtle()
        goalline=t.Turtle()
        fence.penup()
        goalline.penup()
        fence.setposition(-300,-100)
        goalline.setposition(280,200)
        fence.pendown()
        goalline.pendown()
        fence.pensize(3)
        goalline.pensize(3)
        for i in range(2):
            fence.forward(600)
            fence.left(90)
            fence.forward(300)
            fence.left(90)
            goalline.rt(90)
            goalline.fd(300)
            fence.ht()
            goalline.ht()
    def tutlesetting(self): #거북이 처음 셋팅
            for i in range(turtlecount):
                turtlelist[i]=t.Turtle()
                turtlelist[i].shape("turtle")
                turtlelist[i].color(turtlecolor[i])
                turtlelist[i].penup()
                turtlelist[i].setposition(-280,140-(i*60))



    def turtle_move(self):
        raceend=0
        while raceend!=1:
            for i in range(turtlecount): #골인 시 리스트 인덱스 리스트 2에서 받은 후 변환 후 값을 리턴
                if turtlelist[i].xcor()>270:
                    raceend+=1
                    turtlelist[i]=turtlelist2[i]
                    time.sleep(1)
                    return goal.append(turtlelist[i])

                #울타리 닿이면 돌아가도록 설정
                if turtlelist[i].xcor() < -295:
                    turtlelist[i].rt(180)
                    turtlelist[i].fd(15)
                if turtlelist[i].ycor()<-95:
                    turtlelist[i].rt(180)
                    turtlelist[i].fd(15)
                if turtlelist[i].ycor()>195:
                    turtlelist[i].rt(180)
                    turtlelist[i].fd(15)
                #거북이 움직임
                turtlelist[i].setheading(0)
                if r.randint(1,2) % 2==0:
                    turtlelist[r.randint(0,int(turtlecount)-1)].rt(r.randint(20,110))
                else:
                    turtlelist[r.randint(0,int(turtlecount)-1)].lt(r.randint(20,110))
                turtlelist[i].fd(r.randint(10,20))
    def account(self):
        if goal[0]==playerturtle:
            self.wallet+=(self.money*turtlecount)
            print("%s turtle win %s get money."%(goal[0],(self.money*turtlecount)))
            print(self.wallet)
            abc[0]=self.wallet
        else:
            print("%s turtle win. to lose moeny"%(goal[0]))
            self.wallet -= self.money
            print(self.wallet)
            abc[0]=self.wallet
            if self.wallet<0:
                print("have no money in your wallet.")

    def bye(self): #승리 거북이 텍스트로 알린 뒤 터틀 모듈 나오기
        tt=t.Turtle()
        tt.ht()
        t.clearscreen()
        t.reset()
        t.write("★%s turtle win .★"%(goal[0]),False,"center",("",20))
        time.sleep(2)
        t.bye()

        print(goal)



while True:

    print(" ")
    print("                    Turtlerace\n")
    print("""===========================================================
                    1.start


                    2. exit
===========================================================""")

    start=int(input("Button : "))
    os.system("cls")
    if start == 1 :
        turtlecount=int(input("how many?(2 ~ 4)")) #처음 거북이 수 결정

        if turtlecount <=1 or turtlecount> 4:
            print("please 1~4")
            continue # 5
        turtlegame() #class
        print(goal[0])
        #os.system("pause")
        #os.system("cls")
        turtlelist=["sam","matt","aaa","bbb"]
        turtlelist2=["sam","matt","song","thunder"]
        money=0
        start=0
        goal=[]
        continue
    elif start==2:
        exit()

相关问题 更多 >

    热门问题