0

The code i wrote just create a structure and print it in multiple lines How can create a string to contain all the Lines

import pyperclip
symbol = input('Symbol = ')
width = int(input('Width = '))
height = int(input('Height = '))

while height > 0:
 print(symbol * width)
 height = height - 1

print('\nCopy to Clipboard ?\nY For Yes\nN For No\n')
sel = input('')
if sel == 'Y':
 pyperclip.copy('Here i want to copy the Structure')
elif sel == 'N':
 print('Done')
4

2 回答 2

0

You can use f-string and addition.

results = ""
while height > 0:
    results += f"{symbol * width}\n"
    height - = 1

print(results)

This should produce the same output as you code, but this time you have a unique string.

于 2019-11-07T11:23:48.413 回答
0

You can use list comprehension to build up the strings and then use them all at once

import pyperclip
symbol = input('Symbol = ')
width = int(input('Width = '))
height = int(input('Height = '))

structure = '\n'.join([symbol * width for x in range(height)])

print('\nCopy to Clipboard ?\nY For Yes\nN For No\n')
sel = input('')
if sel == 'Y':
    pyperclip.copy(structure)
elif sel == 'N':
    print('Done')
于 2019-11-07T11:26:18.583 回答