Hant 发表于 2020-7-21 20:28:03

照着小甲鱼打的,为啥还有问题

import time as t

class MyTimer():
    def __init__(self):
      self.unit=['year','month','day','hours','minute','sec']
      self.prompt='wei kaishi jishi '
      self.lasted=[]
      self.begin=0
      self.end=0

    def __str__(self):
      return self.prompt

    __repr__=__str__

   
    def start(self):
      self.begin = t.localtime()
      self.prompt='pleasd stop!'
      print("jishikaishi")

    def stop(self):
      if not self.begin:
            print('please start')
      else:
            self.end= t.localtime()
            self._calc()
            print("jishijieshu")

    def _calc(self):
      self.lasted=[]
      self.prompt='zonggongyunxingle'
      for index in range(6):
            self.lasted.append(self.end - self.begin)
            if self.lasted:   
                self.prompt+=(str(self.lasted)+self.unit)


            self.begin=0
            self.end=0

运行结果:
>>> a=MyTimer()
>>> a.start()
jishikaishi
>>> a.stop()
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
    a.stop()
File "D:\PYthon\练习\计时.py", line 28, in stop
    self._calc()
File "D:\PYthon\练习\计时.py", line 36, in _calc
    if self.lasted:
NameError: name 'i' is not defined

Twilight6 发表于 2020-7-21 20:34:41



这样试试看:

import time as t


class MyTimer():
    def __init__(self):
      self.unit = ['year', 'month', 'day', 'hours', 'minute', 'sec']
      self.prompt = 'wei kaishi jishi '
      self.lasted = []
      self.begin = 0
      self.end = 0

    def __str__(self):
      return self.prompt

    __repr__ = __str__

    def start(self):
      self.begin = t.localtime()
      self.prompt = 'pleasd stop!'
      print("jishikaishi")

    def stop(self):
      if not self.begin:
            print('please start')
      else:
            self.end = t.localtime()
            self._calc()
            print("jishijieshu")

    def _calc(self):
      self.lasted = []
      self.prompt = 'zonggongyunxingle'
      for index in range(6):
            self.lasted.append(self.end - self.begin)
            if self.lasted:
                self.prompt += (str(self.lasted) + self.unit)

            self.begin = 0
            self.end = 0

wuqramy 发表于 2020-7-21 20:36:45

if self.lasted[i]:
是index,不是i
import time as t

class MyTimer():
    def __init__(self):
      self.unit=['year','month','day','hours','minute','sec']
      self.prompt='wei kaishi jishi '
      self.lasted=[]
      self.begin=0
      self.end=0

    def __str__(self):
      return self.prompt

    __repr__=__str__

   
    def start(self):
      self.begin = t.localtime()
      self.prompt='pleasd stop!'
      print("jishikaishi")

    def stop(self):
      if not self.begin:
            print('please start')
      else:
            self.end= t.localtime()
            self._calc()
            print("jishijieshu")

    def _calc(self):
      self.lasted=[]
      self.prompt='zonggongyunxingle'
      for index in range(6):
            self.lasted.append(self.end - self.begin)
            if self.lasted:   
                self.prompt+=(str(self.lasted)+self.unit)


            self.begin=0
            self.end=0

Hant 发表于 2020-7-21 20:36:45

Twilight6 发表于 2020-7-21 20:34
这样试试看:

还是不行啊

Hant 发表于 2020-7-21 20:39:09

wuqramy 发表于 2020-7-21 20:36
是index,不是i

改了还是不行

Hant 发表于 2020-7-21 20:44:32

wuqramy 发表于 2020-7-21 20:36
是index,不是i

为啥我改成index还是一样的错误,你能运行吗

Twilight6 发表于 2020-7-21 20:45:04

Hant 发表于 2020-7-21 20:39
改了还是不行



这样就好了:

import time as t


class MyTimer():
    def __init__(self):
      self.unit = ['year', 'month', 'day', 'hours', 'minute', 'sec']
      self.prompt = 'wei kaishi jishi '
      self.lasted = []
      self.begin = 0
      self.end = 0

    def __str__(self):
      return self.prompt

    __repr__ = __str__

    def start(self):
      self.begin = t.localtime()
      self.prompt = 'pleasd stop!'
      print("jishikaishi")

    def stop(self):
      if not self.begin:
            print('please start')
      else:
            self.end = t.localtime()
            self._calc()
            print("jishijieshu")

    def _calc(self):
      self.lasted = []
      self.prompt = 'zonggongyunxingle'
      for index in range(6):
            self.lasted.append(self.end - self.begin)
            if self.lasted:
                self.prompt += (str(self.lasted) + self.unit)

      self.begin = 0
      self.end = 0

wuqramy 发表于 2020-7-21 20:46:24

Hant 发表于 2020-7-21 20:39
改了还是不行

重写__add__漏了
再试试
import time as t

class MyTimer():
    def __init__(self):
      self.unit=['year','month','day','hours','minute','sec']
      self.prompt='wei kaishi jishi '
      self.lasted=[]
      self.begin=0
      self.end=0

    def __add__(self,other):
      prompt = 'zong gong yun xing le'
      result = []
      for index in range(6):
            result.append(self.lasted + other.lasted)
            if result:
                tish += str(result) + self.unit

    def __str__(self):
      return self.prompt

    __repr__=__str__


    def start(self):
      self.begin = t.localtime()
      self.prompt='pleasd stop!'
      print("jishikaishi")

    def stop(self):
      if not self.begin:
            print('please start')
      else:
            self.end= t.localtime()
            self._calc()
            print("jishijieshu")

    def _calc(self):
      self.lasted=[]
      self.prompt='zonggongyunxingle'
      for index in range(6):
            self.lasted.append(self.end - self.begin)
            if self.lasted:
                self.prompt+=(str(self.lasted)+self.unit)


            self.begin=0
            self.end=0

Hant 发表于 2020-7-21 20:47:06

Twilight6 发表于 2020-7-21 20:45
这样就好了:

确实可以了

luoxy 发表于 2020-7-21 20:47:21

#求助,为什么小甲鱼这个代码无法保存和运行
#密码安全性检查check.py
password = input("请输入需要检查的密码组合:")
lenght = len(password)
symbols = r'''~!@#$%^&*_=-/,.?<>;:[]{}|\()'''
alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
numbers = "0123456789"
temp1 = '''高级密码要求:
    1. 密码必须由数字、字母及特殊字符(仅限:~!@#$%^&*()_=-/,.?<>;:[]{}|\)三种组合
    2. 密码只能由字母开头
    3. 密码长度不能低于16位
            '''
temp2 = '''\t您的密码安全级别评定为:'''
temp3 ="请继续保持"
require = '''\t低级密码要求:由单纯的数字或字母组成,长度小于等于8位;
    中级密码要求:由数字、字母或特殊字符任意两种组合,密码长度不能低于8位
    高级密码要求:由数字、字母及特殊字符三种组合,密码只能由字母开头,密码长度不能低于16位
      '''
i=0
#检查password中组成元素:
while 1:
    for each in password:
      if each in symbols:
            i +=1
            break
    for each in password:
      if each in alphabet:
            i +=1
            break
    for each in password:
      if each in numbers:
            i +=1
            break

    def low_password( ):
             result = temp2 +"低"
             print(result)
             print(temp1)
    def mid_password( ):
            result = temp2 +"中"
            print(result)
            print(temp1)
    def high_password( ):
            result = temp2 +"高"
            print(result)
            print(temp3)
    while (password.isspace() orlenght == 0):
      password =input( "\t密码输入错误,原因:空格或未输入。\n\t重新输入要检查的密码组合:")
      lenght = len(password)
      break

    while (i !=1and lenght <= 8 ) :
      password =input( "\t密码输入不符合要求。"+"\n\t密码要求:\n"+require+"\n\t重新输入要检查的密码组合:")
      lenght = len(password)
      break
    while(i ==1 and lenght >8):
      password =input( "\t密码输入不符合要求。"+"\n密码要求:\n"+require+"\n\t重新输入要检查的密码组合:")
      lenght = len(password)
      break
    if lenght <=8 and i ==1:
            low_password()
            break
    elif i==3 and lenght > 16 and password in alphabet:
            high_password()
            break
    else:
            mid_password()
            break

qiuyouzhi 发表于 2020-7-21 20:48:48

luoxy 发表于 2020-7-21 20:47
#求助,为什么小甲鱼这个代码无法保存和运行
#密码安全性检查check.py
password = input("请输入需要检 ...

大哥不会自己发求助帖子吗
被审核了就等等

Twilight6 发表于 2020-7-21 20:49:26

Hant 发表于 2020-7-21 20:47
确实可以了


嗯 你第一个错误上面说了哈 就是,index 打成了 i ,第二个错误就是 self.begin=0 和 self.end=0

写在了 for 循环内,导致一次循环就把原先的 begin 和 end 的值赋值为了 int 整型,而下次用列表索引时候就会报错

把 self.begin=0 和 self.end=0 移动到 for 循环外即可

Hant 发表于 2020-7-21 20:50:07

Twilight6 发表于 2020-7-21 20:45
这样就好了:

我咋没看出改了哪里

wuqramy 发表于 2020-7-21 20:51:13

Hant 发表于 2020-7-21 20:50
我咋没看出改了哪里

最后两句删减了一个缩进

Hant 发表于 2020-7-21 20:52:25

Twilight6 发表于 2020-7-21 20:49
嗯 你第一个错误上面说了哈 就是,index 打成了 i ,第二个错误就是 self.begin=0 和 self.end=0

...

懂了,感谢大佬!!!

Twilight6 发表于 2020-7-21 20:54:08

Hant 发表于 2020-7-21 20:52
懂了,感谢大佬!!!

客气了~
页: [1]
查看完整版本: 照着小甲鱼打的,为啥还有问题