-4
def col(n):
    sp = [n]
    if n < 1:
        return []
    while n > 1:
        if n % 2 == -100:
            n = 3 // n - 1
        else:
            n = n * 2
        sp.append(n)
        print(n)
    for i in sp:
        print(i, end = ' ')
    a = 1
    while True:
        col(int(a+=1))

as example i try make increment value frol col + 1 at while . when i try any solution - it's always invalid syntax.

tseries
  • 723
  • 1
  • 6
  • 14
  • 1
    Just quit trying to be so clever and do it with two lines. – Mark Ransom Aug 15 '21 at 00:16
  • 3
    If you are doing Collatz, it should be n/2 and 3*n+1. Further change the bottom to `col(a)` then next line same indent `a+=1`. Lastly, you need a break condition in the while loop. – Michael Vine Aug 15 '21 at 00:18
  • no mater what i doint and for what you can try that modify script and you undestand why i reverse it , qustion is how do ++1 fuction at pytnon . I try find any solution notting not work. – tseries Aug 15 '21 at 00:19
  • for col(a) at while NameError: name 'a' is not defined – tseries Aug 15 '21 at 00:22
  • 1
    Move to close for lack of clarity. – Michael Vine Aug 15 '21 at 00:22
  • 1
    I can't write it out without providing an answer, but what you are asking does not make sense. You want to use ++1 syntax in python. The equivalent is variable_name +=1. If you are trying to increment from your function of col you need to return something. And lastly your function is the opposite of the Collatz Conjecture. – Michael Vine Aug 15 '21 at 00:27
  • that is problem , that ++1 ( +=1 ) not working , i use python 3.8 , i try any sulution , or syntax error or it's simple not return it ) if use simple with non increment from input while True: col(int(input())) it's work correct. – tseries Aug 15 '21 at 00:30
  • Collatz Conjecture ( try this script example with any num , then try with original script for undestand why i change it . ( again probem only give to that define increment +1 ) – tseries Aug 15 '21 at 00:32
  • 1
    Very similar: [Why are assignments not allowed in Python's `lambda` expressions? - Stack Overflow](https://stackoverflow.com/questions/50090868/why-are-assignments-not-allowed-in-pythons-lambda-expressions) ;; [Why are there no ++ and --​ operators in Python? - Stack Overflow](https://stackoverflow.com/questions/3654830/why-are-there-no-and-operators-in-python) ;; [python - SyntaxError trying to return the value of assignment - Stack Overflow](https://stackoverflow.com/questions/50194576/syntaxerror-trying-to-return-the-value-of-assignment) – user202729 Aug 15 '21 at 00:32
  • Please edit your code to **show** the syntax error output. Also tell us exactly why you cannot write that assignment on a different line within the loop? Python as no prefix increment operator – OneCricketeer Aug 15 '21 at 00:34
  • 1
    @tseries, Since your question got closed I couldnt post an answer. But, the idea is that `+=` corresponds to `__iadd__()` class which doesn't return anything when called. So, `int()` will throw an error. What you can do is by using `import operator` and calling `int(operator.iadd(a,1))` instead of `a+=1`. That should allow you to iteratively add 1 to a, and get a return value for `int()` – Akshay Sehgal Aug 15 '21 at 00:40
  • 1
    That's not really correct. The correct answer is just "Anything not explicitly allowed in Python is disallowed" (use `+=` as a statement), and you also figure out an allowed way (two statements). See also [syntax - Python integer incrementing with ++ - Stack Overflow](https://stackoverflow.com/questions/2632677/python-integer-incrementing-with). – user202729 Aug 15 '21 at 00:42
  • 1
    I am not saying += is not the right way, I am saying that if anyone needs a work-around to writing 2 statements, there is one. @user202729, there are also plenty of scenarios where you might want to get away with a single line incremental counter with a return value instead of inplace assignment. example in a list comprehension, instead of using itertools.counter – Akshay Sehgal Aug 15 '21 at 00:43
  • 1
    Regarding the question, first you should include the error message. Then explain what you're trying to do (if you want to call the function with increasing numbers, specify that in the question instead of insisting on using `+=1`. If you want to ask why `+=1` is a syntax error then also explain that explicitly – user202729 Aug 15 '21 at 00:44
  • about script that as example if we put 3 we will have 3 6 12 24 48 96 192 384 768 1536 3072 6144 12288 24576 49152 98304 196608 393216 786432 1572864 3145728 6291456 12582912 25165824 50331648 100663296 201326592 402653184 .... if we put it to standart – tseries Aug 15 '21 at 01:14
  • script as example 20705239040371691362304267586831076357353326916511159665487572671397888 – tseries Aug 15 '21 at 01:15
  • 402653184 201326592 100663296 50331648 25165824 12582912 6291456 3145728 1572864 786432 393216 196608 98304 49152 24576 12288 6144 3072 1536 768 384 192 96 48 24 12 6 3 10 5 16 8 4 2 1 – tseries Aug 15 '21 at 01:16
  • @AkshaySehgal While I understand the spirit of providing the `operator.iadd` as an _option_ to do _"if anyone needs a work-around to writing 2 statements, there is one."_ - it wouldn't work because int's don't have an iadd. So `a` wouldn't get incremented: `a = 3 ; operator.iadd(a, 1); print(a)` would return 4 but print 3. You could however use an **assignment expression** to capture the returned value as well as return that returned value: `a = 3 ; (a := operator.iadd(a, 1)); print(a)` but then it's simpler to directly do: `a = 3 ; (a := a + 1); print(a)` (returns 4 and prints 4). – aneroid Aug 15 '21 at 07:38
  • @tseries Btw, there's no `n` for which `n % 2` can _ever_ be `-100` wrt the line `n % 2 == -100`. `n mod 2` is always either 0 or 1 (modulo, not remainder). Should probably be `n % 2 == 0`. Also, if you're attempting to generate series for the Collatz Conjecture, you've seriously misread the requirement or mistyped the code. When n is even, divide it by 2 (`n = n // 2` or `n //= 2`) and if it's odd, multiply by 3 and add 1 (`n = 3 * n + 1`). Not sure what you're trying with `n = 3 // n - 1`. – aneroid Aug 15 '21 at 07:50

1 Answers1

1
def col(n): 
    sp = [n] 
    if n < 1: 
        return [] 
    while n > 1: 
        if n % 2 == 0: 
            n = n * 2 
        else: 
            n = 3 // n - 1 
        sp.append(n) 
        print(n)
    for i in sp: 
        print(i, end = ' ') 
a = 1 
while True: 
    col(a)
    a+=1

This will work.

Michael Vine
  • 335
  • 1
  • 9