Comments on: Python For Loops and If Statements Combined (Python for Data Science Basics #6) https://data36.com/python-nested-loops-if-statements-combined-data-sciene/ Learn Data Science the Hard Way! Wed, 11 May 2022 13:19:12 +0000 hourly 1 https://wordpress.org/?v=6.7.4 By: Tomi Mester https://data36.com/python-nested-loops-if-statements-combined-data-sciene/#comment-192346 Mon, 29 Mar 2021 15:19:07 +0000 https://data36.com/?p=2054#comment-192346 In reply to Balázs.

Oh, wow, nice job!!! That’s what I call a nested for loop + if statement. 😀
I haven’t tested it but my first thought was that this probably can be made shorter by using Python’s switch + case syntax. (I haven’t written any tutorial about it yet.)

Great job again, and thanks for sharing it, Balazs!

]]>
By: Balázs https://data36.com/python-nested-loops-if-statements-combined-data-sciene/#comment-192248 Wed, 24 Mar 2021 10:43:52 +0000 https://data36.com/?p=2054#comment-192248 Hi Tomi!

This age founding program is cool. And it inspired me to do some similar program too.
I created a program which is able to find out which county do you live in Hungary.
For the first question I give the answer scenarios, the rest of them yes-no questions.
I know it is not a sophisticated one, and it is easy, but I enjoy the work on it!
The for loop is for the wrong answers, but if you give a wrong answer you have to start it over.
Here is the code:

for i in range(1,4):
    answer=input('Do you live in Pest County? (Possible answers: correct, to the west, to the east, to the south) ')
    if answer=='correct':
        print('Good for you!')
    elif answer=='to the south':
        print('You live in Bács-Kiskun County.')
        break
    elif answer=='to the west':
        answer1=input('Do you live in the northern part of this region? (yes, no) ')
        if answer1=='yes':
            answer2=input('Do you live in a county which is next to the eastern border of the country? (yes, no) ')
            if answer2=='yes':
                answer3=input('Do you live in the western part of this part? (yes, no) ')
                if answer3=='yes':
                    print('You live in Győr-Moson-Sopron County.')
                    break
                elif answer3=='no':
                    print('You live in Komárom-Esztergom County.')
                    break
                else:
                    print('Wrong answer.') 
            elif answer2=='no':
                answer4=input('Do you live in a county which is next to the western border of the country? (yes, no) ')
                if answer4=='yes':
                    print('You live in Vas County.')
                    break
                elif answer4=='no':
                    answer5=input('Do you live next to Pest County? (yes, no)')
                    if answer5=='yes':
                        print('You live in Fejér County.')
                        break
                    elif answer5=='no':
                        print('You live in Veszprém County')
                        break
                    else:
                        print('Wrong answer.')
            else:
                print('Wrong answer.')
        elif answer1=='no':
            answer6=input('Does your county located next to the Danube? (yes, no) ')
            if answer6=='yes':
                answer7=input('Does your county located next to the country border? (yes, no) ')
                if answer7=='yes':
                    print('You live in Baranya County.')
                    break
                elif answer7=='no':
                    print('You live in Tolna County.')
                    break
                else:
                    print('Wrong answer.')
            elif answer6=='no':
                answer8=input('Do you live in a county which is located to the south form the Lake Balaton? (yes, no) ')
                if answer8=='yes':
                    print('You live in Somogy County.')
                    break
                elif answer8=='no':
                    print('You live in Zala County.')
                    break
                else:
                    print('Wrong answer.')                  
    elif answer=='to the east':
        answer9=input('Do you live in a county which is next to the Tisza (not intersecting it)? (yes, no) ')
        if answer9=='yes':
            answer10=input('Do you live to the east from the Tisza? (yes, no) ')
            if answer10=='yes':
                answer11=input('Do you live in the eastern county of the country? (yes, no) ')
                if answer11=='yes':
                    print('You live in Szabolcs-Szatmár-Bereg County.')
                    break
                elif answer11=='no':
                    print('You live in Hajdú-Bihar County.')
                    break
                else:
                    print('Wrong answer.') 
            elif answer10=='no':
                answer12=input('Do you live in the northest county of the country? (yes, no) ')
                if answer12=='yes':
                    print('You live in Borsod-Abaúj-Zemplén County.')
                    break
                elif answer12=='no':
                    print('You live in Heves County.')
                    break
                else:
                    print('Wrong answer.')
            else:
                print('Wrong answer.')
        elif answer9=='no':
            answer13=input('Do you live in a county which is intersected by the Tisza (not next to it)? (yes, no) ')
            if answer13=='yes':
                answer14=input('Do you live in a county which is located next to the country border? (yes, no) ')
                if answer14=='yes':
                    print('You live in Csongrád-Csanád County.')
                    break
                elif answer14=='no':
                    print('You live in Jász-Nagykun-Szolnok County.')
                    break
                else:
                    print('Wrong answer.')
            elif answer13=='no':
                answer15=input('Do you live in a county next to Pest County? (yes, no)')
                if answer15=='yes':
                    print('You live in Nógrág County.')
                    break
                elif answer15=='no':
                    print('You live in Békés County.')
                    break
                else:
                    print('Wrong answer.')          
    else:
        print('Wrong answer.')
]]>
By: Tomi Mester https://data36.com/python-nested-loops-if-statements-combined-data-sciene/#comment-190924 Mon, 19 Oct 2020 20:29:20 +0000 https://data36.com/?p=2054#comment-190924 In reply to daudul.

thanks : )

]]>
By: Tomi Mester https://data36.com/python-nested-loops-if-statements-combined-data-sciene/#comment-190846 Tue, 13 Oct 2020 20:52:20 +0000 https://data36.com/?p=2054#comment-190846 In reply to Nandita.

Hi Nandita,
Good question but it’s just basic math.
If you divide 128 with 2, seven-times, you’ll get 1.
So if you always half the distance between the upper and lower value, wcs, you’ll get the final result in 7 steps. (Not even 8 required.)
The question was meant to help with the solution, too! 😉
Tomi

]]>
By: Nandita https://data36.com/python-nested-loops-if-statements-combined-data-sciene/#comment-152364 Sun, 24 May 2020 11:29:25 +0000 https://data36.com/?p=2054#comment-152364 Hi Tomi,

In the above assignment, how did you make sure that the script will find the answer in maximum 8 tries? I can see it did but was that calculated or by chance?

Thanks,
Nandita

]]>
By: daudul https://data36.com/python-nested-loops-if-statements-combined-data-sciene/#comment-76808 Tue, 14 May 2019 03:38:06 +0000 https://data36.com/?p=2054#comment-76808 awsome ..

]]>
By: Python For Loops Explained (Python for Data Science Basics #5) https://data36.com/python-nested-loops-if-statements-combined-data-sciene/#comment-16175 Fri, 27 Apr 2018 12:22:31 +0000 https://data36.com/?p=2054#comment-16175 […] the next article I’ll write about how to combine for loops with for loops and for loops with if statements. It’s gonna be […]

]]>