Python Forum
Print different positions in loop from functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print different positions in loop from functions
#1
Hello there dear community,

ive been tinkering with some "for loops" and "functions" lately. Here is my code and result:

#no edit wanted
def function_1():
    return "Hello1", "Hello2", "Hello3", "Hello4"

def function_2():
    return "Bye1", "Bye2", "Bye3", "Bye4"

#edit should happen here
for x in function_1():
    for y in function_2():
            print(x , y)
---- RESULT ----

Output:
Hello1 Bye1 Hello1 Bye2 Hello1 Bye3 Hello1 Bye4 Hello2 Bye1 Hello2 Bye2 Hello2 Bye3 Hello2 Bye4 Hello3 Bye1 Hello3 Bye2 Hello3 Bye3 Hello3 Bye4 Hello4 Bye1 Hello4 Bye2 Hello4 Bye3 Hello4 Bye4
The goal of tinkereing with this code for me is to practise fracturing my code into different functions as ive read this is very usefull for later very long codes.

What i want to do with this code is having the following result:

Hello1 Bye1
Hello2 Bye1
Hello3 Bye1
Hello4 Bye1

for that i want to edit the area "#edit should happen here". I also want to do some variations with only printing "Bye2" for example.

how would i do this code as an extension?

Hello1 Bye1

Hello2 Bye1

Hello3 Bye1

Hello4 Bye1



Thanks for all the quick responses so far. I realy love writing my questions here because the answers are always quick and informative :)

Kind regards
Zentu
Reply
#2
all you need to do is modify the print statement to insert the variables. You dont really need a function for an int as range() built-in returns a list of integers to the specified range.
>>> for x in range(1,5):
...     for y in range(1,5):
...             print(f'Hello{x} Bye{y}')
... 
Hello1 Bye1
Hello1 Bye2
Hello1 Bye3
Hello1 Bye4
Hello2 Bye1
Hello2 Bye2
Hello2 Bye3
Hello2 Bye4
Hello3 Bye1
Hello3 Bye2
Hello3 Bye3
Hello3 Bye4
Hello4 Bye1
Hello4 Bye2
Hello4 Bye3
Hello4 Bye4
You can add a newline to print a space between them
>>> for x in range(1,5):
...     for y in range(1,5):
...             print(f'Hello{x} Bye{y}\n')
... 
Hello1 Bye1

Hello1 Bye2

Hello1 Bye3

Hello1 Bye4

Hello2 Bye1

Hello2 Bye2

Hello2 Bye3

Hello2 Bye4

Hello3 Bye1

Hello3 Bye2

Hello3 Bye3

Hello3 Bye4

Hello4 Bye1

Hello4 Bye2

Hello4 Bye3

Hello4 Bye4
If you want the second number to always be 1 Bye1 then just ignore the nested loop y.

You can alternatively skip the nested for loop and use itertools.product
>>> from itertools import product
>>> r = range(1,5)
>>> for x,y in product(r,r):
...     print(f'Hello{x} Bye{y}')
... 
Hello1 Bye1
Hello1 Bye2
Hello1 Bye3
Hello1 Bye4
Hello2 Bye1
Hello2 Bye2
Hello2 Bye3
Hello2 Bye4
Hello3 Bye1
Hello3 Bye2
Hello3 Bye3
Hello3 Bye4
Hello4 Bye1
Hello4 Bye2
Hello4 Bye3
Hello4 Bye4
Recommended Tutorials:
Reply
#3
Thanks for the quick response! Maybe i have been a bit confusing with my code Blush

The numbers after "hello" and "bye" are only there to create different strings in the list. This should make my code clear as of what i want:

def function_1():
    return "Hello Sir", "Hello You", "Hello Mam", "Hello Though"

def function_2():
    return "Bye to me", "Bye to you", "Bye to everyone", "Bye to noone"

for x in function_1():
    for y in function_2():
            print(x , y)
--- result ---

Output:
Hello Sir Bye to me Hello Sir Bye to you Hello Sir Bye to everyone Hello Sir Bye to noone Hello You Bye to me Hello You Bye to you Hello You Bye to everyone Hello You Bye to noone Hello Mam Bye to me Hello Mam Bye to you Hello Mam Bye to everyone Hello Mam Bye to noone Hello Though Bye to me Hello Though Bye to you Hello Though Bye to everyone Hello Though Bye to noone
--- wanted result ---

Output:
Hello Sir Bye to me Hello You Bye to me Hello Mam Bye to me Hello Though Bye to me
Reply
#4
you need to swap outer and inner loop

def function_1():
    return "Hello Sir", "Hello You", "Hello Mam", "Hello Though"
 
def function_2():
    return "Bye to me", "Bye to you", "Bye to everyone", "Bye to noone"
 
for x in function_2():
    for y in function_1():
            print(y, x)
Output:
Hello Sir Bye to me Hello You Bye to me Hello Mam Bye to me Hello Though Bye to me Hello Sir Bye to you Hello You Bye to you Hello Mam Bye to you Hello Though Bye to you Hello Sir Bye to everyone Hello You Bye to everyone Hello Mam Bye to everyone Hello Though Bye to everyone Hello Sir Bye to noone Hello You Bye to noone Hello Mam Bye to noone Hello Though Bye to noone
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thanks for all your help ! :) ive also looked over the forum with some programming friends,

here is one solution i especialy like:

def function_1():
return "Hello Sir", "Hello You", "Hello Mam", "Hello Though"

def function_2():
return "Bye to me", "Bye to you", "Bye to everyone", "Bye to noone"

for x in function_2():
for y in function_1():
print(y , x)
break



have a nice day! :)))
Reply
#6
I didn't understand you want just the first string from function 1

y = function_1()[0] 
# y, *_ = function_1() # this is alternnative
for x in function_2():
    print(y , x)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help with commas in print functions kronhamilton 11 3,422 Feb-10-2022, 02:02 PM
Last Post: mishraakash
Exclamation question about input, while loop, then print jamie_01 5 2,649 Sep-30-2021, 12:46 PM
Last Post: Underscore
  why print('\n') produced 2 new lines instead of 1 - Located inside a FOR loop JulyFire 2 2,519 Jan-10-2021, 01:50 AM
Last Post: JulyFire
  Adding Sliced Positions Gizzmo28 1 1,598 Nov-05-2020, 02:46 AM
Last Post: bowlofred
  Read characters of line and return positions Gizzmo28 2 2,034 Nov-04-2020, 09:27 AM
Last Post: perfringo
  Unable to print stuff from while loop Nick1507 4 2,342 Sep-17-2020, 02:26 PM
Last Post: Nick1507
  Print output in single file using pramika loop deepakkhw 1 2,069 Jul-11-2020, 11:57 AM
Last Post: j.crater
  Unable to combine print statements in for loop adeana 2 1,988 Jun-12-2020, 05:08 PM
Last Post: adeana
  Create, assign and print variables in loop steven_tr 10 4,337 May-28-2020, 04:26 PM
Last Post: ndc85430
  writing in particular positions clarablanes 5 2,821 Apr-13-2019, 04:07 PM
Last Post: clarablanes

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020