首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我的def变量之一卡在相同的值上。

我的def变量之一卡在相同的值上。
EN

Stack Overflow用户
提问于 2020-07-08 03:54:19
回答 1查看 23关注 0票数 0

我对python和这个网站很陌生*我很难理解为什么我从我的Car.name函数中得到了相同的值

代码语言:javascript
运行
AI代码解释
复制
class Car:
    def __init__(self, name, price):
        Car.name = name
        Car.price = price

我在课堂上存储了一些汽车数据。

代码语言:javascript
运行
AI代码解释
复制
Lamborghini_Centenario = Car("Lamborghini Centenario", 2500000)
Rolls_Royce_Sweptail = Car("Roll's-Royce Sweptail", 12800000)
McLarenP1LM = Car("McLaren P1 LM", 3600000)
Koenigsigg = Car("Koenigsigg CCXR Trevita", 4800000)
Lykan_Hypersport = Car("Lykan Hypersport", 3400000)
Lamborghini_Veneno_Roadster = Car("Lamborghini Veneno Roadster", 4500000)
Aston_Martin_Valkyrie = Car("Aston Martin Valkyrie", 3200000)
Bugatti_Chiron = Car("Bugatti Chiron", 2700000)
Pagani_Huayra_BC = Car("Pagani Huayra BC", 2800000)
Ferrari_Sergio = Car("Ferrari Pininfarina\n      Sergio", 3000000) <--.
                                                                      |
#But when I try to get the name of a random car                       |
#Here is the code I used vvv (Carlist is not shown here)              |                    
#b = random.choice(carlist)                                           | ignore arrow not part of 
#b.name                                                               | code(sorry couldn't come
#it only gets the name from the bottom of the car data area.  --------+ up with a name)
#I have switched it up and have only been getting the bottom value
#it might be due to the turtle module .mainloop() function

这是整个剧本,谢谢

代码语言:javascript
运行
AI代码解释
复制
import random
import turtle
import os
import time

class Car:
    def __init__(self, name, price):
        Car.name = name
        Car.price = price

higher_true = 0
lower_true = 0
lives1 = 1
score = 0

Lamborghini_Centenario = Car("Lamborghini Centenario", 2500000)
Rolls_Royce_Sweptail = Car("Roll's-Royce Sweptail", 12800000)
McLarenP1LM = Car("McLaren P1 LM", 3600000)
Koenigsigg = Car("Koenigsigg CCXR Trevita", 4800000)
Lykan_Hypersport = Car("Lykan Hypersport", 3400000)
Lamborghini_Veneno_Roadster = Car("Lamborghini Veneno Roadster", 4500000)
Aston_Martin_Valkyrie = Car("Aston Martin Valkyrie", 3200000)
Bugatti_Chiron = Car("Bugatti Chiron", 2700000)
Pagani_Huayra_BC = Car("Pagani Huayra BC", 2800000)
Ferrari_Sergio = Car("Ferrari Pininfarina\n      Sergio", 3000000)

carlist = [Lamborghini_Centenario, Rolls_Royce_Sweptail, McLarenP1LM, Koenigsigg, Lykan_Hypersport, Lamborghini_Veneno_Roadster, Aston_Martin_Valkyrie, Bugatti_Chiron, Ferrari_Sergio, Pagani_Huayra_BC]

wn = turtle.Screen()
wn.screensize(1000, 500)
wn.title("Higher or Lower: Car Price Edition")
wn.setup(1050, 650, 0, 0)

Higher = turtle.Turtle()
Higher.penup()
style = ('Courier', 55, 'italic')
Higher.color('black')
Higher.goto(130, -300)
Higher.write('Higher', font=style, align='center')
Higher.hideturtle()

Or = turtle.Turtle()
Or.penup()
Or.color('black')
Or.goto(275, -300)
Or.write('or', font=style, align='center')
Or.hideturtle()

correct = turtle.Turtle()
correct.penup()
correct.color('green')
correct.hideturtle()

Lower = turtle.Turtle()
Lower.penup()
Lower.color('black')

Lower.goto(406, -300)
Lower.write('Lower', font=style, align='center')
Lower.hideturtle()

line = turtle.Turtle()
line.goto(0, 0)
line.shape('square')
line.shapesize(500, 0.5)
line.color('black')

Lives = turtle.Turtle()
Lives.penup()
Lives.color('black')
style = ('Courier', 60, 'italic')
Lives.goto(158, 260)
Lives.write('Lives:', font=style, align='center')
Lives.hideturtle()

Scores = turtle.Turtle()
Scores.penup()
Scores.color('black')
Scores.goto(-258, 260)
Scores.write('Score:', font=style, align='center')
Scores.hideturtle()

wn.addshape(os.path.expanduser("~/Downloads/Lamborghini_Centenario.gif"))
wn.addshape(os.path.expanduser("~/Downloads/RollsRoyceSweptail.gif"))
wn.addshape(os.path.expanduser("~/Downloads/McLaren-P1-LM.gif"))
wn.addshape(os.path.expanduser("~/Downloads/Koenigsegg-CCRX-Trevita.gif"))
wn.addshape(os.path.expanduser("~/Downloads/Lamborghini-Veneno-Roadster.gif"))
wn.addshape(os.path.expanduser("~/Downloads/Aston-martin-Valkyrie.gif"))
wn.addshape(os.path.expanduser("~/Downloads/Bugatti-Chiron.gif"))
wn.addshape(os.path.expanduser("~/Downloads/Pagani-Huayra-BC.gif"))
wn.addshape(os.path.expanduser("~/Downloads/Ferrari-Pininfarina-Sergio.gif"))
wn.addshape(os.path.expanduser("~/Downloads/Lykan-Hypersport.gif"))

BugattiChiron = turtle.Turtle()
BugattiChiron.penup()
BugattiChiron.shape(os.path.expanduser("~/Downloads/Bugatti-Chiron.gif"))
BugattiChiron.goto(-300, 0)

Carname = turtle.Turtle()
Carname.penup()
Carname.color('black')
Carname.goto(-300, -200)
style1 = ('Courier', 40, 'italic')
Carname.write("Bugatti Chiron", font=style1, align='center')
Carname.hideturtle()


def screenclick(x, y):
    print(x, y)
    if x in range(39, 227) and y in range(-290, -250):
        higher_true + 1


wn.listen()
wn.onscreenclick(screenclick)

a = Bugatti_Chiron
b = random.choice(carlist)
bname = turtle.Turtle()
bname.penup()
bname.goto(275, -200)
bname.write(b.name, font=style1, align='center')
# problem   ^^^^^^ is here(it's just giving the Car.name of Ferrari_Sergio)
while higher_true == 0:
    wn.update
    while higher_true == 1:
        if b >= a:
            score = score + 1
            correct.write('Correct!', font=style, align='center')
            time.sleep(1.5)
            correct.clear()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-08 03:59:13

您需要在对象上而不是在类上设置属性。

代码语言:javascript
运行
AI代码解释
复制
class Car:
    def __init__(self, name, price):
        self.name = name
        self.price = price

Car.name将在Car类上设置一个name属性。您可以直接从name类访问Car属性,而无需使用Car.name之类的对象。

每次更新Car.name类的name属性时都设置Car,每次创建Car对象时都会更新该属性。因此,它只保留最后一个值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62794230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档