heatJack
(Heat Jack)
November 26, 2022, 11:36pm
1
How can I make my program to print “X” if x is true
# -*- coding: UTF-8 -*-
#Sentience 2.6
import typing
Ninf = typing.Callable[[int], bool]
Ninf_map = typing.Callable[[Ninf], bool]
def inj(k: int) -> Ninf:
return lambda n: n < k
def eps(p : Ninf_map) -> Ninf:
return lambda n: min(p(inj(k)) for k in range(n + 1))
def omniscience(p : Ninf_map) -> typing.Optional[Ninf]:
return None if p(x := eps(p)) else x
if x:
print ("X”)
heatJack
(Heat Jack)
November 26, 2022, 11:45pm
2
Also how to I use code format on discussion forums
To use code formatting, you can:
Indent your code by four spaces on every line.
Or better, use “code fences”. Surround your code with three backticks on their own line, like this:
code goes here
Or you can use tildes:
heatJack
(Heat Jack)
November 27, 2022, 12:19am
4
Thanks! Now it looks like a real question !
tjreedy
(Terry Jan Reedy)
November 27, 2022, 12:32am
5
Since the apparent answer to your question is in your code, if x: print('X')
, I don’t know what you question is.
PS. the rest of your code appears to be noise unrelated to the question. Such should be omitted from questions.
1 Like
God, I am so sick of Discuss mangling posts. I hate this buggy platform.
What I tried to write is that instead of three backticks you can use three tildes on a line of their own ~~~
surrounding your code, except Discuss truncated my post and deleted everything from that point on.
I then answered your question:
x = ... # You need to define a value for x first.
if x:
print("X")
Your code already does this, except that you neglect to define a value for x. So you will have got an error that tells you exactly what the problem is: NameError: name 'x' is not defined
.
heatJack
(Heat Jack)
November 27, 2022, 8:50am
7
I’m not looking for if it’s defined I’m looking for if it’s declared
Dutcho
(Dutcho)
November 27, 2022, 9:11am
8
Without going into semantic nuances of “defined” vs. “declared”, I think you want to test for x’s existence.
Using a non-existent name raises a NameError
exception. You can catch that to handle the existent/non-existent cases separately. Like:
try:
x
print("x exists")
except NameError:
print("x doesn't exist")
Alternatively, you can look whether the name (a string) "x"
occurs in the various namespaces.
2 Likes
heatJack
(Heat Jack)
November 27, 2022, 9:39am
9
Thank you for your post.
It appears to me, after using that “try” block that my functions aren’t running…
Is there any way to save the type hinting and also run the functions?
It’s weird cuz if I put down
if omniscience() == x:
print("OXOX")
I get
TypeError: omniscience() missing 1 required positional argument: 'p'
But if I write
if omniscience(Ninf_map) == x:
print("XOXO")
I get
TypeError: Callable() takes no arguments
ndc86430
(ND)
November 27, 2022, 11:21am
10
Haven’t you been here before?
Abstract classes are used to act as parent classes to create subclasses. They are not supposed to be used directly.
typing.Callable exists to be used for typing annotations, not to be called as a function. You have to define a specific function to be called. You cannot call typing.Callable whose purpose is to be used just to describe a type of a function or other callable.
In the following REPL example, foo is a required positional
parameter, while bar and baz have default values. We call the
function by passing positional values for foo (required) and bar
(overridden), but the function uses the default value for baz since
we don’t override that:
>>> def my_func(foo, bar='xyzzy', baz='quux'):
... print(foo, bar, baz)
...
>>> my_func('gralpy', 'plugh')
gralpy plugh quux
I got one last question (believe it or not I am getting responses I can use here and there)
return None if p(x := eps(p)) else x
How do I retrieve what was returned? Is it None or x? How do I fetch the value from a return statement?
etc.
If you want to run the functions, you have to actually call them.
def func(): # This just defines the function.
print("Hello")
func() # This calls the function.
Heat Jack, you’ve been working on this project for many, many months. Maybe a year. Maybe more. And you’ve made zero progress. None at all.
Perhaps it is time for you to work through the Python tutorial so that you learn the basics of programming?
That way you will stop wasting your own time, and ours, by asking questions like
2 Likes