如何获取用户输入并将其传递给我的班级? [英] How do I get user input and pass it to my class?

查看:71
本文介绍了如何获取用户输入并将其传递给我的班级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法将输入值传递给我的班级



 class Tutu():
def __init __( self,name,color):
self.color = color
self.name = name

def introduction(self):
print(Hey {0}我是{1}。format(self.name,self.color))

zizi = Tutu(Joe,Potato)
zizi.introduce()


类Zuzu(图图):
def __init __(自我,土豆):
self.potato =土豆

def介绍(个体经营):
print(What is up {0}。格式(self.potato))

x = input()
pipi = Zuzu(x)
pipi.introduce ()





我尝试了什么:



我尝试在__init__和introduction方法中添加输入但是它不起作用。



错误:

 NameError:name 'd'未定义

解决方案

我不知道该错误消息来自哪里但是鳕鱼以上工作正常:



  1. zizi = Tutu(Joe,Potato)#zizi是Tutu类的对象,具有这些属性
  2. zizi.introduce()#displayHey Joe I am Potato
  3. x = input()#获取用户的输入,例如fred
  4. pipi = Zuzu(x)#pipi是Zuzu类的一个对象,具有该属性(fred)
  5. pipi.introduce()#displayWhat is up fred

I can't seem to pass the input value to my class

class Tutu():
    def __init__(self, name, color):
        self.color = color
        self.name = name

    def introduce(self):
        print("Hey  {0} I am {1}".format(self.name, self.color) )

zizi = Tutu("Joe", "Potato")
zizi.introduce()


class Zuzu(Tutu):
    def __init__(self, potato):
        self.potato = potato

    def introduce(self):
        print("What's up {0}".format(self.potato))

x = input()
pipi = Zuzu(x)
pipi.introduce()



What I have tried:

I tried adding the input in the __init__ and in the introduce method but it doesn't work.

Error:

NameError: name 'd' is not defined

解决方案

I do not know where that error message comes from but the code above works correctly:


  1. zizi = Tutu("Joe", "Potato") # zizi is an object of class Tutu with those attributes
  2. zizi.introduce() # display "Hey Joe I am Potato"
  3. x = input() # get input from the user, e.g. fred
  4. pipi = Zuzu(x) # pipi is an object of class Zuzu with that attribute (fred)
  5. pipi.introduce() # display "What's up fred"


这篇关于如何获取用户输入并将其传递给我的班级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆