Re: Output showing "None" in Terminal
On 25/08/2020 01:12, Py Noob wrote:
Hi!
i'm new to python and would like some help with something i was working on
from a tutorial. I'm using VScode with 3.7.0 version on Windows 7. Below is
my code and the terminal is showing the word "None" everytime I execute my
code.
Many thanks!
print("Conversion")
def km_mi():
return answer
selection = input("Type mi for miles or km for kilometers: ")
if selection == "mi":
n = int(input(print("Please enter distance in miles: ")))
answer = (1.6*n)
print("%.2f" % answer, "miles")
else:
n = float(input(print("Please enter distance in kilometers: ")))
answer = (n/1.6)
print("%.2f" % answer, "kilometers")
answer = km_mi
Without claiming to speak for @Calvin, I think the original question was
reasonably clear (albeit incomplete), but the code-answer doesn't seem
to fit together and thus invites the question about 'intention'.
Here's a question or two: is this a course-assignment? If so, which
course and is the full question and course-content leading to this point
available on-line? A bunch of people here could write the code. However,
that won't help you learn Python!
Let's look at the code and the email text:
- does the term "None" mean that even the "Conversion" heading/intro
does not appear?
- what is the purpose of the function?
- if the two branches of the if statement each print a result, what is
the purpose of the last line?
- is the specification that the program computes (exactly) one result,
or is it expected to repeat an input-calculate-output cycle?
- by "terminal" are you referring to the one built-in to VS-Code, or
something else?
Please be advised that everyone here is volunteering his/her assistance,
so the more you help us, the better we can help you! Also, are you aware
that there is a Python-Tutor list specifically for Python trainers and
trainees?
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list
Re: ABC with abstractmethod: kwargs on Base, explicit names on implementation
Samuel Marks wrote at 2020-8-24 18:24 +1000: >After a discussion on #python on Freenode, I'm here. > >The gist of it is: >> Falc - Signature of method 'Pharm.foo()' does not match signature of base >> method in class 'Base' > >What's the right way of specialising the children and leaving the Base >pretty much empty? > >Specifically I want: >• All implementers to be forced to implement Base's method >• Base to be able to do very basic things with kwargs, namely log it >(to a file like stdout or a db) >• Support for [at least] Python 3.6–3.9 (I don't think `Protocol` has >been backported?) >• Implementors to have access to the proper name, rather than having >to unpack kwargs > >Should I be using a Base class? - Metaclass? - Something else >entirely? - I know giving `b` a default value would resolve the >[PyCharm] linter error… but I want it to be a required argument. > >Full example: > >from abc import ABC, abstractmethod > > >class Base(ABC): >@abstractmethod >def foo(self, a, **kwargs): > ... >class Pharm(Base): >def foo(self, a, b): > ... Python make a distinction between positional and keyword arguments. A positional argument is identified by its position in the parameter list; a keyword argument is identified by its name. `**` introduces arbitrary keyword arguments. In a call, all those arguments must be passed as "name=value". In your case above, `b` is not a keyword argument and thus is not matched by `**kwargs`. The error you observe is justified. You can try: class Base(ABC): @abstractmethod def foo(self, a, *args, **kwargs): ... class Pharm(Base): def foo(self, a, b, *args, **kwargs): ... Note that the base method signature allows arbitrary positional and keyword arguments. As a consequence, derived methods must do the same. If this is not what you want, you might want to explore the use of a decorator or a meta class rather than a base class. -- https://mail.python.org/mailman/listinfo/python-list
Re: Whitespace not/required
On 2020-08-14 16:29:18 +1200, dn via Python-list wrote:
> For f-strings/formatted string literals, the most usual form is:
>
> "{" f_expression ["="] ["!" conversion] [":" format_spec] "}"
>
> Remembering that this is BNF, see the space separating the closing-brace
> from anything preceding it
No. I see a space before the quote before the closing brace.
> - how else would we separate the components to
> comprehend?
>
> Returning to Python:
>
> >>> one = 1# is the loneliest number...
> >>> f'{ one }'
> '1'
> >>> f'{ one:03 }'
> Traceback (most recent call last):
> File "", line 1, in
> ValueError: Unknown format code '\x20' for object of type 'int'
> >>> f'{ one:03}'
> '001'
>
> Notice the presence/absence of the final space.
>
> >>> pi = 3.14159 # better to import math
> >>> f'{ pi!r:10 }'
> Traceback (most recent call last):
> File "", line 1, in
> ValueError: Unknown format code '\x20' for object of type 'str'
> >>> f'{ pi!r:10}'
> '3.14159 '
> >>> f'{ pi!r }'
> File "", line 1
> SyntaxError: f-string: expecting '}'
> >>> f'{ pi!r}'
> '3.14159'
>
> So, the f-string will work if the braces include only an expression
> surrounded by spaces. However, if one adds a conversion or
> format-specification, that final space becomes a no-no. Eh what!
That doesn't surprise me. The "!" and ":" split the replacement field
into three parts.
The f_expression is just a subset of normal python expressions, which
allow whitespace, so
f"{pi+1}"
f"{pi + 1}"
f"{ pi + 1 }"
f"{ pi + 1 !r}"
f"{ pi + 1 :f}"
are all valid.
The conversion consists only of a single character ("a", "r", or "s"),
anything else is invalid.
The format_spec is everything between the colon and the closing brace.
Syntactically, that can contain spaces. However, that is passed to the
object's __format__() method, and for builtin objects that method
doesn't know what to do with a space (you could implement it for your
own objects, though).
> To be fair, the 'book of words' does say: "A replacement field ends with a
> closing curly bracket '}'.". No mention of whitespace. No mention that a
> replacement field consisting only of an f_expression, will be treated
> differently by allowing a space.
>
> Version 3.8 introduced the "=" short-cut:
>
> >>> f"{ foo = }" # preserves whitespace
> " foo = 'bar'"
>
> Note the comment! Yet, the manual's examples continue:
>
> >>> line = "The mill's closed"
> >>> f"{line = }"
> 'line = "The mill\'s closed"'
> >>> f"{line = :20}"
> "line = The mill's closed "
>
> Hey, why does this second example dispense with the braces-internal spaces?
It doesn't. The space is still there - before the colon.
> Should the closing brace be considered part of a conversion or
> format-specification?
No.
> The space (I'd like to add) cannot be considered part of a conversion
> or format-specification (see BNF, see text of web.ref)!
But it is part of the format specification:
format_spec ::= (literal_char | NULL | replacement_field)*
literal_char ::=
Note: *any* code point except "{", "}" or NULL. So that includes space.
(BTW, what is NULL? U+ doesn't make much sense here (and is usually
written NUL (with one L) and an empty string is not a code point.)
hp
--
_ | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| | | [email protected] |-- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
signature.asc
Description: PGP signature
--
https://mail.python.org/mailman/listinfo/python-list
RE: Output showing "None" in Terminal
The very first line of your function km_mi(): ends it: def km_mi(): return answer answer has not been assigned, so it returns None. Advice: remove that "return" line from there. Also get rid of the last line, answer = km_mi which makes answer refer to the function km_mi(). Put the "return answer" line at the end, where the "answer=km_mi" used to be. That should help. The code calculates "answer". It prints "answer". You should return "answer" at the end, after it has been calculated. --- Joseph S. -Original Message- From: Py Noob Sent: Monday, August 24, 2020 9:12 AM To: [email protected] Subject: Output showing "None" in Terminal Hi! i'm new to python and would like some help with something i was working on from a tutorial. I'm using VScode with 3.7.0 version on Windows 7. Below is my code and the terminal is showing the word "None" everytime I execute my code. Many thanks! print("Conversion") def km_mi(): return answer selection = input("Type mi for miles or km for kilometers: ") if selection == "mi": n = int(input(print("Please enter distance in miles: "))) answer = (1.6*n) print("%.2f" % answer, "miles") else: n = float(input(print("Please enter distance in kilometers: "))) answer = (n/1.6) print("%.2f" % answer, "kilometers") answer = km_mi -- https://mail.python.org/mailman/listinfo/python-list
