#1 in the US this week, that title will be taken soon lol. How can I incorporate more countries without using more code? "|" ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

#1 in the US this week, that title will be taken soon lol. How can I incorporate more countries without using more code? "|" ?

https://code.sololearn.com/c1twH578Dm8Y/?ref=app

13th Aug 2017, 1:50 AM
Michael
Michael - avatar
5 Answers
+ 4
Use an array/list. That would actually reduce the code. Optionally, you can use a dictionary or some way to determine if the country is 3rd or 1st world from inside the array/list, without multiple if statements. PS: you should consider using 'elif' when you don't want both if statements to ever both be true.
13th Aug 2017, 2:15 AM
Rrestoring faith
Rrestoring faith - avatar
+ 3
Notice how everything is a 3rd world country except for Poland? This is due to how your if statements are set up. if country == "Canada" # do stuff if country == Poland # do stuff else # do other stuff What this does: if country is Canada, we do stuff. If country is poland, we do stuff. else, we do other stuff. That else statement is only bound to the one if statement for Poland, meaning both if statements are being compared. In other words If the country is canada, the program will still test if the country is Poland, if it isn't Polanf, the else statement runs. Meanwhile, had you of done: if country == "Canada" # do stuff elif country == "Poland" # do stuff else # do stuff If one is true, no others are compared. So, the else statement will only run if all if/elif conditions are false. This will now only say the country is 3rd world if you do not enter one of those options.
13th Aug 2017, 3:55 AM
Rrestoring faith
Rrestoring faith - avatar
+ 1
Okay Awesome, I'll start on a version with array/list. Could you elaborate on elif for me? I think I'm missing something but when would I ever don't want both if statements to be true? (I did use it in a portion of the code ie Russia, China, as a test but I forgot to take it out to be consistent)
13th Aug 2017, 3:30 AM
Michael
Michael - avatar
0
I see, that explains why it unnecessarily printed information about 3rd world. I went in and changed it
13th Aug 2017, 4:21 AM
Michael
Michael - avatar
0
Okay Faith, I added the list! Go check it out, thanks for the feedback and support!
13th Aug 2017, 10:04 AM
Michael
Michael - avatar