1

I'm new to Python. I'm trying to create a program that prints set of docs I usually print by hand every week, however I'm running into several problems:

Here is the code:

import os

file_list = os.listdir("C:/Python27/Programs/PrintNgo/Files2print")
print ("List of available documents to print" '\n')

enum_list = ('\n'.join('{}: {}'.format(*k) for k in enumerate(file_list)))
print(enum_list)

user_choice = input('\n' "Documents # you want to print: ")
copies = input("How many copies would you like from each: ")
#not implemented
current_choice = file_list[user_choice]
current_file = os.startfile("C:/Python27/Programs/PrintNgo/Files2print/"+current_choice, "print")

Here is the output:

List of available documents to print

0: doc0.docx
1: doc1.docx
2: doc2.docx
3: doc3.docx
4: doc4.docx
5: doc5.docx

Documents # you want to print: 

I managed to input numbers from 0-5 and print the document desired, however entering 2 values like: 2,3 Do not work and throws an error. How can I print more than one at a time?

If I want to make copies of each doc. Let's say I choosed 2,3 should I do loop to repeat each action as many times as I want the number of copies?

I wonder if my style is fine, however that kind of menu looks nice as well and I can try it eventually

Community
  • 1
  • 1

1 Answers1

1

You should avoid the input function in Python 2. It can be convenient, but it is a security risk. Instead you should use the raw_input function. In Python 3, the function named input is equivalent to Python 2's raw_input and the functionality of the old Python 2 input function has been dropped.

The code below shows how to handle multiple document numbers given in a comma-separated list. The code also handles a single document number. If the user supplies any non-integer values the program will crash with a ValueError. However, blanks spaces are permitted in the input.

from __future__ import print_function

user_choice = raw_input("\nDocuments # you want to print: ")
user_choice = [int(u) for u in user_choice.split(',')]
copies = int(raw_input("How many copies would you like from each: "))

for i in range(copies):
    print('Copy', i + 1)
    for j in user_choice:
        print('Printing document #', j)

Demo

Documents # you want to print: 3, 2,7
How many copies would you like from each: 2
Copy 1
Printing document # 3
Printing document # 2
Printing document # 7
Copy 2
Printing document # 3
Printing document # 2
Printing document # 7

The heart of this code is the str.split method.

user_choice.split(',')

gets the string in user_choice and splits it into a list of strings, splitting wherever it finds a comma, discarding the commas.

[int(u) for u in user_choice.split(',')]

gets each of those resulting strings and converts them to integers, storing the results in a list.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • Edit: it is interesting how the second tag for the python version was assigned by default, since I remember well that I didn't put any, anyway I'm using 3.5 Thank you for your prompt response. I'll try it out when I come home. – salmonOnATrampoline Sep 24 '16 at 16:26
  • @D.Dachkinov That `python-2.7` tag wasn't assigned by default: I added it to your question because your code has "C:/Python27/Programs/PrintNgo/Files2print" and because your code would not work on Python 3 even if you just enter a single document number. [This answer](http://stackoverflow.com/a/17245543/4014959) has some info about running Python 2 and Python 3 on Windows. – PM 2Ring Sep 25 '16 at 07:14
  • Oh, I noticed it now, it is funny how I thought it ran on 3.5 I feel really stupid right now. Thank you for explaining me the `str.split` method. It is very thoughtful and I learned something today – salmonOnATrampoline Sep 25 '16 at 12:10
  • @D.Dachkinov: Hey, there's no need to feel stupid. Plenty of people have run their scripts on Python 2 when they thought they were using Python 3 (or vice versa). :) It's not exactly obvious which version you're using, unless you're in the interactive interpreter, or you're using a language feature that exists in one version but not the other. – PM 2Ring Sep 25 '16 at 12:18
  • @D.Dachkinov: The string `.split` method is _very_ useful. It's quite common to call it without any args: that uses runs of whitespace as the splitting delimiter, see the docs for details. Also note that you can tell `.split` how many times to split via the `maxsplit` arg. There's also an `.rsplit` method. – PM 2Ring Sep 25 '16 at 12:18
  • Thank you very much for explaining that, I'm going to have to go through the documentation of `.split` – salmonOnATrampoline Sep 26 '16 at 18:40