Home > Software engineering >  Barcode Generator - homework
Barcode Generator - homework

Time:01-06

I have a homework with nestedloops, but i dont know how to do it. I need to write a program to read 2 four-digit integers and print all the numbers in between that do not contain even digits.

first_num = int(input())
second_num = int(input())
count = 0

for i in range(1, 11):
    for j in range(1, 11):
        for k in range(1, 11):
            for l in range(1, 11):
                count  = 1
                if first_num <= count <= second_num:
                    if i % 2 != 0 and j % 2 != 0 and k != 2 != 0 and l % 2 != 0:
                        print(f"{i}{j}{k}{l}", end=" ")

I have an example with input

3256 6579

expected output:

3357 3359 3377 3379 3557 3559 3577 3579 5357 5359 5377 5379 5557 5559 5577 5579

And my output:

5111 5113 5115 5117 5119 5131 5133 5135 5137 5139 5141 5143 5145 5147 5149 5151 5153 5155 5157 5159 5161 5163 5165 5167 5169 5171 5173 5175 5177 5179 5181 5183....75109

CodePudding user response:

Its my fault. I didnt explain the task well. The program must output all odd numbers between every digit in the input numbers.

first_num = int(input())
second_num = int(input())

for i in range(int(str(first_num)[0]), int(str(second_num)[0])   1):
    for j in range(int(str(first_num)[1]), int(str(second_num)[1])   1):
        for k in range(int(str(first_num)[2]), int(str(second_num)[2])   1):
            for l in range(int(str(first_num)[3]), int(str(second_num)[3])   1):
                if i % 2 != 0 and j % 2 != 0 and k % 2 != 0 and l % 2 != 0:
                    print(f"{i}{j}{k}{l}", end=" ")

CodePudding user response:

For what it's worth, I did analyze the task according to your original explanation of the requirements. If you were to need to get a list of all numbers having nothing but odd-numbered digits between the starting and ending numbers entered using nested testing, the I offer up the following code snippet.

first_num = int(input("Enter first four-digit number: "))
second_num = int(input("Enter second four-digit number: "))

count = 0

for x in range((first_num   1), second_num):
    y = str(x)
    if (y[0:1] != '0' and y[0:1] != '2' and y[0:1] != '4' and y[0:1] != '6' and y[0:1] != '8'):
        if (y[1:2] != '0' and y[1:2] != '2' and y[1:2] != '4' and y[1:2] != '6' and y[1:2] != '8'):
            if (y[2:3] != '0' and y[2:3] != '2' and y[2:3] != '4' and y[2:3] != '6' and y[2:3] != '8'):
                if (y[3:4] != '0' and y[3:4] != '2' and y[3:4] != '4' and y[3:4] != '6' and y[3:4] != '8'):
                    print(x, end = " ")
                    count  = 1
                    if count > 10:
                        print()
                        count = 0

if count != 0:
    print()

In lieu of nested loops, there is a main loop with nested testing. Utilizing the starting and ending values in your example, following was the terminal output.

@Vera:~/Python_Programs/OddNumbers$ python3 All_Odd.py 
Enter first four-digit number: 3357
Enter second four-digit number: 5579
3359 3371 3373 3375 3377 3379 3391 3393 3395 3397 3399 
3511 3513 3515 3517 3519 3531 3533 3535 3537 3539 3551 
3553 3555 3557 3559 3571 3573 3575 3577 3579 3591 3593 
3595 3597 3599 3711 3713 3715 3717 3719 3731 3733 3735 
3737 3739 3751 3753 3755 3757 3759 3771 3773 3775 3777 
3779 3791 3793 3795 3797 3799 3911 3913 3915 3917 3919 
3931 3933 3935 3937 3939 3951 3953 3955 3957 3959 3971 
3973 3975 3977 3979 3991 3993 3995 3997 3999 5111 5113 
5115 5117 5119 5131 5133 5135 5137 5139 5151 5153 5155 
5157 5159 5171 5173 5175 5177 5179 5191 5193 5195 5197 
5199 5311 5313 5315 5317 5319 5331 5333 5335 5337 5339 
5351 5353 5355 5357 5359 5371 5373 5375 5377 5379 5391 
5393 5395 5397 5399 5511 5513 5515 5517 5519 5531 5533 
5535 5537 5539 5551 5553 5555 5557 5559 5571 5573 5575 
5577 

Perhaps this will embellish the spirit of your project.

  • Related