0

So I wrote this code and it works, however, I'm trying to find a way to make this code return the right output, as shown down below. Here is my code.

upto = 5
def original_num(upto:int):
    for x in range(1, upto+1):
        print (x, (7**x)%97)
def powerLoop(upto: int):
    upto = upto
    return original_num(upto)
print(powerLoop(upto))

But when I print the output, this is what I get...

1 7
2 49
3 52
4 73
5 26
None

But if I use print instead of return in the code,

upto = 5
def powerLoop(upto:int):
    for x in range(1, upto+1):
        print(x, (7**x)%97)
powerLoop(upto)

This is what I get,

1 7
2 49
3 52
4 73
5 26

How can I use return and then once I say print(powerloop(upto) the output will occur without printing 'none'?

Broski
  • 41
  • 6
  • you have you make `original_num` return a value since `powerloop` returns whatever value `original_num` returns – joshmeranda Oct 13 '21 at 14:18
  • What's the purpose of `upto = upto` ? – sj95126 Oct 13 '21 at 14:18
  • yea the upto =upto is probably a mistake on my part – Broski Oct 13 '21 at 14:20
  • Are you saying you want `powerloop` to not print unless you surround it with `print(...)`? That's kind of weird because you tell `original_num` to always `print`. – Peter Wood Oct 13 '21 at 14:22
  • I want to print it out, but when I print it theres the 'none' that occurs below. I wanna know what should I change so that the 'none' doesnt appear – Broski Oct 13 '21 at 14:24

3 Answers3

0

Your original_num(...) function already prints the desired output and returns None which you ultimately print in print(powerLoop(upto)).

Just:

powerLoop(upto)

will not print the return value, but still "harvest" the side-effects of the function calls (the other prints).

user2390182
  • 72,016
  • 6
  • 67
  • 89
  • yea but the requirements for me is that I must use PRINT to yield the output, what changes do I need to make to ensure that it would be possible without yielding none? – Broski Oct 13 '21 at 14:22
0

If you want to write print(powerLoop(upto)) and don't get None you can use yield like below:

>>> upto = 5
>>> def powerLoop(upto:int):
...    for x in range(1, upto+1):
...        yield (x, (7**x)%97)
>>> print(*powerLoop(upto), sep='\n')
(1, 7)
(2, 49)
(3, 52)
(4, 73)
(5, 26)

>>> for ret in powerLoop(upto):
...    print(*ret)
1 7
2 49
3 52
4 73
5 26

If you want to use return you can use list and return list like below: (Edit answer base comment)

>>> upto = 5
>>> def powerLoop(upto:int):
...    out = []
...    for x in range(1, upto+1):
...        out.append([x, (7**x)%97])
...    return out

>>> print(*powerLoop(upto), sep='\n')
[1, 7]
[2, 49]
[3, 52]
[4, 73]
[5, 26]


>>> for ret in powerLoop(upto):
...    print(*ret)

1 7
2 49
3 52
4 73
5 26
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
  • is there a way for the output to not have brackets around it though... like instead of (1, 7) (2, 49) (3, 52), can it be like 1,7 -- 2,49-- 3,52 like that... – Broski Oct 13 '21 at 14:27
  • @Broski edited answer, is this helping you? – I'mahdi Oct 13 '21 at 14:29
  • hmmm,,,, yea it works, is there any like shortcut or faster method, or like the easiest way, so that I, as a noobie, can understand it too :_-() – Broski Oct 13 '21 at 14:33
  • @Broski OK, you want return all value and print them, so you can use yield that do this for you, yield return value but continue running, then you can run for on it. – I'mahdi Oct 13 '21 at 14:35
  • @Broski `for ret in powerLoop(upto): print(*ret)` like `for i in range(4): print(i)` then I use `*` because you return `tuple` with `*` you get only value in tuple – I'mahdi Oct 13 '21 at 14:36
  • 1
    @Broski yes I can, but what do you want to put? – I'mahdi Oct 13 '21 at 14:42
  • okay actually, instead of yield in the code, can u use return instead?? can u use return and then make the output yield the same thing without the 'none,? – Broski Oct 13 '21 at 14:43
  • not yield i mean return LOL my bad – Broski Oct 13 '21 at 14:47
0

Why you print again?

upto = 5
def original_num(upto:int):
    for x in range(1, upto+1):
        print (x, (7**x)%97)
def powerLoop(upto: int):
    return original_num(upto)

powerLoop(upto)

above, the last line, just cancel print, because your function original_num had already print the result.

and in python, if your function have nothing to return, it will return None, and then you print None.

def abc():
    a=100

print(abc())

above example will get None.