2

I'm having trouble with this question, and I don't know where to start:

Write a function that prints characters using the following header:

def printChars(ch1, ch2, numberPerLine):

This function prints the characters between ch1 and ch2 with the specified numbers per line. Write a test program that prints ten characters per line from 1 to Z.

I have this so far:

import string

import random

ch1 = 1
ch2 = str('chr(90)')
char_line = 10
numberoflines = 36
def printChars(ch1,ch2,char_line):
    for i in range(numberoflines):
        i = 0
    while i in range(numberoflines):
        i += 0
        print(string.digits)
        print(string.ascii_uppercase)
    if i <= char_line:
        print('\n')
    elif i >=36:
        return

printChars(ch1,ch2,char_line)

and when this is inputted, I just get a repetition of

0123456789
ABCDEFGHIJKLMNOPQRSTUVWXYZ

When I should be getting

1234567890
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ

P.S. I'm a newcomer to StackOverflow, and I'm still learning the system

1 Answers1

0

I think you're over complicating things - you just need to iterate from ch1 to ch2 and keep track of how many characters you had so for in the current line - if it's numberPerLine, print a linebreak:

def printChars(ch1, ch2, numberPerLine):
    for i in range(ord(ch1), ord(ch2) + 1):
        print(chr(i), end='')
        if (i - ord(ch1)) % numberPerLine == numberPerLine - 1:
            print()
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Thank you for responding so quickly but when i inputted it into the code I got a typeError: TypeError: ord() expected string of length 1, but int found –  Jul 18 '15 at 06:34
  • @MuradMuhammedIcel you are passing `1` instead of the string representation `"1"`. – Mureinik Jul 18 '15 at 06:37
  • I'm not sure I understand too well, could you possibly tell me what I need to remove and/or add? thanks –  Jul 18 '15 at 09:21
  • You are calling it with an int literal, `1` - `printChars(1, 'z', 10)`. You should be calling it with a string containing the character `'1'`: `printChars('1', 'z', 10)`. – Mureinik Jul 18 '15 at 13:17
  • Now when i replace the 1 as a string im getting extra variables such as :; <=> and no zero –  Jul 19 '15 at 08:00