Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how can i not br defined?
#1
a little script to print a horizontal rule with optional tab markers of an optional character (default '#') and an optional interval (default 8).

hr.py:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import print_function
import fcntl,os,struct,termios
from os import environ as e
from sys import argv
try:
    h,w = struct.unpack('4H',fcntl.ioctl(1,termios.TIOCGWINSZ,struct.pack('4H',0,0,0,0)))[:2]
except:
    try:
        fd = os.open('/dev/tty',os.O_WRONLY)
        h,w = struct.unpack('4H',fcntl.ioctl(fd,termios.TIOCGWINSZ,struct.pack('4H',0,0,0,0)))[:2]
        os.close(fd)
    except:
        h,w = 1,80
b='-'
if 'b' in e: b=e['b']
r=[b]*w
if 'i' in e:
    try:
        i=int(e['i'])
    except:
        i=8
if 't' in e:
    t=e['t']
    if not t: t='#'
    for n in range(0,w,i):
        r[n]=t
if len(argv)>2:
    a=' '.join(argv[2:])
    for n in range(min(w,len(a))):
        r[n]=a[n]
print(''.join(r))
then this happens:
Output:
lt1/forums /home/forums 4> t='#' python3 hr.py Traceback (most recent call last): File "hr.py", line 27, in <module> for n in range(0,w,i): NameError: name 'i' is not defined lt1/forums /home/forums 5>
either line 21 or line 23 should define variable "i" in local name space, before control reaches line 27, right? or am i missing something? how can the flow of control avoid both line 21 and line 23?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Maybe the 'i' is not in e:
i=8
if 'i' in e:
    i=int(e['i'])
Reply
#3
What happens when i is not in e.
Reply
#4
do'h ... changing this code around so much has got me blurry-eyed.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python library not defined in user defined function johnEmScott 2 3,885 May-30-2020, 04:14 AM
Last Post: DT2000

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020