[Tutor] Python

Dennis Lee Bieber wlfraed at ix.netcom.com
Thu Oct 21 13:16:23 EDT 2021


On Wed, 20 Oct 2021 18:35:19 -0400, Vanshika Sadhwani <monvansha at gmail.com>
declaimed the following:

	I'm not going to crawl through all of your code but will comment on one
facet.

>Tasks: In this assignment, you will write a complete program in Python that will repeatedly ask the user for a code (integer) and determine whether it is a valid Basic Code, a Positional Code or a UPC Code. It is possible that some integers may be valid for more than one of these codes; even all three! Your program should consist of two Python files:

	<SNIP>
 
>    for code in codes:
>        if basic_code(code) is True:
>            valid_basic.append(code)
>        elif positional_code(code) is True:
>            valid_positional.append(code)
>        elif UPC(code) is True:
>            valid_UPC.append(code)
>        else:
>            none.append(code)
>

	Your if/elif/else tree only saves the FIRST matching type, but the
program description explicitly states that codes could be valid for
multiple formats. You probably need to catch all valid formats, not just
the first one.

>
>print("Summary")
>print("Basic :" + ''.join(valid_basic))
>print("Position :" + ''.join(valid_positional))
>print("UPC :" + ''.join(valid_UPC))
>print("None :" + ''.join(none))

	The  "+" aren't really needed (you are generating one string on the
right, "adding" it to the string on the left [which creates another new
string], writing the result string, and then letting the runtime garbage
collect both the long string, and the string from the .join() ), comma
separated strings are acceptable. NOTE: you probably want to use " " (or '
') on those .join() operations, otherwise you are concatenating all the
values with no delimiters.

	Compare:

No delimiter
>>> print("string 1", "".join(["1", "A", "3"]))
string 1 1A3

Space delimiter
>>> print("string 1", " ".join(["1", "A", "3"]))
string 1 1 A 3

Comma-Space delimiter
>>> print("string 1", ", ".join(["1", "A", "3"]))
string 1 1, A, 3
>>> 


>This is what I am getting when I run the code

	If you attempted to paste an IMAGE, the forum strips attachments (it
may permit text attachments to pass, but no binary or executable formats).


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list