鱼C论坛

 找回密码
 立即注册
查看: 2063|回复: 5

[已解决]初学python 没有编程基础 这个自己的编程问题出在哪儿

[复制链接]
发表于 2020-10-13 22:51:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
class C:
        def __init__(self,*x):
                self.x = x
        def getNumber(self):
                if len(x):
                        print('一共有%d个参数,' % len(x))
                        for i in x:
                                print(i)
                else:
                        print('无参数')

                       
>>> c = C()
>>> c = C(1,2,3,4,5,6)
>>> c



标准答案
class C:
    def __init__ (self,*args) :  # *arg表示不确定个数的参数
        if  not args :
            print ("并没有传入参数")
        else :
            print ("传入了%d个参数,分别是:"%len(args),end = ' ')
            for each in args :
                print (each , end = ' ')

c = C()
c = C(1,2,3)
最佳答案
2020-10-13 23:04:59
  1. class C:
  2.     def __init__(self,*x):
  3.         self.x = x
  4.     def getNumber(self):  #有报错啊,把这里的x改成self.x
  5.         if len(self.x):
  6.             print('一共有%d个参数:' % len(self.x))
  7.             for i in self.x:
  8.                 print(i)
  9.             return ""  #避免none(笨办法)
  10.         else:
  11.             # print('无参数') #避免返回值为空显示的“none”
  12.             return "无参数 "
  13. c1 = C()
  14. print(c1.getNumber())

  15. c2 = C(1,2,3,4)
  16. print(c2.getNumber())
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-10-13 22:52:40 | 显示全部楼层
print(c.getNumber)
<bound method C.getNumber of <__main__.C object at 0x000001E9B24F4E80>>
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-13 22:58:17 | 显示全部楼层
  1. class C:
  2.         def __init__(self,*x):
  3.                 self.x = x
  4.         def getNumber(self):
  5.                 if len(self.x):
  6.                         print('一共有%d个参数,' % len(self.x))
  7.                         for i in self.x:
  8.                                 print(i)
  9.                 else:
  10.                         print('无参数')
  11. c=C()
  12. c.getNumber()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-10-13 23:04:59 | 显示全部楼层    本楼为最佳答案   
  1. class C:
  2.     def __init__(self,*x):
  3.         self.x = x
  4.     def getNumber(self):  #有报错啊,把这里的x改成self.x
  5.         if len(self.x):
  6.             print('一共有%d个参数:' % len(self.x))
  7.             for i in self.x:
  8.                 print(i)
  9.             return ""  #避免none(笨办法)
  10.         else:
  11.             # print('无参数') #避免返回值为空显示的“none”
  12.             return "无参数 "
  13. c1 = C()
  14. print(c1.getNumber())

  15. c2 = C(1,2,3,4)
  16. print(c2.getNumber())
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-13 23:05:32 | 显示全部楼层
输出为
  1. 无参数
  2. 一共有4个参数:
  3. 1
  4. 2
  5. 3
  6. 4
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-14 21:40:27 | 显示全部楼层
代码帮你改了一下
首先;你犯下的最严重的错误是 getNumber 方法中的if判断当中的条件 len(x) 是错误的.因为x的作用域在 __init__()方法当中. getNumber 方法中从来没有过x这个变量
所以这里要改成 len(seif.x) 可以简单的理解为self.x 的作用域在这个类当中
还有就是你print(c.getNumber)   你这里就是在打印c对象中的getNumber()方法的属性(姑且叫属性吧) 而不是执行getNumber方法 执行的话需要加上()
Python 3.8.6rc1 (tags/v3.8.6rc1:08bd63d, Sep  7 2020, 23:10:23) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> class C:
    def __init__(self,*x):
            self.x = x
    def getNumber(self):
        if len(self.x):
            print('一共有%d个参数,' % len(self.x))
            for i in self.x:
                print(i)
        else:
            print('无参数')

            
>>>
>>> c = C()
>>> c.getNumber()
无参数
>>> c = C(1,2,3,4,5,6,7,8)
>>> c.getNumber()
一共有8个参数,
1
2
3
4
5
6
7
8
>>> c.getNumber
<bound method C.getNumber of <__main__.C object at 0x0000023CB501CC40>>
>>>
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-28 07:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表