“X” if x (algorithm to output loop)

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”)

Also how to I use code format on discussion forums

To use code formatting, you can:

  1. Indent your code by four spaces on every line.

  2. 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:

Thanks! Now it looks like a real question !

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.

I’m not looking for if it’s defined I’m looking for if it’s declared

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

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

Haven’t you been here before?

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